Hello everyone. I am getting into the gist of the C++ programming language. =) I have come up with a problem and I've been brainstorming for hours. It has something to do with a Grade Calculation program. I have been following the code, line by line, to the letter and it doesn't seem to do what it is suppose to. What happens is that upon executing, when I type yes, it falls through to the if loop, and ends. Not sure why. I have reason to believe that I have to clear cin, but that's just one way to put it. The source code for the program is below. All of this is from a book I am reading. It's pretty good. 'Learning to program with C++' by John Smiley. I don't know if it's an error of mine, but the code's been read and re-read countless times, and it matches every line.
I am hoping that someone can assist me with this. I did my version without using the while loop and it works the way it should. What I did was use return to main() and to another function I created called question(). It maintains the loop until the user says to end it. I hope someone can help me solve this one. If I can get the author's code to work, I'd be happy.
Thanks in advance.
cout << "Do you want to calculate a grade? ";
cin >> moreGradesToCalculate;
cin.clear();
for(int i = 0; i < moreGradesToCalculate.length(); i++){
moreGradesToCalculate[i] = toupper(moreGradesToCalculate[i]);
}
while(moreGradesToCalculate == "YES"){
// What student type are we calculating?
cout << "Enter student type (1 = English, 2 = Math, 3 = Science): ";
cin.getline(response,256);
if(strlen(response) == 0){
cout << "You must select a Student Type";
return 1;
}
if((atoi(response) < 1) | (atoi(response) > 3)){
cout << response << " - is not a valid Student Type";
return 1;
}
// Student type is valid, now lets calculate the grade
switch(atoi(response))
{
// Case 1 is an English Student
case 1:
cout << "Enter the Midterm Grade: ";
cin.getline(response,256);
midterm = atoi(response);
cout << "Enter the Final Examination Grade: ";
cin.getline(response,256);
finalExamGrade = atoi(response);
cout << "Enter the Research Grade: ";
cin.getline(response,256);
research = atoi(response);
cout << "Enter the Presentation Grade: ";
cin.getline(response,256);
presentation = atoi(response);
finalNumericGrade =
(midterm * ENGLISH_MIDTERM_PERCENTAGE) +
(finalExamGrade * ENGLISH_FINALEXAM_PERCENTAGE) +
(research * ENGLISH_RESEARCH_PERCENTAGE) +
(presentation * ENGLISH_PRESENTATION_PERCENTAGE);
if(finalNumericGrade > 93)
finalLetterGrade = 'A';
else if((finalNumericGrade >= 85) && (finalNumericGrade < 93))
finalLetterGrade = 'B';
else if((finalNumericGrade >= 78) && (finalNumericGrade < 85))
finalLetterGrade = 'C';
else if((finalNumericGrade >= 70) && (finalNumericGrade < 78))
finalLetterGrade = 'D';
else if(finalNumericGrade < 70)
finalLetterGrade = 'F';
cout << endl << "*** ENGLISH STUDENT ***" << endl << endl;
cout << "Midterm Grade is: " << midterm << endl;
cout << "Final Examination Grade is: " << finalExamGrade << endl;
cout << "Research Grade is: " << research << endl;
cout << "Presentation Grade is: " << presentation << endl << endl;
cout << "Final Numeric Grade is: " << finalNumericGrade << endl;
cout << "Final Letter Grade is: " << finalLetterGrade;
break;
// Case 2 is a Math Student
case 2:
cout << "Enter the Midterm Grade: ";
cin.getline(response,256);
midterm = atoi(response);
cout << "Enter the Final Exam Grade: ";
cin.getline(response,256);
finalExamGrade = atoi(response);
finalNumericGrade =
(midterm * MATH_MIDTERM_PERCENTAGE) +
(finalExamGrade * MATH_FINALEXAM_PERCENTAGE);
if(finalNumericGrade >= 90)
finalLetterGrade = 'A';
else if((finalNumericGrade >= 83) && (finalNumericGrade < 90))
finalLetterGrade = 'B';
else if((finalNumericGrade >= 76) && (finalNumericGrade < 83))
finalLetterGrade = 'C';
else if((finalNumericGrade >= 65) && (finalNumericGrade < 76))
finalLetterGrade = 'D';
else if(finalNumericGrade < 65)
finalLetterGrade = 'F';
cout << endl << "*** MATH STUDENT ***" << endl << endl;
cout << "Midterm Grade is: " << midterm << endl;
cout << "Final Examination Grade is: " << finalExamGrade << endl;
cout << "Final Numeric Grade is: " << finalNumericGrade << endl;
cout << "Final Letter Grade is: " << finalLetterGrade;
break;
// Case 3 is a Science Student
case 3:
cout << "Enter Midterm Grade: ";
cin.getline(response,256);
midterm = atoi(response);
cout << "Enter the Final Examination Grade: ";
cin.getline(response,256);
finalExamGrade = atoi(response);
cout << "Enter the Research Grade: ";
cin.getline(response,256);
research = atoi(response);
finalNumericGrade =
(midterm * SCIENCE_MIDTERM_PERCENTAGE) +
(finalExamGrade * SCIENCE_FINALEXAM_PERCENTAGE) +
(research * SCIENCE_RESEARCH_PERCENTAGE);
if(finalNumericGrade >= 90)
finalLetterGrade = 'A';
else if((finalNumericGrade >= 80) && (finalNumericGrade < 90))
finalLetterGrade = 'B';
else if((finalNumericGrade >= 70) && (finalNumericGrade < 80))
finalLetterGrade = 'C';
else if((finalNumericGrade >= 60) && (finalNumericGrade < 70))
finalLetterGrade = 'D';
else if(finalNumericGrade < 60)
finalLetterGrade = 'F';
cout << endl << "*** SCIENCE STUDENT ***" << endl << endl;
cout << "Midterm Grade is: " << midterm << endl;
cout << "Final Exam Grade is: " << finalExamGrade << endl;
cout << "Research Grade is: " << research << endl;
cout << "Final Numeric Grade is: " << finalNumericGrade << endl;
cout << "Final Letter Grade is: " << finalLetterGrade << endl;
break;
default:
cout << response << " - is not a valid student type";
return 1;
} // end of switch
cout << endl << endl << "Do you have another grade to calculate? ";
cin >> moreGradesToCalculate;
for(int i = 0; i < moreGradesToCalculate.length(); i++){
moreGradesToCalculate[i] = toupper(moreGradesToCalculate[i]);
} // end of for
} // end of while
cout << "Thanks for using the Grades Calculation program!";
I haven't looked at your entire program because thats a lot of code to look at while it is not formatted in code tags but 'moreGradesToCalculate' is a string object and you're using cin>> to get input. You need to be using getline(cin, moreGradesToCalculate);. This is the best way to get input for string objects.
Your while loop, (the one that follows) should be an if statement, and you should check whether it is equal to "yes" by using the functions available e.g.: