How to ask the user if wants to play again?
TananTanTananTanToink (11)
Dec 28, 2012 at 3:08pm UTC
How to apply 'play again y/n' in simple hello world code.
// Repeat Hello world, by
// include the iostream library
#include iostream
//using the standard namespace
using namespace std;
void main()
{
cout << " Hello world " << endl;
cout << " Whatsup " << endl;
system ("pause");
cout << " I heard your dying " << endl;
cout << " world! " << endl;
system ("pause");
}
whitenite1 (751)
Dec 29, 2012 at 4:09am UTC
@TananTanTananTanToink
Here's one way.
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
// Repeat Hello world, by
// include the iostream library
#include <iostream>
#include <windows.h> // For Sleep command, instead od System("pause");
//using the standard namespace
using namespace std;
int main()
{
char yn='y' ;
do
{
cout << " Hello world " << endl;
cout << " Whatsup " << endl;
Sleep(1500);
cout << " I heard your dying " << endl;
cout << " world! " << endl;
do
{
cout << "Show this text again?? : Enter a 'y' or 'n' " << endl;
cin >> yn;
if (yn!='y' && yn!='Y' &&yn!='n' &&yn!='N' )
cout << endl << "I only respond to the letters 'y' or 'n'" << endl << "Try again!!" << endl << endl;
}while (yn!='y' && yn!='Y' && yn!='n' && yn!='N' );
}while (yn=='y' || yn=='Y' );
cout << "Okay, I'm outta here...." << endl << endl;
}
TheIdeasMan (1752)
Dec 29, 2012 at 4:36am UTC
A rather different way - not the full code - just to give you an idea:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <locale> //for the toupper function
bool Quit = false ;
char Answer;
while (!Quit) {
//your code here
std::cout << "Would you like to quit Y / N" << std::endl;
std::cin >> Answer;
Answer = std::toupper(Answer);
if (Answer == 'Y' ) {
Quit = true ;
}
}
I personally really dislike things like this:
if (yn!='y' && yn!='Y' &&yn!='n' &&yn!='N' )
And nested do loops with repeated tests. This results in 12 LOC just to decide whether to repeat or not.
HTH
Last edited on Dec 29, 2012 at 4:37am UTC
buffbill (415)
Dec 29, 2012 at 5:27am UTC
iostream should be in angle brackets like <iostream>
void main() should be int main()
system("pause") is not recommended to keep the screen open. cin.get() is an improvement for a variety of reasons that you will become familiar with in time, but its not the only alternative.
Imadatobanisa (647)
Dec 29, 2012 at 3:05pm UTC
Another choice :
1 2 3 4 5 6 7 8
while (bQuit == false )
{
//code....
cout << "Would you like to quit (Press Y)" ;
cin >> choice;
if (choice == 'y' || choice == 'Y' )
bQuit = true ;
}
TheIdeasMan (1752)
Dec 30, 2012 at 6:32am UTC
@JM
Not another choice, just a poorer version of what I had.
Suggs (1)
Dec 30, 2012 at 11:39am UTC
Or
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
#include <iostream>
#include <string>
using namespace std;
void main()
{
rerun:
cout << " Hello world " << endl;
cout << " Whatsup " << endl;
system ("pause" );
cout << " I heard you're dying " << endl;
cout << " world! " << endl;
cout << "Would you like to play again? (Y/N)" ;
string yorn;
cin >> yorn;
if (yorn == "Y" || yorn == "y" )
goto rerun;
else
return ;
}
TheIdeasMan (1752)
Dec 30, 2012 at 7:14pm UTC
@Suggs
Couple of problems with your code:
line 4: using namespace std should be avoided
line 6: should be int main()
line 8: see below about goto
line 23: return 0; //return an int
line 19: use toupper function to avoid testing the variable twice
line 20: goto is really really bad, always use loops or functions
Topic archived. No new replies allowed.