confusion with if else statement
Feb 9, 2021 at 2:54am UTC
If I enter one number < 60 and one > 60 why is it still printing student failed? and
If I enter two numbers above 95 it doesn't print Student Graduated with Honors :) it just prints student graduated.
What am I doing wrong?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
int main() {
int gradeOne, gradeTwo;
char space;
cout<< "Please enter 2 grades, separated by a space: " <<endl;
cin>>gradeOne>>space>>gradeTwo;
if (gradeOne < 60 && gradeTwo < 60 )
cout << "Student Failed :(" <<endl;
if (gradeOne >= 95 && gradeTwo >= 95 )
cout << "Student Graduated with Honors :)" <<endl;
else
cout << "Student Graduated!" <<endl;
return 0;
}
Last edited on Feb 9, 2021 at 2:55am UTC
Feb 9, 2021 at 2:58am UTC
cin's operator >> is already whitespace delimited.
You do not need the 'char space' or to explicitly extract the space in-between the numbers.
Just do
cin >> gradeOne >> gradeTwo;
Also, you want your second if statement to actually be "else if" so that it's chained with the first if statement (if, else if, else).
Last edited on Feb 9, 2021 at 3:00am UTC
Feb 9, 2021 at 3:02am UTC
thank you! My instructor confused me with that char lol
Feb 9, 2021 at 6:31pm UTC
Hello int321,
Not saying that
Ganado is wrong, but there is a time when the "char" can be useful.
Consider:
1 2 3 4
int hours{}, minutes{}, seconds{};
char junk{};
std::cin >> hours >> junk >> minutes >> junk >> seconds;
Then when you enter a time of "10:15:30" the "10" is placed in "hours", the "15" is placed in "minutes" and the "30" is placed in "seconds. Discarding the colons as they are read into the "junk" variable.
Andy
Feb 9, 2021 at 7:15pm UTC
Yeah Andy. That was actually my previous lab with time. That’s why I was a bit confused with char by the way my instructor wrote the solution.
Topic archived. No new replies allowed.