views:

154

answers:

3

Alright, I have a question, I veered away from using strings for selection so now I use an integer. When the user enters a number then the game progresses. If they enter a wrong character it SHOULD give the else statement, however if I enter a letter or character the system goes into an endless loop effect then crashes. Is there a way to give the else statement even if the user defines the variable's type.

// action variable;
int c_action: 

if (c_action == 1){
    // enemy attack and user attack with added effect buffer. 
    ///////////////////////////////////////////////////////
    u_attack = userAttack(userAtk, weapons);
    enemyHP = enemyHP - u_attack;

    cout << " charging at the enemy you do " << u_attack << "damage" << endl;
    e_attack = enemyAttack(enemyAtk);
    userHP = userHP - e_attack;
    cout << "however he lashes back causing you to have " << userHP << "health left "  << endl << endl << endl << endl;
    //end of ATTACK ACTION
}else{
    cout << "invalid actions" << endl;
    goto ACTIONS;
}
+2  A: 

You haven't shown how you are reading the integer. But in general you want to do something like this:

int answer;
if (cin >> answer)
{
   // the user input a valid integer, process it
}
else
{
   // the user didn't enter a valid integer
   // now you probably want to consume the rest of the input until newline and
   // re-prompt the user
}
Brian Neal
Specifically, something like cin.clear(); cin.ignore(numeric_limits<streamsize>::max(), '\n');
+1  A: 

The problem is that your cin is grabbing the character and then failing, which leaves the character in the input buffer. You need to check whether the cin worked:

if( cin >> k) { ... }

or

cin >>k;
if(!cin.fail()) { ... }

and if it fails, clear the buffer and the fail bit:

cin.clear(); // clears the fail bit
cin.ignore(numeric_limits<streamsize>::max()); // ignore all the characters currently in the stream

EDIT: numeric_limits is found in the limits header file, which you include as per usual:

#include <limits>
Niki Yoshiuchi
I must be getting the wrong code or something because I don't see a single use of cin in the OP code.
Noah Roberts
@Noah Roberts: He mentioned that the infinite loop is caused by the user entering a character instead of a number. I also commented on an earlier question he asked about cin, so I assumed this was a follow up.
Niki Yoshiuchi
A: 

Your problem is not with the else-statement, but with your input. If you do something like

cin >> i;

and enter a character, the streams error state is set and any subsequent try to read from the stream will fail unless you reset the error state first.

You should read a string instead and convert the strings contents to integer.

Axel
ha... 40 seconds late ;)
Axel
thanks, thats a great idea, im teaching myself C++ so i have tons of questions.
TimothyTech
@Timothy: Not to be a joy-kill, but you're just going to hurt yourself in knowledge that way. Grab a good book. http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list
GMan