-6

I am trying to make my first text-to-play game in C++. The only problem is that the default statement in my code is making the game glitch. I am trying to use another function called exceptionHandler to deal with the default statement, but it doesn't seem to be working. Any suggestions? Here is the code:

#include <iostream>
#include <cstdlib>

using namespace std;

void space(), menu(), exceptionHandler();
int back1, back2;

int main()
{
   cout << "Siddiqui Interactive presents..." << endl;
   cin.get();
   system("CLS");
   cout << "Outland" <<endl;
   cin.get();
   int bob = 0;
   //to loop back to main menu
   while(bob < 5){
     system("CLS");
     cout << "Outland" <<endl;
     space();
     cout << "Press 1 to begin" <<endl;
     cout << "Press 2 for credits" <<endl;
     cout << "Press 3 to quit" <<endl;
     int switch1;
     cin >> switch1;
     switch(switch1){
         case 1:
             //nothing here for now
             break;
         case 2:
            system("CLS");
            menu();
            if(back1 == 1){
                   system("CLS");
                   //clears screen to loop back to the menu
             }
             break;
         case 3:
            return 0;
            break;
        default:
            system("CLS");
            exceptionHandler();
       }
  }
  return 0;
}

void menu(){
   //to create a function for the menu, saves time
   cout << "This game was coded by: Shahmir Siddiqui and Ibrahim" <<endl;
   cout << "Outland was designed by: Azmir Siddiqui" <<endl;
   space();
   cout << "Press 1 to go back" <<endl;
   cin >> back1;
}

void space(){
   //just saves time
   cout << "" <<endl;
}

void exceptionHandler(){
    //to handle exceptions or errors
    system("CLS");
    cout << "Invalid!" <<endl;
    space();
    cout << "Press 1 to go back" <<endl;
    cin >> back2;
    if(back2 == 1){
       system("CLS");
       //also clears screen to loop back to main menu
    }
}

EDIT: Let's say, I typed in d instead of 1, it just keeps on fluctuating rapidly between error screen and main menu.

11
  • 1
    Define what do you mean by "glitching". Commented Dec 16, 2015 at 15:01
  • The game switches rapidly from "Invalid!" to the main menu. Commented Dec 16, 2015 at 15:02
  • 1
    What was your input, then? Edit your question to contain all the information needed. Commented Dec 16, 2015 at 15:05
  • 1
    @Shahs It is a duplicate, since you are not asking about the similar problem, you are asking about the exactly the same problem. Commented Dec 16, 2015 at 15:17
  • 3
    The common piece between your code and the code in the link is what's causing your problem, which is trying to read an int but not verifying that it was actually read. Commented Dec 16, 2015 at 15:27

1 Answer 1

1

cin >> switch1 tries to read in an integer. If you type d (which can't be converted to an int, it doesn't "eat" the bad input so you must clear it manually.

Try adding this to your error case:

cin.clear();
cin.ignore(INT_MAX, '\n');
Sign up to request clarification or add additional context in comments.

4 Comments

I've tried it out, but it still does not solve the problem
@Shahs Look in the link in the comments below your question, in the answers of the question, that your question is a duplicate of, there's your answer.
@Shahs Edited my answer, see if it works for you now.
It's giving me an error (sorry if I am a noob) error: 'INT_MAX' was not declared in this scope

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.