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.
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;
}
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
usingnamespace std;
int reviewGUESS(int, int); //definition of reviewGUESS function
{
int NUM_COMP;
if (guess < R_NUM)
{
NUM_COMP = -1;
}
elseif (guess > R_NUM)
{
NUM_COMP = 1;
}
elseif (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
constint 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;
}