While loop with if else

Feb 10, 2021 at 1:26am
Hi, so I believe I did my coding right so far? I could be wrong, I'm completely new to this. Assignment was to enter number from 0 to 501. 0 and 501 out of range. If anything out of range to display OUT OF RANGE. And when entered a number, print out even numbers from 0 to number input. THEN, ask if want to input another question if Yes, loop back. If no, then end with a goodbye. I got to the no part but now how do I do yes to loop back to ask for a number again?

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
 
#include <iostream>
using namespace std;

int main()
{
   
    //Ask the user for a number between 0 and 501
    int number; 
    char choice;
    int count = 1;
    while (count < 500)
    {
        cout << "Enter a number between 0 and 501: ";
        cin >> number;
        if (number < 1 || number > 500)
        {
            cout << "\nOUT OF RANGE!"; "\n";
        }

        for (int i = 0; i <= number; i += 2)
            cout << i << ' ';
        cout << "\n";

        // Ask if theyd like to enter another number
        cout << "Would you like to enter another number? (Yes/No)" << endl;
        cin >> choice;
        if (choice == 'No' || choice == 'No');
        {cout << "Goodbye!"; }
        break;
        
    
    }
    return 0;
Last edited on Feb 10, 2021 at 1:26am
Feb 10, 2021 at 2:13am
ok, some issues.
"No" -- strings in c++ have double quotes. single quotes are for characters. It compiles but it may not be doing what you think, be careful with this.
why do you ask the same thing twice, if its no or no? (line 28). you only need to check once.
the break is incorrect. you break ALL THE TIME. I believe you want to break only if they said NO. The break should be INSIDE the {} with the if no statement, then!

you never change count. so why 500? get rid of count, you don't need it (its not wrong, its just cluttery):
a forever loop:
while(true)
{
if(something) break; //the only way out
}

fixing the break will fix the original question you had. Also be sure to REMOVE THE ; on the IF STATEMENT for NO, this is incorrect. I see that choice is a char, so instead of "No" you do want single quotes, 'N' perhaps? Either make it a string and want "No" or keep it a char and want 'N' but fix this one way or the other.
but, you should not perhaps do the work (print the even values?) if they entered 10042?
as it stands, bad values will still print the evens. Is that what you wanted?

anyway, my quick edits to repair it:
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
 
#include <iostream>
using namespace std;

int main()
{
   
    //Ask the user for a number between 0 and 501
    int number; 
    char choice;
    int count = 1;
    while (count < 500)
    {
        cout << "Enter a number between 0 and 501: ";
        cin >> number;
        if (number < 1 || number > 500)
        {
            cout << "\nOUT OF RANGE!"; "\n";
        }
        //consider: 
         else {


        for (int i = 0; i <= number; i += 2)
            cout << i << ' ';
        cout << "\n";
        } //end of else idea

        // Ask if theyd like to enter another number
        cout << "Would you like to enter another number? (Yes/No)" << endl;
        cin >> choice;
        if (choice == 'N')
        {cout << "Goodbye!"; break;}
        
                
    
    }
    return 0;}
Last edited on Feb 10, 2021 at 2:22am
Feb 10, 2021 at 2:26am
Oh okay, thank you! I'll keep that in mind. It's my second assignment of the term so I'm still learning. Its definitely complicated. So I changed it to yours, but what about when the user inputs Yes? it still says "Goodbye!" I'm suppose to have it where if its "Yes" it loops back to the beginning where it asks for a number.
Feb 10, 2021 at 2:30am
no, it should loop back if they type anything other than a N for my version.
it basically says this in the logic: if it is N, say goodbye and stop. If not, it will loop again by default.

it may keep letters buffered up in the input. I HIGHLY ADVISE you to change it to read a full string instead of ONE letter for the yes/no thing instead. Otherwise, see if it works for you when you only type Y or N to proceed. Typing YES or NO when it wants a single letter could cause this version to go a little nuts.

1
2
3
4
5
6
7
8
9
10

Enter a number between 0 and 501: 10
0 2 4 6 8 10 
Would you like to enter another number? (Yes/No)
y
Enter a number between 0 and 501: 42
0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 
Would you like to enter another number? (Yes/No)
N
Goodbye! 


c++ is case aware. n does not equal N.
Last edited on Feb 10, 2021 at 2:35am
Feb 10, 2021 at 2:48am
You are right!! Thank you so much! I don't know if this is what was messing it up but I had a ; after if (choice == 'N'). So now when I type N it says goodbye! and if i type Y it asks again for a number. Thank you!!! Now, my last question, when I pick lets say 501 it gives me even numbers for all although it is out of range. Is there any way to show only out of range for the numbers that are out of range? I dont think it matters in my assignment i am just curious.
Feb 10, 2021 at 2:48am
Hello miguelminaj,

I had hoped that you might have caught on to a few things a little better.

This makes your program easier to read and you might have seen some of the errors earlier.

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>

using namespace std;

int main()
{

    //Ask the user for a number between 0 and 501
    int number;
    char choice;
    int count = 1;

    while (count < 500)
    {
        cout << "Enter a number between 0 and 501: ";
        cin >> number;

        if (number < 1 || number > 500)
        {
            cout << "\nOUT OF RANGE!"; "\n";
        }

        for (int i = 0; i <= number; i += 2)
            cout << i << ' ';

        cout << "\n";

        // Ask if theyd like to enter another number
        cout << "Would you like to enter another number? (Yes/No)" << endl;
        cin >> choice;

        if (choice == 'No' || choice == 'No');

        { 
            cout << "Goodbye!"; 
        }

        break;
    }

    return 0;

Like "main" has no closing }. This could be just a highlight and paste malfunction.

Your while loop is an endless loop because "count" never changes its value, so the condition is always true. No to worry the while loop will only loop 1 time because line 38 will always break out of the loop.

On line 18 should the if statement be true you print an error message then continue with the program as is nothing is wrong. Needs fixed.

I have not tested the for loop, but I suspect that it will loop 1 more time than you are expecting. It usually does when you use "<=" in the condition.

Line 33 is blank to show that lines 34 - 36 are not part of the if statement as they should be.

Since the "break" statement is the last line of the while loop it will always be executed.

Andy
Feb 10, 2021 at 3:18am
yes, I added an 'else' statment in the code I posted to avoid printing the numbers when invalid values were given. (line 21 of my code version)
and yes, the ; on the if (and on loops as well) is a common mistake that was part of your issues (I said that, and fixed it).
Last edited on Feb 10, 2021 at 3:19am
Feb 10, 2021 at 10:18am
Based upon code from previous post here http://www.cplusplus.com/forum/beginner/275999/

Consider:

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
#include <iostream>
#include <limits>
#include <string>
using namespace std;

int main()
{
	while (true) {
		int number {};

		while ((cout << "Enter a number between 0 and 501: ") && (!(cin >> number) || number < 1 || number > 500)) {
			if (!cin) {
				cout << "Not a number\n";
				cin.clear();
				cin.ignore(numeric_limits<streamsize>::max(), '\n');
			} else
				cout << "Not in range\n";
		}

		for (int i = 0; i <= number; i += 2)
			cout << i << ' ';

		cout << '\n';

		std::string choice;

		cout << "Would you like to enter another number? (Yes/No): ";
		cin >> choice;

		if (choice == "No" || choice == "no" || choice == "NO" || choice == "n" || choice == "N") {
			cout << "Goodbye!\n";
			break;
		}
	}
}


The issue with asking for a yes/no answer is that there are many possible 'correct' permutations - yes, YES, Yes, N, No, NO, no etc. Do you treat anything other than a variation of 'no' as yes - or do you check for all variations of yes as well (such as converting to say lowercase and checking)???
Topic archived. No new replies allowed.