-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.

Shahs
  • 3
  • 4
  • 1
    Define what do you mean by "glitching". – Algirdas Preidžius Dec 16 '15 at 15:01
  • The game switches rapidly from "Invalid!" to the main menu. – Shahs Dec 16 '15 at 15:02
  • 1
    What was your input, then? Edit your question to contain all the information needed. – Algirdas Preidžius Dec 16 '15 at 15:05
  • Duplicate of [integer input validation, how?](http://stackoverflow.com/q/13212043) – GingerPlusPlus Dec 16 '15 at 15:11
  • I don't understand how it is a duplicate? This is asking a similar topic, but are using two different pieces of code. – Shahs Dec 16 '15 at 15:12
  • 1
    @Shahs It is a duplicate, since you are not asking about the similar problem, you are asking about the exactly the same problem. – Algirdas Preidžius Dec 16 '15 at 15:17
  • ... his involves a piece of code that includes a while loop, but I can't find a switch statement anywhere in there. I have read through the answer and the question. Just because it involves a SIMILAR problem, it does not mean that it is the exact same! – Shahs Dec 16 '15 at 15:23
  • 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. – Kevin Dec 16 '15 at 15:27
  • @Shahs And, you know what, your problem doesn't have anything to do with `while` loop, or `switch` statement! It has everything to do with `cin` being in an invalid state after character is read, when it was expecting an int. Which is **exactly** what that other question is about. If the code is different, it doesn't automatically make them different problems. – Algirdas Preidžius Dec 16 '15 at 15:27
  • I understand. I am sorry about the misunderstanding. I am a noob at this after all. – Shahs Dec 16 '15 at 15:32
  • Wow, all those `endl`s; `cin` is truly flushed! (Just use `'\n'` when all you need is a newline) – Pete Becker Dec 16 '15 at 16:27

1 Answers1

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');
Buddy
  • 10,874
  • 5
  • 41
  • 58