'Random Number Guesser' game (w/ functions)

I'm having trouble creating this. I've looked at a few tutorials and other 'help' posts online but they all have the code in the main method and I was under the impression you should avoid doing this so I'm trying to implement functions to separate certain blocks of code.


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

#include <cstdlib>
#include <iostream>
#include<ctime>

using namespace std;


int random()
{
 
 int randomNumber = rand()%1000+1;   
 return (randomNumber);
 
}

int start(randomNumber) *#Error1:'randomNumber' was not declared in this scope#*

*#Error2: expected ',' or ';' before '{' token#* {
int guess;
 
 cout << "Welcome to the number guessing game!" << endl;
 cout << "Please input a number between 1 and 1000" << endl;
 cin >> guess;
 
 if (guess == randomNumber) {
           cout << "You have guessed the number!" << endl;
           } else {
                  if (guess > randomNumber) {
                            cout << "Your guess of " << guess << " is too high" << endl;
                            counter++;
                            start();
                            } else {
                                   cout << "Your guess of " << guess << " is too low." << endl;
                                   counter++;
                                   }
                                      
    
                  }
}

int main()
{
    random();
    start(); *#Error 3: 'start' cannot be used as a function#*
    
}
Last edited on
Change your declaration part of start() to void start(int randomNumber), and in main(), get rid of the other two function calls and put start(random());.
randomNumber is a local variable in int random(). It gets destroyed when the end of the function is reached.

You have two options: (#2 is better)
1. Make randomNumber global.
2. Pass randomNumber into start correctly.


Solution 1:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int randomNumber; // Declared outside of functions
int random()
{
 randomNumber = rand()%1000+1;    //It's declared globally, no need to do it again
 return (randomNumber);
}
void start() //no input required. Anyone can use randomNumber now
{
    //your function here
}
int main()
{
    //your function here
}


Solution 2:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int random()
{
 int randomNumber = rand()%1000+1;   
 return (randomNumber);
}
void start(int randomNumber) //randomNumber didn't exist anymore, so create a new one. 
//This is not automatically set to the one in the other function, it is completely seperate.
{
    //Your function here
}
int main()
{
    int MyNumber = random();  //random() returns the random number.  
    start(MyNumber); //Feed that result into the argument of start();
}
Last edited on
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
// Brent Sonnen
// 01-23-2012
// Number guessing game

#include <cstdlib>
#include <iostream>
#include <ctime>

using namespace std;


int random()
{

 int randomNumber = rand()%1000+1;
 return (randomNumber);

}

void start(int randomNumber) { 


int guess;
int counter=0;

 cout << "Welcome to the number guessing game!" << endl;

 while(true){
 cout << "Please input a number between 1 and 1000" << endl;
 cin >> guess;


 if (guess == randomNumber) {
           cout << "You have guessed the number!" << endl;
           break;
           } else {
                  if (guess > randomNumber) {
                            cout << "Your guess of " << guess << " is too high" << endl;
                            counter++;

                            } else {
                                   cout << "Your guess of " << guess << " is too low." << endl;
                                   counter++;
                                   }


                  }
}

}

int main()
{
    start(random());

}


1. first, since start wont return any value it must be a void method... void start()
2. you must declare the variables to pass as parameters in a function... void start ( int randomNumber )
3. about the expected , error, that is because you missed a { next to the function declaration: void start (int randomNumber){
4. there was an error about counter variable not existing... i created it and initialized it on 0 int counter=0;
5. Go to the main function... there you can't start the function because it won't return any value, that's why you need it to be void... and you need to pass the function the number the computer randomly calculated... you can either, do it by integrating the random function as a parameter in the start function (as i did), or you can do this.

int number= random();
start (number);

and also, inside your guessing method, after the user types his guess, whether he guesses the number or not, the program will end, because you don't loop the process... I added a while(true) to the whole asking process so it will run without ending unless you guessed the number, when the break; i added will end the cycle...

if you are trying to get the user to guess the number with limited attempts, you can add also an if(counter==attempts){
cout<<"\n Your attempts are over";
break;
}


I'd recommed reading how functions work, since int,double,etc ones need you to catch their return in a variable, and voids do not...

Sorry for my english, it sucks lol...
Last edited on
Thanks for the help so far. After reading through the 2 pages about 'functions' I still have some questions.


1) Danoc, when you say "5. Go to the main function... there you can't start the function because it won't return any value, that's why you need it to be void". What do you mean by "Return a value"? If you return a value does that mean one function gets a piece of data (through calculation, user input, etc) and will be used by another function?

2) My random number isn't so random. It's always 42.

3) I'm not sure how to integrate the exit() function. Part of this may relate back to my question in #1.

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
#include <cstdlib>
#include <iostream>
#include<ctime>

