0

No idea where the issue is coming from so I will insert the whole sub-routine. When you insert a string into the guess space it will loop infinitly, if you insert a number it will return "ion do you want to go?"(which isn't even written anywhere in the program).

void guess(){
int guess;
string guess2;
string guess_status="";
bool win;
int attempts;
int counter;
int num;
while (guess2 != "exit"){
    num=rand() %100 + 1;
    win=0;
    counter=0;
    while (win != 1){
        attempts=5-counter;
        cout << "Guess a number                Attempts Left: " << attempts << endl;
        cout << "between 1 and 100       ============================\n                              The Guesing Game\n                        ============================" << endl;
        cout << "\n" << guess_status << endl;
        cout << "\nGuess: ";
        cin >> guess;
        system("cls");
        if (!cin) {
            guess2=guess;
            if (guess2 != "exit"){
                guess_status="Please insert a valid number, restarted game.";
                win=1;
            }
        }
        if (cin){
            if (guess==num){
                win=1;
                guess_status="Correct! Generated new number.";
            }
            if (guess != num){
                if (guess < num){
                    guess_status=num +"was too low!";
                }
                if (guess > num){
                    guess_status=num +"was too high!";
                }
            }
        }

    }


}


}

The routine is indented, it just didn't paste that way

Resmik
  • 7
  • 1

1 Answers1

2
int guess;
string guess2;
guess2=guess;

This is your problem. You can't convert an int into a string this way. What you're actually doing is telling the computer that guess2 is a string that starts at the memory address that the value of guess is currently. That's why you're getting a string output that's not even in your program- it's just what happens to be at that address.

See here for how to convert an int to a string: Easiest way to convert int to string in C++

Also, don't use cin >> guess. Get the input as a string, then check to see if it can be converted to an integer.

Community
  • 1
  • 1