ok so I have a class assignment that asks me to write a program that will give a letter grade based on what numerical value one inputs. so for example if I put in a numerical value between a 90 and 100 the letter A comes out. if its between 80 and 90 then a B comes out and so on.i cant find a way for it to check if a number lower than 80 was inputed AND output a correct letter grade. what i was trying to do was just keep going through checks until one check is true and then output the correct letter grade but i cant seem to get that to work. if someone could help it wou;d be awesome. I just started learning c++ so im a total noob so please dont assume i know what you are talking about. this is what i have so far...
#include <iostream>
using namespace std;
int main()
{
int grade; // grade variable
bool validNumber = false;
bool validGrade_1 = false;
// checks to see if number inputed is between 0 and 100
// if not then error message occurs and ask to input number again
// then loops until number inputed is between 0 and 100
while (validNumber == false) {
cout << "Please enter a grade between 0 and 100: ";
cin >> grade;
if ((grade < 0 ) || (grade > 100)) {
cout << "ERROR the grade you entered is not between 0 and 100. \n\n";
}
else
validNumber = true;
}
// checks to see what numerical grade was inputed in
// order to determine what letter grade to output
while (validGrade_1 == false) {
if (grade >= 90) {
cout << "The student has an A. \n\n";
break;
}
else
if ((grade <=89.99) || (grade > 79.99)) {
cout << "The student has a B \n";
break;
}
}
// I am stumped after this. I can not find a way for it to correctly
// decide whether a number smaller than 79.99 was inputed so that it can display
// a proper letter grade. When I try to continue, it just keeps on outputting B as a letter grade
I don't understand what while (validGrade_1 == false) { is for? Up until that point it seemed to be ok, but then I lose what your trying to do. After you come out of the first while loop why not just have a few if statements:
1 2 3 4 5 6 7 8 9
if (grade >= 90) {
cout << "The student has an A. \n\n";
}elseif(grade >=80){
cout << "The student has an B. \n\n";
}elseif(grade >=70){
cout << "The student has an C. \n\n";
}else{
cout << "The student failed. \n\n";
}
Or something to that effect.
Also when you are posting code use code tags [code]code goes here[/code]
EDIT: Haha, how do you show someone how to use code tags?
ok so i managed to get all of that to work but now im stuck again. now i have to figure out a way for the program to check if the grade entered is 0.5 points or less from the next letter grade. I got it to check but i cant figure out how to set a boundary. for example, in the code, the program will check to see if the grade entered it greater than 99.5 and if its not then it goes on to the next check which is to check if the number is greater than 89.5. the problem im having is that lets say i input 94.5 as my grade. it will skip the first check but not the second because technically 94.5 is greater than 89.5 so it will run the code under the check but then that grade is not 0.5 points away from the next letter grade. If anyone can help i would really appreciate it.
#include <iostream>
usingnamespace std;
int main()
{
double grade; // grade variable
bool validNumber = false; // constant for numerical grade check
bool validGrade = false; // constant for grade over 100 check
char yesno = 'y'; // asks user yes or no
char correct_grade = 'y'; // asks user yes or no
char round_up_grade = 'n'; // asks user yes or no
// checks to see if number inputed is less than 0
// if number inputed is less than 0, error message occurs and ask to input number again
// then loops until number inputed is greater than 0
while (validNumber == false) {
cout << "Please enter a grade between 0 and 100: ";
cin >> grade;
cout << endl;
if (grade < 0 ) {
cout << "ERROR the grade you entered is not between 0 and 100. \n\n";
}
else validNumber = true;
}
// If grade entered is over 100, the program asks if that is correct.
// If it is correct then the program moves on
// If it is incorrect the it asks for a numerical grade again
while (validGrade == false) {
if (grade > 100) {
cout << "Are you sure this grade is correct? \n\n Type y for Yes, n for no: ";
cin >> correct_grade;
cout << endl;
}
if (correct_grade == 'n') {
cout << "Please enter a grade between 0 and 100: ";
cin >> grade;
cout << endl;
}
else validGrade = true;
}
// checks to see if number is 0.5 points away from next letter grade
// if it is then the program asks the user if they wish to round up
// to the next letter grade
if (grade >= 99.5) {
cout << "Grade is " << grade << " which is 0.5 points or less away from the next letter grade. \n\n";
cout << "Would you like to round up to the next letter grade?\n\n";
cout << "y for Yes, n for No: ";
cin >> round_up_grade;
cout << endl;
}
elseif (grade >= 89.5) {
cout << "Grade is " << grade << " which is 0.5 points or less away from the next letter grade. \n\n";
cout << "Would you like to round up to the next letter grade?\n\n";
cout << "y for Yes, n for No: ";
cin >> round_up_grade;
cout << endl;
}
elseif (grade >= 79.5) {
cout << "Grade is " << grade << " which is 0.5 points or less away from the next letter grade. \n\n";
cout << "Would you like to round up to the next letter grade?\n\n";
cout << "y for Yes, n for No: ";
cin >> round_up_grade;
cout << endl;
}
elseif (grade >= 69.5) {
cout << "Grade is " << grade << " which is 0.5 points or less away from the next letter grade. \n\n";
cout << "Would you like to round up to the next letter grade?\n\n";
cout << "y for Yes, n for No: ";
cin >> round_up_grade;
cout << endl;
}
elseif (grade >= 59.5) {
cout << "Grade is " << grade << " which is 0.5 points or less away from the next letter grade. \n\n";
cout << "Would you like to round up to the next letter grade?\n\n";
cout << "y for Yes, n for No: ";
cin >> round_up_grade;
cout << endl;
}
if (round_up_grade == 'y') {
grade = grade + 0.5;
cout << "The new grade is " << grade;
cout << endl;
}
// Checks to see if student is eligible for extra credit
// If user answers yes then studen gets 5 extra points
// If user answers no then grade remains the same
cout << "Is the student eligible for extra credit? \n\n" << "Type y for yes, n for no: ";
cin >> yesno;
cout << endl;
if (yesno == 'y'){
grade = grade+5;
}
elseif (yesno == 'n') {
grade = grade;
}
// checks to see what numerical grade was calculated based on users decisions in
// order to determine what letter grade to output
if (grade > 100) {
cout << "The student has an A+. \n\n";
} elseif(grade >=90){
cout << "The student has an A. \n\n";
} elseif(grade >=80){
cout << "The student has a B. \n\n";
} elseif(grade >=70){
cout << "The student has a C. \n\n";
} elseif(grade >=60){
cout << "The student has a D. \n\n";
} elseif(grade < 60){
cout << "The student has an F. \n\n";
}
system ("pause");
return 0;
}
ok so i managed to get all of that to work but now im stuck again. now i have to figure out a way for the program to check if the grade entered is 0.5 points or less from the next letter grade. I got it to check but i cant figure out how to set a boundary. for example, in the code, the program will check to see if the grade entered it greater than 99.5 and if its not then it goes on to the next check which is to check if the number is greater than 89.5. the problem im having is that lets say i input 94.5 as my grade. it will skip the first check but not the second because technically 94.5 is greater than 89.5 but then that grade is not 0.5 points away from the next letter grade. I guess what i am saying is that i don't know how to set a boundary. If anyone can help i would really appreciate it.
#include <iostream>
usingnamespace std;
int main()
{
double grade; // grade variable
bool validNumber = false; // constant for numerical grade check
bool validGrade = false; // constant for grade over 100 check
char yesno = 'y'; // asks user yes or no
char correct_grade = 'y'; // asks user yes or no
char round_up_grade = 'n'; // asks user yes or no
// checks to see if number inputed is less than 0
// if number inputed is less than 0, error message occurs and ask to input number again
// then loops until number inputed is greater than 0
while (validNumber == false) {
cout << "Please enter a grade between 0 and 100: ";
cin >> grade;
cout << endl;
if (grade < 0 ) {
cout << "ERROR the grade you entered is not between 0 and 100. \n\n";
}
else validNumber = true;
}
// If grade entered is over 100, the program asks if that is correct.
// If it is correct then the program moves on
// If it is incorrect the it asks for a numerical grade again
while (validGrade == false) {
if (grade > 100) {
cout << "Are you sure this grade is correct? \n\n Type y for Yes, n for no: ";
cin >> correct_grade;
cout << endl;
}
if (correct_grade == 'n') {
cout << "Please enter a grade between 0 and 100: ";
cin >> grade;
cout << endl;
}
else validGrade = true;
}
// checks to see if number is 0.5 points away from next letter grade
// if it is then the program asks the user if they wish to round up
// to the next letter grade
if (grade >= 99.5) {
cout << "Grade is " << grade << " which is 0.5 points or less away from the next letter grade. \n\n";
cout << "Would you like to round up to the next letter grade?\n\n";
cout << "y for Yes, n for No: ";
cin >> round_up_grade;
cout << endl;
}
elseif (grade >= 89.5) {
cout << "Grade is " << grade << " which is 0.5 points or less away from the next letter grade. \n\n";
cout << "Would you like to round up to the next letter grade?\n\n";
cout << "y for Yes, n for No: ";
cin >> round_up_grade;
cout << endl;
}
elseif (grade >= 79.5) {
cout << "Grade is " << grade << " which is 0.5 points or less away from the next letter grade. \n\n";
cout << "Would you like to round up to the next letter grade?\n\n";
cout << "y for Yes, n for No: ";
cin >> round_up_grade;
cout << endl;
}
elseif (grade >= 69.5) {
cout << "Grade is " << grade << " which is 0.5 points or less away from the next letter grade. \n\n";
cout << "Would you like to round up to the next letter grade?\n\n";
cout << "y for Yes, n for No: ";
cin >> round_up_grade;
cout << endl;
}
elseif (grade >= 59.5) {
cout << "Grade is " << grade << " which is 0.5 points or less away from the next letter grade. \n\n";
cout << "Would you like to round up to the next letter grade?\n\n";
cout << "y for Yes, n for No: ";
cin >> round_up_grade;
cout << endl;
}
if (round_up_grade == 'y') {
grade = grade + 0.5;
cout << "The new grade is " << grade;
cout << endl;
}
// Checks to see if student is eligible for extra credit
// If user answers yes then studen gets 5 extra points
// If user answers no then grade remains the same
cout << "Is the student eligible for extra credit? \n\n" << "Type y for yes, n for no: ";
cin >> yesno;
cout << endl;
if (yesno == 'y'){
grade = grade+5;
}
elseif (yesno == 'n') {
grade = grade;
}
// checks to see what numerical grade was calculated based on users decisions in
// order to determine what letter grade to output
if (grade > 100) {
cout << "The student has an A+. \n\n";
} elseif(grade >=90){
cout << "The student has an A. \n\n";
} elseif(grade >=80){
cout << "The student has a B. \n\n";
} elseif(grade >=70){
cout << "The student has a C. \n\n";
} elseif(grade >=60){
cout << "The student has a D. \n\n";
} elseif(grade < 60){
cout << "The student has an F. \n\n";
}
system ("pause");
return 0;
}
Why not just have more if statements. Something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
if (grade >= 90) {
cout << "The student has an A. \n\n";
}elseif (grade >=89.5){
cout << "Grade is " << grade << " which is 0.5 points or less away from the next letter grade. \n\n";
cout << "Would you like to round up to the next letter grade?\n\n";
cout << "y for Yes, n for No: ";
cin >> round_up_grade;
cout << endl;
}elseif(grade >=80){
cout << "The student has an B. \n\n";
}elseif(grade >=70){
cout << "The student has an C. \n\n";
}else{
cout << "The student failed. \n\n";
}
ok that works fine for anything greater than 99.5 but then what would I do if i wanted to use a grade between 99.4 and 80? In this code below it will do what is after the elseif (grade >= 89.5) even if the grade is not 0.5 points away from the next letter grade. so if i use 99.4 as the grade it will print out that it is 0.5 points or less away from the next letter grade even though its not because technically 99.4 is greater than or equal to 89.5.
Like is there anyway that i could set a boundary like (100 > grade >= 99.5) or (90 > grade >= 89.5) that way it checks to see if the grade is only between those set boundaries.
if (grade > 100) {
cout << "The student has an A+ \n\n";
cout << endl;
}
elseif (grade >= 99.5) {
cout << "Grade is " << grade << " which is 0.5 points or less away from the next letter grade. \n\n";
cout << "Would you like to round up to the next letter grade?\n\n";
cout << "y for Yes, n for No: ";
cin >> round_up_grade;
cout << endl;
}
elseif (grade >= 89.5) {
cout << "Grade is " << grade << " which is 0.5 points or less away from the next letter grade. \n\n";
cout << "Would you like to round up to the next letter grade?\n\n";
cout << "y for Yes, n for No: ";
cin >> round_up_grade;
cout << endl;
}
if (grade == 100) {
cout << "The student has an A+. \n\n";
}elseif (grade >=99.5){
cout << "Grade is " << grade << " which is 0.5 points or less away from the next letter grade. \n\n";
cout << "Would you like to round up to the next letter grade?\n\n";
cout << "y for Yes, n for No: ";
cin >> round_up_grade;
cout << endl;
}elseif (grade >= 90) {
cout << "The student has an A. \n\n";
}elseif (grade >=89.5){
cout << "Grade is " << grade << " which is 0.5 points or less away from the next letter grade. \n\n";
cout << "Would you like to round up to the next letter grade?\n\n";
cout << "y for Yes, n for No: ";
cin >> round_up_grade;
cout << endl;
}elseif (grade >= 80) {
cout << "The student has an B. \n\n";
}elseif (grade >=79.5){
cout << "Grade is " << grade << " which is 0.5 points or less away from the next letter grade. \n\n";
cout << "Would you like to round up to the next letter grade?\n\n";
cout << "y for Yes, n for No: ";
cin >> round_up_grade;
cout << endl;
}elseif (grade >= 70) {
cout << "The student has an C. \n\n";
}elseif (grade >=69.5){
cout << "Grade is " << grade << " which is 0.5 points or less away from the next letter grade. \n\n";
cout << "Would you like to round up to the next letter grade?\n\n";
cout << "y for Yes, n for No: ";
cin >> round_up_grade;
cout << endl;
}else{
cout << "The student failed. \n\n";
}
you can add in more if statements if you want more grades to be displayed.
I tried something like that but for some reason it did not work..... I feel really stupid.... sorry for making you go through all this trouble. I really appreciate it though. Thank you for all your help!
Yea no problem. However for the code above you shouldn't need to explicitly say it is between two particular values in any if statement as long as you have each statement checking for "greater than" or "greater than or equal to" as with "else if"s only one of the if statements will execute.