Hi all;
I need help with a program, where its a game where users are to guess a randomly selected number. The user has a max of 1 - 100.
class CScoreboard
{
private:
string players_name;
int round;
int guess_avg;
int total_score;
public:
CScoreboard() {
players_name ="";
round = 0;
guess_avg = 0;
total_score = 0;
}
void input_name();
string get_name()
{
return players_name;
}
int getRound()
{
return round;
}
double cal_avg()
{
return guess_avg;
}
};
class CGame
{
private:
int numb_array[100];
int guess_numb;
int win_numb;
int elct;
int found; int low;
int high;
int middle;
public:
CGame() // default constructor
{
for (int i = 0; i < 100; i++)
{
numb_array[i] = false;
}
guess_numb;
win_numb;
srand(time(NULL));
int elct = 100;
int found = false;
int low = 0;
int high = elct -1;
int middle = 0;
}
void input_numb();
void pick_number();
int search_numb (int guess_numb);
int generate_numb() { return rand()% 100 + 1; }
int get_guess_numb() { return guess_numb; }
int get_win_numb() { return win_numb; }
int display_array()
{
for (int i = 0; i < 100; i++)
{
cout << numb_array[i];
}
return 0;
}
};
void CScoreboard::input_name()
{
cout << "Please enter your name: ";
getline(cin, players_name);
cout << endl << "Welcome to the Main Menu, " << players_name;
}
void CGame::pick_number()
{
win_numb = generate_numb();
numb_array[win_numb] = win_numb;
}
int CGame::search_numb(int guess_numb)
{
while (found == false && low <= high)
{
middle = (low + high) / 2; // determine midpoint
if ( numb_array[middle] == guess_numb )
found = true; // found it
else if ( guess_numb < numb_array[middle])
high = middle - 1; // cut search range in half
else
low = middle + 1; // cut search range in half
}
return found;
}
void CGame::input_numb()
{
int found = -1;
for (int i = 10; i > 0 && found == -1; i--)
{
int temp;
cout << "You have " << i << " guesses" << endl;
cout << "Guess a number: ";
cin >> guess_numb;
temp = search_numb(guess_numb);
if (temp != false)
cout << "The number is found!" << endl;
}
}
char display_menu()
{
char choice;
cout << endl << endl << " MAIN MENU" << endl << endl;
cout << "P. Press 'P' to play" << endl;
cout << "Q. Press 'Q' to Quit" << endl;
cout << "Please make your selection: ";
cin >> choice;
return choice;
}
int main()
{
char choice;
CScoreboard myScore;
CGame arr;
myScore.input_name();
do
{
CGame myGame;
choice = toupper(display_menu()); // call function to display menu
switch (choice)
{
case 'P':
myGame.pick_number();
myGame.input_numb();
break;
case 'Q':
cout << "The correct number is: " << myGame.get_win_numb() << endl;
break;
default:
cout << endl << "Invalid choice -- You have been warned!" << endl;
}
} while (choice != 'X');
cout << "Boy, "<< myScore.get_name() << " it is working!!" << endl;
arr.display_array();
// myGame.pick_number();
// myGame.search_numb();
system("Pause");
return 0;
}
The user is given a maximum of 10 tries to guess each number. He can play as many rounds as he wants, each time with a new number to guess. Begin with a screen describing
the game and asking for the player’s name. - If incorrect, display "Too High" or "Too Low". Adjust the number of guesses left and the current range:
This is what the program suppose to do :
What’s your name? Joe Smoe
[Round 1 -- assume number is 90]
You have 10 guesses
Guess a number between 1 and 100: 70
Too low
You have 9 guesses
etc,
When I run this it does 2 things incorrectly
First, if you choose to "Quit" immediately, it displays a -858993460, and second, after the user enters a number, the system displays "The number is found"
This is what I am trying to fix, and don't know where...
Here are the compiler output
1>------ Build started: Project: GuessGame, Configuration: Debug Win32 ------
1> NameThatNumber.cpp
1>c:\home\programs\NameThatNumber.cpp(67): warning C4244: 'argument' : conversion from 'time_t' to 'unsigned int', possible loss of data
1> Assignments.vcxproj -> C:\home\programs\Debug\program.exe
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
Georgy