1,105,232 Community Members

Simple number guessing game

Member Avatar
lupacarjie
Newbie Poster
13 posts since Aug 2011
Reputation Points: 1 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hello, I created a simple number guessing game which thinks of a secret number from 1 to 100. It tells the user if his guess is high or low and finally stops after the secret number is equal to the user's input, it also notes down the number of tries. It then asks if the user wants to try again, but whenever it runs to play again, the number of tries add up to the previous result. What am I missing? Also, the setw() function doesn't seem to work. Thanks!

#include <cstdlib> //directive for rand() function
#include <time.h>  // directive for random pick of number
#include <iomanip>
#include <iostream>
 
using namespace std;
 
int main(){

      srand(time(0));  //changes the secret number
      int number;
      number = rand() % 100 + 1;	//range of secret number 1-100
      int guess;
	  int tries = 1;
	  int play = 1;

	  pick:
	  
	  cout << setw(5) << "\nWelcome to the Guess the number game! " ;
	  cout << setw(5) << "\nI'm thinking of a secret number from 1 to 100 " ;
	  cout << setw(5) << "\nGuess it!\n\n" ;
     
	  do {	//loops until guess is equal to secret number
            cout << "\nEnter your estimate: ";
            cin >> guess;

            if (guess < number){
				cout << "Higher!" << endl;
				tries = tries + 1;}

            else if (guess > number){
                  cout << "Lower!" << endl;
				  tries = tries + 1;}
            else
                  cout << "Your guess is right!" << endl;
				  

      } while (guess != number);
	  cout << "\n\nYou tried "<<tries<<" times " ;

	   char g;
		cout << "\nTry Again? (y for yes or n for no):  ";
		 cin >> g;
			 if(g !='n'){
				 srand(time(0));
				 play = play + 1;
				 system("cls");
				 goto pick;}
			 else {
				 cout << "\nNumber of game plays is "<<play<<"\n\n" ;}

      system("PAUSE");
      return 0;}
Member Avatar
WaltP
Posting Sage w/ dash of thyme
9,363 posts since May 2006
Reputation Points: 2,905 [?]
Q&As Helped to Solve: 1,151 [?]
Skill Endorsements: 45 [?]
Team Colleague
 
3
 

1) Never use goto . Use a while loop instead.
2) Format your code properly. It's difficult to follow and mistakes are made with bad formatting.
3) Always call srand() once and only once, at the beginning of the program.
4) Never use system("cls"); while programming. It's hard to debug code and see what has happened when the screen clears. We recommend never using it because it's annoying to the user, but if you really need it, add it after the program is running -- not before.
5) Never use system("pause"); . Why call the operating system to pause a program when cin will do it, cin is part of the language, and it's portable.

Member Avatar
imsinu
Newbie Poster
12 posts since Nov 2011
Reputation Points: -4 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

try by declaring tries as static variable.

Member Avatar
imsinu
Newbie Poster
12 posts since Nov 2011
Reputation Points: -4 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
-1
 
#include <cstdlib> //directive for rand() function
#include <time.h>  // directive for random pick of number
#include <iomanip.h>
#include <iostream.h>


using namespace std;


int main(){


srand(time(0));  //changes the secret number
int number;
number = rand() % 100 + 1;  //range of secret number 1-100
int guess;


int play = 1;


pick:
int tries = 1;


cout << setw(55) << "Welcome to the Guess the number game! \n" ;
cout << setw(55) << "I'm thinking of a secret number from 1 to 100 \n" ;
cout << setw(55) << "Guess it!\n\n" ;


do {    //loops until guess is equal to secret number
cout << "\nEnter your estimate: ";
cin >> guess;


if (guess < number){
cout << "Higher!" << endl;
tries = tries + 1;}


else if (guess > number){
cout << "Lower!" << endl;
tries = tries + 1;}
else
cout << "Your guess is right!" << endl;



} while (guess != number);
cout << "\n\nYou tried "<<tries<<" times " ;


char g;
cout << "\nTry Again? (y for yes or n for no):  ";
cin >> g;
if(g !='n'){
srand(time(0));
play = play + 1;
system("cls");
goto pick;}
else {
cout << "\nNumber of game plays is "<<play<<"\n\n" ;}


system("PAUSE");
return 0;}
Member Avatar
lupacarjie
Newbie Poster
13 posts since Aug 2011
Reputation Points: 1 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Thanks guys for the reminders :)

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: