This is my first attempt at writing a little program in C++, a simple guess-the-number game. You get three tries to guess a number between 1 and 10.
The program worked fine before introducing and rearraging code to ask if you want to play again. I seem to have a problem when responding yes to wanting to play again - it just doesn't seem to go to the "begin game" function. It's probably something silly, but given my newbiness with C++, I'm currently stumped. anyone want to take a stab at where I'm stuck?
/*******************************************
Guess the Number
Version 1.0
*******************************************/
#include <cstdlib>
#include <iostream>
#include <ctime>
usingnamespace std;
int IntroText();
int PlayAgain();
int BeginGame();
int compnum;
int yournum;
int tries;
string yn;
int main(int argc, char *argv[])
{
IntroText();
BeginGame();
}
int IntroText() //
{
cout << "Find and defuse the bomb!\n";
}
int BeginGame()
{
cout << "\n\n";
cout << "Guess a room number between 1 and 10. You have three tries. \n\n";
srand (time(NULL)); //"randomizer"
tries = 0;
compnum = rand()%10 +1;
string yn = "n";
while (tries <3)
{
tries = tries++;
cout << "Attempt number " << tries << ": ";
cin >> yournum;
cout << "\n";
if (compnum == yournum){
cout << "Congratulations!!!" << "\n";
PlayAgain();
}
else {
if ( (compnum > yournum) && (tries <3) ){
cout << "Uh oh. The secret number is higher. \n";
}
else {
if ( (compnum < yournum) && (tries <3) ){
cout << "Uh oh. The secret number is lower. \n";
}
}
}
if ((tries ==3) && (compnum != yournum)){
cout << "KABOOOOOM!!! The secret number was " << compnum << ".\n\n";
PlayAgain();
}
}
}
int PlayAgain()
{
cout << "Would you like to play again (y/n)?";
cin >> yn;
if (yn == "y"){
cout << "\n\n Back to game\n";
BeginGame(); //this is where it doesn't work?
}else{
cout << "\n\n Goodbye!\n";
system("pause");
}
}
if your function dosent return anything like int it can be void
for ex: int introtext could just be void introtext;
the int in front of the function name just tells c++ if the function is going to return anything and if it is what type of data is it going to return
in c++ when you call a function and then that function ends the program will go back to where the function was called
1 2 3
if (compnum == yournum){
cout << "Congratulations!!!" << "\n";
PlayAgain();
so here it will eventually come back to here and execute the rest of the code that is below it
" it just doesn't seem to go to the "begin game" function"
i copy pasted the code and for me it worked perfectly :D
if looking for good video c++ tuts here is couple good youtube video c++ tut series:
1 from guy called antirtfm. hes tuts are really slow and he expliains everything good for beginners hers the link for the first part: http://www.youtube.com/watch?v=tyVhn0FWWB4
I was calling the wrong exe, and I couldn't see the program running "fine" as you did. <blush>
Now that it works, I'd like to ask - how bad is the structure? I could have done this nicely in Basic (for example, with gosub and goto), but as I said, i'm totally new to C++. For example, BeginGame() has most of the code, instead of Main(); I know that's probably a no-no. Any suggestions?
"For example, BeginGame() has most of the code, instead of Main();I know that's probably a no-no"
well sometimes its better to have the code in the main () but most of the time its better to have the code in different functions or classes and then just call them from main(); makes the code tidier ;D
void IntroText();
int PlayAgain();
void BeginGame();
int main(){
int x;
int d=1;
while(d != 0)
{
IntroText();
BeginGame();
x = PlayAgain();
switch(x){
case 2:
d = 0;
break;
default:
d = 1;
}
}
}
int PlayAgain(){
int a;
int g =0;
int f=1;
while(f!=0){
cout << "want to play again?" << endl;
cout "1.yes!"<<endl;
cout "1.no!"<<endl;
cin >> g;
switch(g)
{
case 1:
a = 1;
f=0;
break;
case 2:
a= 2;
f=0;
}
}
return a;
}