Hi guys I'm new to C++ and I'm taking a class for this but I'm sort of stuck on this assignment. I keep getting errors for uninitialized local for my computer_guess and warning for "Warning 1 warning C4244: 'initializing' : conversion from 'time_t' to 'unsigned int', possible loss of data". I don't know how to fix this and was hoping one of you guys could help me out.
#include <iostream>
#include <ctime> //For the time function
#include <cstdlib> //For rand and srand
usingnamespace std;
int num;
char B;
void ComputervsPlayer();
void PlayervsComputer();
int main()
{
//Get the system time
unsigned seed = time(0);
//Seed the random number generator
srand(seed);
//Computer Guess Placement
num = rand() % 100 + 1;
char a;
B = 'Y';
while (B == 'Y' || B == 'y') //Instead of do/while can use while statement for Yes/No answer
{
cout << "Do you want to play a game? ";
cin >> a;
if (a == 'Y' || a == 'y')
ComputervsPlayer(); //Takes it to computer vs player function
elseif (a == 'N' || a == 'n')
{
PlayervsComputer(); //Takes it to Computer vs Player function
}
else
cout << "Error! Please choose a valid answer" << endl;
}
return 0;
}
void PlayervsComputer()
{
//Variables involved
int Guess_Num1;
int Computer_Guess;
int NumofGuesses = 10;
//What the user is seeing
do
{
cout << "Please guess what I'm thinking between the ranges\n" << endl;
cout << "of 1-100: " << endl;
cin >> Guess_Num1;
NumofGuesses--; //This helped keep track of the guess and decreased the number of guess.
//The number of Guesses was set to 2 at the time. After 2 rounds it went straight to 'if statement' for number of Guess.
//Validate the input
if (Guess_Num1 > 100 || Guess_Num1 < 1)
{
cout << "Yeah! You got the right answer!" << endl;
}
elseif (Guess_Num1 > Computer_Guess)
{
cout << "That number is too high.\n";
cout << "Please enter a lower number: ";
}
elseif (Guess_Num1 < Computer_Guess)
{
cout << "That number is too low.\n";
cout << "Please enter a higher number: ";
}
else
cout << "Error! That is outside the range.\n";
cout << "Please enter within the range of 1-100.\n" << endl;
//Trying to get count the number of guesses. If number of guesses is gone, then this statement runs
//Letting the user know what the answer was
if (NumofGuesses <= 0)
{
cout << "Ran out of guess. Number was " << Computer_Guess << endl;
}
}
while (Computer_Guess != Guess_Num1);
}
void ComputervsPlayer() //Computer guessing number
{
int Guess = 0,
select = 0,
Max_Num = 100,
Min_Num = 1;
int i = 1;
int NumofGuesses = 10;
do
//for (int i = 1; i <= 100; i++)
{
if (select == 1) Max_Num = Guess - 1; //Setting Max guessing number limit Too High number gussing
elseif (select == 2) Min_Num = Guess + 1; // Setting Low Guessing number limit Too low
cout << "I'm thinking of a number between " << Min_Num << " and " << Max_Num << "... ";
if (Max_Num == Min_Num) Guess = Max_Num;
else Guess = rand() % (Max_Num - Min_Num) + Min_Num; //Create random number
cout << "Is the number " << Guess << endl;
cout << "Is it too high or to low or right on the mark?\n";
cout << "Select: 1 for to low, 2 for to high, and 3 for right on the mark.";
cin >> select;
NumofGuesses--; //Counting number of guesses
//What the user puts in and sees
if (select == 1)
cout << "I'll go higher this time!" << endl;
elseif (select == 2)
cout << "I'll go lower this time!" << endl;
elseif (select == 3) //Hits the mark
{
cout << "Yeah! I got it right on the mark!" << endl;
break; // Ends the loop
}
else
{
cout << "I don't understand?" << endl;
}
cout << "Guess left: " << NumofGuesses << endl;
/*if (i == 10)
cout << "You win this round!" << endl;*/
if (NumofGuesses <= 0) //What happens when the computer runs out of guess
{
cout << setfill('-') << setw(20) << "-" << endl;
cout << "Ran out of guess. Number was " << num << endl;
break;
}
} while (Guess <= 100 || Guess >= 1);
playAgain();
}
I was able to figure out the problem, I had to set my computer guess into a global variable for the random number to take hold into the other functions. No I just have a problem of trying to close the program when I say no to "Do you wan to play again?".