using namespace std;


int random()
{
 
 int randomNumber = rand()%1000+1;   
 return (randomNumber);
 
}

void start(int randomNumber) {
     
int guess;
int counter = 0;
 
 cout << "Welcome to the number guessing game!" << endl;
 
  while(true){
 cout << "Please input a number between 1 and 1000." << endl;
 cin >> guess;


 if (guess == randomNumber) {
           cout << "You have guessed the number!" << endl;
           //exit();     ****I'm not too sure how to integrate this feature
           } else {
                  if (guess > randomNumber) {
                            cout << " " << endl;
                            cout << "Your guess of " << guess << " is too high" << endl;
                            counter++;
                            cout << "You have guessed " << counter << " times." << endl;
                            cout << " " << endl;

                            } else {
                                   cout << " " << endl;
                                   cout << "Your guess of " << guess << " is too low." << endl;
                                   counter++;
                                   cout << "You have guessed " << counter << " times." << endl;
                                   cout << " " << endl;
                                   }


                  }
                  
}

}

void exit(){
       
       string close;
       
       cout << "Please type 'exit' to close the program or type 'retry' to restart the program." << endl;
                  cin >> close;
                  
                  if (close == "Exit" && close == "Exit")
                  {
                  exit(0);
                  } else {
                         start(random());
                         } 
       
       }
       
int main()
{
    start(random());
    
}
1) a function that isn't of type void will return a value. Example:
1
2
3
4
int add_a_and_b( int a, int b)
{
  return a+b;
}


This is of type int as that is what is in front of your function name. It also accepts two parameters a, and b. It returns the value of the sum of a and b. We use it like this:
1
2
int x = 1, y = 2;
int Sum = add_a_and_b(x, y);

Sum will equal 3.

Now if your function doesn't need to return a value then you make it type void. An example would be like this:
1
2
3
4
void printChar(char a)
{
  cout << a;
}


This function will take a character and put it on the console. Nothing needs to be returned here. A return statement will exit the function early if desired, but it isn't required.

In your code you had a function which was supposed to return an integer, but you didn't do anything with that integer. It should have set a variable or something.

2) Use srand(time(NULL)) at the start of your main. This will seed the random number generator.

3) If you want exit() to be called, you just have to call it somewhere.
1
2
3
4
5
6
int main()
{
  int myRand = random();
  start(myRand);
  exit();
}


This will call the exit() function which contains the start() function. Note that I added an integer here between random() and start(). This is to make the concept that random() is returning a value and that start is accepting that value easier for you to visualize.

Edit: If you wanted the program to repeat continuously, either call the exit() function at the end of start() to make this recursive or use a while loop.
Last edited on
Thank you stew.

Another question I have is why isn't function 'add_a_and_b()' assigning the value to a variable. I expected to see something like

1
2
3
4
5
6
int add_a_and_b( int a, int b)
{
int sum;
sum = a+b;
  return a+b;
}



In regards to exit(). I want to give the user the option to exit or restart. So i'm prompting them for input. If they say "exit" or "Exit" the program will close, else it will do "start(random());". Is there a way to have it call "main" so "srand(time(NULL))" re-seeds the random number generator?
Last edited on
Topic archived. No new replies allowed.