How do I fix this while loop?

Hi guys. My source code would not loop. It does not go beyond the if statements? I've tried break; and continue; but neither works? How do I solve this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <string.h>
char answer[10];
int main()
{
    do
    {


    float grade;
    std::cout<< "Please enter your grade \n";
    std::cin>> grade;
    if (grade >=70)
        {
            if(grade>=90)
            {
                std::cout<< "You passed with an A!";
                break;
            }
            if(grade >=80)
            {
                std::cout<<"You passed with a B! \n";
                break;
            }
            std::cout<< "You passed with a C \n";
            break;;

        }
        else
        {
            std::cout<< "You failed \n";
        }
        std::cout<<"Would you like to start over? \n";

        std::cin>>answer;
    }while(strcmp(answer,"Yes")==0);
}


Thank You.
I'm pretty sure break should only be used with loops. So for example is grade == 90. Then it enters the first if(grade >=70) , then enters the second if(grade>=90) and reaches the break. Here it breaks out of the smallest loop that it is in, which is the do while loop, and finishes your main function.

Delete all the breaks, you don't need them. That's not your only problem, so ask if you can't figure the rest out.
Thanks TheMeerKat! Yeah, I took the breaks out and it worked.

But, then it gave me a new problem. Now, it doesn't skip at all and names all of the if statements.

For example, when I type in 90, it would say "You passed with a C. You passed with a B. You passed with an A".

Like, what technique can I use to prevent this? Obviously I cannot use break; or continue;.

Do you have any ideas?
Yup, you want to use "else if" statements. And you don't actually have to use nested loops at all. Just start with the first if statement checking if it is over 90. Then have else ifs for over 80, and over 70 and finally have just an else statement saying that they failed.

Try that, and if you still can't work it out, post what you have so far and I'll see what I can do.
Thank you, TheMeerkat. If there was a star system on this site, I would give you 5 stars.
This is my new, edited, fixed code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <string.h>
char answer[10];
int main()
{
    do
    {


    float grade;
    std::cout<< "Please enter your grade \n";
    std::cin>> grade;
    if (grade >=90)
        {
            std::cout<< "You passed with an A \n";
        }
            else if(grade>=80)
            {
                std::cout<< "You passed with a B!";


            }
            else if(grade >=70)
            {
                std::cout<<"You passed with a C! \n";

            }





        else
        {
            std::cout<< "You failed \n";
        }
        std::cout<<"Would you like to start over? \n";

        std::cin>>answer;
    }while(strcmp(answer,"Yes")==0);
}


And I'm proud to say it works. Thanks again, TheMeerkat!
Yea looks good, you should pay more attention to the way you code is indented though. I know it doesn't make any difference to the compiler but it can make it much easier to understand what is happening. The way you have it posted above it seems as if the (grade>=80) and (grade >=70) parts are nested within the (grade >=90) statement, which actually isn't the case.

Whereas this is much more obvious what is going on:
1
2
3
4
5
6
7
8
9
if (grade >=90){
	std::cout<< "You passed with an A \n";
}else if(grade>=80){
	std::cout<< "You passed with a B!";
}else if(grade >=70){
	std::cout<<"You passed with a C! \n";
}else{
	std::cout<< "You failed \n";
}


The other thing is when you ask the user to input something, you should list the options of things that can be input. Often people just use 'y' or 'n' to mean yes and no, but in your code that would work, and it wouldn't be obvious to the user why not.
Wow thanks TheMeerkat. Actually, I thought as well that the else if was actually nested within the if statement before I read your post haha. Ah, now I have a better understanding of the if, else, and else if statements. Oh yeah, thats true, I should give them options. Thanks again, TheMeerKat!
Yea no problem.

Happy coding.
Topic archived. No new replies allowed.