while loop

How can I add a while loop to this code, so it can continue then adding some code to make it stop when I command it to?

#include <iostream>

using namespace std;
int main()
{
int a,b;
cout<<"Enter first number: ";
cin >> a;
cout<<endl<<"Enter second number: ";
cin >> b;

if(a>b)
cout<<"The first number is greater than the second number"<<endl;
else if (a<b)
cout<<"The first number is not greater than the second number"<<endl;
else if (a==b)
cout<<"Both numbers are equal"<<endl;


system("pause");
return 0;
}
Last edited on
You can do it in 2 (very similar) ways:

(Warning: Just examples, you can do it in any way you want using one of these ways)

1
2
3
4
5
6
7
8
9
10
11
12
13
char Answer;
while(Answer == 'y' || Answer == 'Y'){ //If you enter yes, the condition is false and stops

    /*Interesting code here*/

    cout << "\n Do you want to exit? (Y/N)\n";
    cin >> Answer;
    if(Answer != 'y' && Answer != 'Y' && Answer != 'n' && Answer != 'N'){
        cout << "\n Invalid answer.\n";
        cin.get();
    }

}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
while(true){ //Runs infinitely

    /*Interesting code here*/

    cout << "\n Do you want to exit? (Y/N)\n";
    char Answer;
    cin >> Answer;
    if(Answer=='y' || Answer=='Y'){
        break; //Breaks the loop
    }
    if(Answer != 'y' && Answer != 'Y' && Answer != 'n' && Answer != 'N'){
        cout << "\n Invalid answer.\n";
        cin.get();
    }

}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
bool Exit = false;
while(!Exit){ //Needs the condition to be true

    /*Interesting code here*/

    cout << "\n Do you want to exit? (Y/N)\n";
    char Answer;
    cin >> Answer;
    if(Answer=='y' || Answer=='Y'){
        Exit = true; //Condition is now false
    }
    else if(Answer=='n' || Answer=='N'){
        Exit = false; //Does nothing anyways, but still...
    }
    else{
        cout << "\n Invalid answer.\n";
        cin.get();
    }

}

That seems a bit much when you could just do something like this.

1
2
3
4
5
6
7
8
char Answer;
while(Answer == 'y' || Answer == 'Y') { //Needs the condition to be true

    /*Interesting code here*/

    cout << "\n Do you want to exit? (Y/N)\n";
    cin >> Answer;
}
Noticed what i did was too much, I changed it.

One last thing: When using the break statement to stop a loop, it can't be inside a case label (in a switch), because it will just break the switch. In this situations you need to instead use if/elses, or use conditions for the loop.
Last edited on
Thank you so much for your help. I've input the info as stated and I'm still getting errors. Please tell me where I made my mistake.

#include <iostream>

char Answer;
while (Answer == 'y' || Answer == 'Y') {

using namespace std;
int main()

{
int a,b;
cout<<"Enter first number: ";
cin >> a;
cout<<endl<<"Enter second number: ";
cin >> b;

if(a>b)
cout<<"The first number is greater than the second number"<<endl;
else if (a<b)
cout<<"The first number is not greater than the second number"<<endl;
else if (a==b)
cout<<"Both numbers are equal";

cout << "\n Do you want to exit? (Y/N)\n";
cin >> Answer;
}

system("pause");
return 0;
}

You can't have a while loop outside of the main function.
EDIT: Well, you actually can have one outside of the main function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>

using namespace std;
int main() {
   char Answer = 'y';
   while (Answer == 'y' || Answer == 'Y') {
      int a,b;
      cout<<"Enter first number: ";
      cin >> a;
      cout<<endl<<"Enter second number: ";
      cin >> b;

      if(a>b)
         cout<<"The first number is greater than the second number"<<endl;
      else if (a<b)
         cout<<"The first number is not greater than the second number"<<endl;
      else if (a==b)
         cout<<"Both numbers are equal";

      cout << "\n Do you want to go again? (Y/N)\n";
      cin >> Answer;
   }
   return 0;
}

You'll also have to assign the value 'y' or 'Y' to Answer.
Last edited on
As you have it, the question should be "Do you want to go again?"
Wow I feel like an idiot!

Thank you so much! That worked. Where is the thank meter? So I can sure to give credit where deserved.
If I helped, I'm glad I did.

Good luck in C++! (I wish someone would tell me that when I started :c)
Last edited on
Topic archived. No new replies allowed.