Guessing game program

Ok, so I'm in my pRG410 programming class C++ and I've been tasked with writing a program that uses a function to compare the players guessed number with the random number and return a -1 if guess is too low, a 1 if the guess is too high and a 0 if the guess is correct. I can only allow 5 guesses and I have to allow the player to choose to play again. So far the feedback from my instructor says that I can't declare the definition of the function within the _tmain ()function and I needed a break after each case ( oh yeah I had to use a switch to display the "too high" "too lo" and "correct" output. ...keep in mind I am a noob at this. the program crashes immediately and I'm not sure why. I moved the function definition outside of the main area but that did not help. any help on this matter would be appreciated since I also have t create an array to store guesses and display them after the first guess.

#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;

int reviewGUESS(int, int); //definition of reviewGUESS function
{
int NUM_COMP;
if (guess < R_NUM)
{
NUM_COMP = -1;

}

else if (guess > R_NUM)
{
NUM_COMP = 1;

}
else if (guess == R_NUM)
{
NUM_COMP = 0;

}
}

int _tmain(int argc, _TCHAR* argv[])
{


int guess; // sets the variable for the payers guess
char playagain;
int NUM_COMP;
srand(time(NULL)); // allows the program to use the system clock to generate random numbers
double R_NUM = 1 + rand() % 10; //sets the random number range as variable between 1 and 10
int counter = 1; // sets the inital counter for the number of guesses at 1
const int MAX_NUMBER_OF_GUESSES = 5; // sets the maximum number of guess players have. const
//indicates that this number cannot change during the program
cout << " Welcome to the Guessing Game!" << endl; // initial output and instructions for the player next 5 lines
cout << "Enter a number between 1 and 10." << endl;
cout << "After each guess I will tell you if you are too low or too high." << endl;
cout << "You will have 5 guesses" << endl;
cout << "Good Luck!!" << endl;


while (counter <= MAX_NUMBER_OF_GUESSES)
{
cin >> guess; //input line to allow player to type in a number
reviewGUESS(guess, NUM_COMP); // calls reviewGUESS function
}

switch (NUM_COMP)// uses returned value of NUM_COMP to decide which case to display
{
case -1:
cout << "Your guess is too low! Try again!\n";
counter++;// increments the counter
break;

case 1:
cout << "Your guess is too High! Try again!\n";
counter++;// increments the counter
break;

case 0:
cout << "You've correctly guessed my number!\n";
cin >> playagain;
}

if (counter == MAX_NUMBER_OF_GUESSES)
{
cout << "You've used up all your guesses! \n";
cin >> playagain;

}

if (playagain = 'Y')// restarts or ends program based upon choice of player
{
counter = 0;
cin >> guess;
}
else (playagain = 'N');
{
cout << "Thank you for playing!\n";
return 0;
}




return 0;
}

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
82
83
84
85
86
87
88
89
90
91
92
93
94

#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;

int reviewGUESS(int, int); //definition of reviewGUESS function
{
int NUM_COMP;
if (guess < R_NUM)
{
NUM_COMP = -1;

}

else if (guess > R_NUM)
{
NUM_COMP = 1;

}
else if (guess == R_NUM)
{
NUM_COMP = 0;

}
}

int _tmain(int argc, _TCHAR* argv[])
{


int guess; // sets the variable for the payers guess
char playagain;
int NUM_COMP;
srand(time(NULL)); // allows the program to use the system clock to generate random numbers
double R_NUM = 1 + rand() % 10; //sets the random number range as variable between 1 and 10
int counter = 1; // sets the inital counter for the number of guesses at 1
const int MAX_NUMBER_OF_GUESSES = 5; // sets the maximum number of guess players have. const 
//indicates that this number cannot change during the program
cout << " Welcome to the Guessing Game!" << endl; // initial output and instructions for the player next 5 lines
cout << "Enter a number between 1 and 10." << endl;
cout << "After each guess I will tell you if you are too low or too high." << endl;
cout << "You will have 5 guesses" << endl;
cout << "Good Luck!!" << endl;


while (counter <= MAX_NUMBER_OF_GUESSES)
{
cin >> guess; //input line to allow player to type in a number
reviewGUESS(guess, NUM_COMP); // calls reviewGUESS function
}

switch (NUM_COMP)// uses returned value of NUM_COMP to decide which case to display
{
case -1:
cout << "Your guess is too low! Try again!\n";
counter++;// increments the counter
break;

case 1:
cout << "Your guess is too High! Try again!\n";
counter++;// increments the counter
break;

case 0:
cout << "You've correctly guessed my number!\n";
cin >> playagain;
}

if (counter == MAX_NUMBER_OF_GUESSES)
{
cout << "You've used up all your guesses! \n";
cin >> playagain;

}

if (playagain = 'Y')// restarts or ends program based upon choice of player
{
counter = 0;
cin >> guess;
}
else (playagain = 'N');
{
cout << "Thank you for playing!\n";
return 0;
}




return 0;
}
Topic archived. No new replies allowed.