### How to make Try Again in my Atmosphere program?

I am a beginner C++ programmer, I think I manage to create this program almost 97% complete! But my problem is how make Try Again choice? When I try to run it, I press Y, but instead this program come to end, and I want to make when you choose N, the program will quit. Am I missing something? And, can I make this program by using void?

Sorry for bad english
PS : Any correction and tips are appreciate

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182`` `````` // This simple program will let you know which Atmosphere type #include using namespace std; int main() { int choice, km; int Y,N; menu: cout << "\t Welcome guest! \n"; cout << "\t This program will let you know about atmosphere layers \n" << endl; cout << "Enter a number in Km : "; cin >> km; while (km > 0) if (km <= 12) { cout << "This is Troposphere layer! \n"; cout << "Try Again? "; cin >> choice; if (choice == Y) goto menu; else return 0; } else if (km <= 50) { cout << "This is Stratosphere layer! \n"; cout << "Try Again? "; cin >> choice; if (choice == Y) goto menu; else return 0; } else if (km <= 80) { cout << "This is Mesosphere layer! \n"; cout << "Try Again? "; cin >> choice; if (choice == Y) goto menu; else return 0; } else if (km <= 400) { cout << "This is Termosphere layer! \n"; cout << "Try Again? "; cin >> choice; if (choice == Y) goto menu; else return 0; } else if (km > 401) { cout << "This is Exosphere layer! \n"; cout << "Try Again? "; cin >> choice; if (choice == Y) goto menu; else return 0; } } ``````
When comparing against the users input on the choice on whether to continue, you need to compare that to `'y'` or `'Y'` not `int Y; ` - this is a number.

Also, don't use gotos, use a while loop.

 ``1234567891011`` ``````char userChoice = 'y'; // default to continue while(userChoice == 'y') { // your code starting at line 12, minus all the choice stuff // ... cout << "Try again? "; cin >> userChoice; } // end while loop which will check the user choice ``````

Also what are you doing with this while (km > 0) loop on 18?
Still, it didn' work. When I press Y, the program still end itself same as I press N.
I went ahead and fixed up the things I noticed that could be improved, and got a working "Try again?" input.

Things I did:

- Made the 'Try again?' at the end of the if-else's to eliminate redundance.
- Removed unused variables.

Here ya go!

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263`` ``````#include using namespace std; int main() { int nKM; char chAgain; bool bAgain = 1; cout << "\t Welcome guest! \n"; cout << "\t This program will let you know about atmosphere layers! \n" << endl; while(bAgain == 1) { cout << "Enter a number in KM: "; cin >> nKM; if (nKM <= 12) { cout << "This is Troposphere layer! \n"; } else if (nKM <= 50) { cout << "This is Stratosphere layer! \n"; } else if (nKM <= 80) { cout << "This is Mesosphere layer! \n"; } else if (nKM <= 400) { cout << "This is Termosphere layer! \n"; } else if (nKM > 401) { cout << "This is Exosphere layer! \n"; } cout << "Try again? (Y/N): "; cin >> chAgain; switch(chAgain) { case 'Y': case 'y': continue; return; case 'N': case 'n': bAgain = 0; break; } cout << endl; return 0; }``````

Sorry if anything here is silly or doesn't make sense, it was 6AM when I made this.

Let me know if you have any other questions!
Last edited on
@TrulyRazor

 ``123`` ``````bool bAgain = true; while(bAgain) {``````

I wouldn't have used a switch for a yes / no answer. Make use of the toupper function, then have an if statement that compares with 'Y'.

It is a good idea to always have an else clause, to catch invalid input - in this case a negative value say.

The code for the yes no answer should be separate from the other, at the moment one has to enter a value above 401 before the question is asked.

Hope all goes well.
Topic archived. No new replies allowed.