i need help with this program,
i need to give the user 20 chances to guess, and they should be asked if they want to playagain.(by playing again i mean generating a new number with 20 new chances) after playing for how many times i should count the number of wins and losses when they should to exit. (one loss is failing to guess the right number for 20 times.)
oh we need to use functions
this is my code:
******************************************
// ***************************************************
// Guess my number
// ***************************************************
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include <cmath>
#include <time.h>
using namespace std;
void take_a_guess(int);
int main( )
{
int range = 100;
int myNumber;
srand(time(NULL)); //initailize random generator
myNumber = rand()%range ;
take_a_guess (myNumber);
return 0;
}
void take_a_guess( int myNumber)
{
int Right=0, Wrong=0;
int range = 100;
int loopCount, wins = 0, losses = 0, n1, n2, yourNumber, win_message;
string answer;
string message;
loopCount = 0;
int playagain;
bool ans;
do
{
cout << "I have a number between 0-99 in mind." << endl;
cout << "Please type your guess:" << endl;
cin >> yourNumber;
message = myNumber > yourNumber ? "small" : "large";
while(loopCount <= 20 )
{
if (yourNumber == myNumber)
{
win_message = true;
wins++;
n1 = rand() % 10;
switch (n1)
{
case 0 :
cout << "You 're smart. My number was " << myNumber << endl;
break;
case 1:
cout << "You 're lucky. My number was " << myNumber << endl;
case 2:
cout << "WOW, My number is " << myNumber << endl;
break;
case 3:
cout << "Cool! My number is " << myNumber << endl;
break;
case 4:
cout << "Great!! My number is " << myNumber << endl;
break;
case 5:
cout << "You got it! my number was " << myNumber << endl;
break;
case 6:
cout << "Nice guess! my number was " << myNumber << endl;
break;
case 7:
cout << "Nice try! my number was " << myNumber << endl;
break;
case 8:
cout << " I like that! my number was " << myNumber << endl;
break;
case 9:
cout << "You guessesd right!! my number was " << myNumber << endl;
break;
case 10:
cout << "LOL... YOU GOT ME THERE! my number was " << myNumber << endl;
break;
default :
cout << "there is a problem with the random function!" << endl;
break;
}
}
else
{
n2 = rand() % 10;
switch (n2)
{
case 0:
cout << "You typed " << yourNumber << ". Your number is too " << message << ". You do not worry about getting them wrong, try again.\n" << endl;
break;
case 1:
cout << "You typed " << yourNumber << ". Your number is too "
<< message << ".Do not worry about missing .\n" << endl;
break;
case 3:
cout << "You typed " << yourNumber << ". Your number is too "
<< message << cout << "type here please." << endl;
break;
case 4:
cout << "You typed " << yourNumber << ". Your number is too "
<< message << ". try your luck again," << endl;
case 5:
cout << "You typed " << yourNumber << ". Your number is too "
<< message << ". You still have a chance. " << endl;
break;
case 6:
cout << "You typed " << yourNumber << ". Your number is too "
<< message << ". Another try might work,\n" << endl;
break;
case 7:
cout << "You typed " << yourNumber << ". Your number is too "
<< message << ". You still have a chance. " << endl;
break;
case 8:
cout << "You typed " << yourNumber << ". Your number is too "
<< message << ". you missed, come on!. " << endl;
break;
case 9 :
cout << "You typed " << yourNumber << ". Your number is too "
<< message << ". ops! you didnt guess right. " << endl;
break;
case 10:
cout << "You wrote " << yourNumber << ". Your number is too "
<< message << ". It's ok if you miss. " << endl;
break;
default:
cout << "something is wrong with your random function" << endl;
break;
}
}
loopCount++;
}
if( win_message != true)
{
cout << "sorry! you lost." << endl;
losses++;
cout << "Would you like to play again? (y/n)" << endl;
cin >> playagain;
}
}
while (playagain == 'y' || playagain == 'Y');
cout << "you won " << wins << " times, and lost " << losses << " times." << endl;
return ;
}