ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
When I compiled and ran the program it should at the end ask if they want to calculate another grade, but instead it automatically ends. Also I had it endl after anytime a incorrect input was recieved, yet it just ignores that and bunches the line like so:
You must select a Student Typeanon@SLACKWARE:~/cpp files/Grades$ WORK YOU FREAKING THING!
-bash: WORK: command not found
anon@SLACKWARE:~/cpp files/Grades$
//Created By Eclipse75
//Grades.cpp
#include <iostream>
#include <string>
cout << "Do you want to calculate a grade? ";
cin >> moreGradesToCalculate;
for (int i = 0;
i < moreGradesToCalculate.length();i++) {
moreGradesToCalculate[i] =
toupper (moreGradesToCalculate[i]);
}
while (moreGradesToCalculate == "YES") {
// What type of student are we calculating?
cout << "Enter student type " <<
"(1=English, 2=Math, 3=Science): ";
cin.getline(response,256);
if (strlen(response) == 0) {
cout << "You must enter a student type" << endl;
return 1;
}
if ((atoi(response) < 1) | (atoi(response) > 3)) {
cout << response <<
" - is not a valid student type" << endl;
return 1;
}
// Student type is valid, now let's calculate the grade
switch(atoi(response))
{
// Case 1 is an English Student
case 1:
cout << endl << endl << "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 Exam grade is: " <<
finalExamGrade << endl;
cout << "Research grade is: " <<
research << endl;
cout << "Presentation grade is: " <<
presentation << endl;
cout << "Final Numeric grade is: " <<
finalNumericGrade << endl;
cout << "Final Letter grade is: " <<
finalLetterGrade << endl << endl;
break;
// Case 2 is a Math Student
case 2:
cout << "Enter the Midterm Grade: " << endl;
cin.getline(response,256);
midterm = atoi(response);
cout << "Enter the Final Examination Grade: " << endl;
cin.getline(response,256);
finalExamGrade = atoi(response);
finalNumericGrade = (MATH_MIDTERM_PERCENTAGE * midterm) +
(MATH_FINALEXAM_PERCENTAGE * finalExamGrade);
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;
cout << "Midterm grade is: " <<
midterm << endl;
cout << "Final Exam 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 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);
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;
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 <<
"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 << endl <<
"Thanks for using the Grades Calculation program!" << endl;
return 0;
} // end of main
I appreciate all those who have helped me. All others, I'm disappointed.
I normally don't help with homework stuff, but the problem you have is something that drove me nuts many years ago. You're dealing with a problem where some user input is left in the input buffer, and it gets mis-interpreted. Long story short, I would suggest trying to figure out a way to use cin.getline() for reading in the yes/no response from the user.
Also, in your if-statements. You're not using the right AND and OR operators. What you have in the code you posted are binary operators, not logical operators. Trust me, you want to use && and || instead. No really, trust me. What you have might work for this program, but it is a very bad habit you should avoid.
Edit:
Also, to help others help you, always, always, always use code tags around code you post.
Last edited by Dark_Helmet; 03-27-2005 at 08:59 PM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.