Problem with functions - first attempt at C++

Hello -

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?

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*******************************************
Guess the Number
Version 1.0
*******************************************/
#include <cstdlib>
#include <iostream>
#include <ctime> 
using namespace 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");
         }
    }
well it seems to work fine....apart from the fact that you designed the program in the most complex way and your program designing is terrible!
in function
1
2
3
4
int IntroText() //
{
     cout << "Find and defuse the bomb!\n";
}

no return statement, but it need to return int.

not tries = tries++; ,but tries++; line 44

no return for int BeginGame() to.


no return for int int PlayAgain() to.

and program works for me.
1
2
3
while (tries <3)
           {
            tries = tries++;

could only be

tries++;(has the same effect)

cout << "\n"; could be done with cout << endl;

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

2 after wathing antirtfm next guy is called thenewboston
you can skip the stuff that antirtfm explained link to the first part:
http://www.youtube.com/watch?v=tvC1WCdV1XU&list=PLAE85DE8440AA6B83&index=1&feature=plpp_
Last edited on
And you have redefinition of string "yn". First defined in global scope and after in line 41.

P.S. Sorry for my English, I teach it)
Thanks to all for your replies!

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?

And thanks for the links; I'll look into those.
"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

i would have probally made something like this

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
42
43
44
45
46
47
48
49
50
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;     
    }

Last edited on
Topic archived. No new replies allowed.