Skip to content

Instantly share code, notes, and snippets.

@DanielDe
Created October 22, 2012 07:20
Show Gist options
  • Save DanielDe/3930089 to your computer and use it in GitHub Desktop.
Save DanielDe/3930089 to your computer and use it in GitHub Desktop.
CS 10 Classroom Session 4 User Input Validation
#include <iostream>
using namespace std;
int main() {
int score1, score2;
// collect score 1
cout << "Enter your score on the first exam (0 - 100): ";
cin >> score1;
while (score1 > 100 || score1 < 0) {
cout << "Score not in the range 0-100. Please re-enter your score on the first exam: ";
cin >> score1;
}
cout << score1 << endl;
// collect score 2
cout << "Enter your score on the second exam (0 - 100): ";
cin >> score2;
while (score2 > 100 || score2 < 0) {
cout << "Score not in the range 0-100. Please re-enter your score on the second exam: ";
cin >> score2;
}
// determine necessary score on final exam
const int PASSING_GRADE = 70;
int necessary_grade = PASSING_GRADE * 3 - score1 - score2;
if (necessary_grade > 100) {
cout << "You cannot pass the course" << endl;
} else {
cout << "You need a " << necessary_grade << " to pass." << endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment