> int noofgamesplayed; noofgamesplayed++;
Is using it without it being defined. cout << "You played " << noofgamesplayed << " games";
Is also using it without it being defined. int noofgamesplayed = 0;
Now we're cooking!.
Salem
Posting Sage
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
where are you facing the problem...???
tracethepath
Junior Poster in Training
54 posts since Jul 2007
Reputation Points: 8
Solved Threads: 4
Think about using some functions.
int main ( ) {
playSomeGames();
return 0;
}
void playSomeGames( ) {
int numGamesPlayed = 0;
do {
playAGame();
numGamesPlayed++;
// prompt for "play again" and read answer
} while ( again );
cout << "You played " << numGamesPlayed << " games";
}
void playOneGame ( ) {
int numGuesses = 0;
// generate a target value
do {
// prompt for guess etc
} while ( numGuesses < 10 );
cout << "You took " << numGuesses << " to guess";
}
Each function is short (<20 lines say), and performs a very specific task. Indeed, main could just call playOneGame() to begin with, so you can test that function, then switching over to playSomeGames() is a one-line change in main.
Salem
Posting Sage
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
don't think he's reached functions yet. Your errors are syntax. Look at something like this:
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <string>
using namespace std;
int main()
{
srand ( static_cast<unsigned int> ( time ( 0 ) ) );
int randNum = rand() % 100 + 1;
int num = 0;
int GuessLow=0;
int GuessHigh = 0;
int guess=0;
string uname;
cout<<"Please enter your name: ";
getline ( cin, uname );
cout<< uname <<'\n';
cout<<"Welcome to the Random Number Guessing Game!!\n";
{
do
{
// Loops until user finds the number
cout <<uname<<" "<<"please enter a number between (#1 - 100): ";
cin >> num; // User's input of guessed number
guess++;
if (num < randNum) // If num is < than the random number, inform player to try again.
cout <<uname<<" "<<"I'm sorry-your number was too low-please try again!";GuessLow++;
else
if (num > randNum) // If num is > than the random number, inform player to try again.
cout<<uname<<" "<<"I'm sorry-your number was too high-please try again!";GuessHigh++;
else
break;
} while (num != randNum);
cout<<uname<<endl;
cout<<" "<< "Excellent Job!! You guessed correctly!!"<< endl;//Informs if the player is successfull
cout << "Guessed too high: " << GuessHigh;//Display the user's score at the end of the program
cout << "Guessed too low: " << GuessLow;//Display the user's score at the end of the program
cout<< "Total number of guesses: "<<guess;//Display the # of guesses by user
}
return 0;
}
This ought to work...there may be a few errors in it...you ought to modify it o fit the requirements of your assignments. Hope this helps.
zandiago
Nearly a Posting Maven
2,480 posts since Jun 2007
Reputation Points: 129
Solved Threads: 26
Hold up...........why are all these people double posting.....or is there a bug on my PC?...thought i just answered the question.....
zandiago
Nearly a Posting Maven
2,480 posts since Jun 2007
Reputation Points: 129
Solved Threads: 26