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;}``````

1) Never use `goto` . Use a `while` loop instead.
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.

commented: Excellent tip on the clear screen +10

try by declaring tries as static variable.

``````#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;}
``````
commented: We use CODE Tags here, and we do NOT write programs for people. They write them and we AID them only!!! -4

Thanks guys for the reminders :)