If statement being ignored despite conditions being true

Hello all, I’ve been working on a quiz game program for my class, and it includes if statements for a menu selection screen. The program was almost complete, and I was going to finish it today, but when I opened up the project to test out the endless mode, the menu selection screen began repeating itself. I tested it JUST before I signed out of the computer yesterday, and this problem was not occuring. Nobody else signed into my computer and tampered with the code, no errors/warnings relating to the if() statements are being shown, and I am seriously confused. What the ■■■■ is up with my code?? I asked my teacher for help, and he was just as confused as I am. Seriously on my last legs here.

`int main(int argc, const char * argv) {

for (;;){

    
    srand(time(NULL)); //randomize seed for rand(),
    int score = 0;//The players score, this is meant to encourage getting answers right consistently, because they lose score if they get a question wrong. This is like their total grade while the 'total' variable is like their mark on a test.
    int streak = 0;//Everytime a player gets an answer correctly, this number goes up by one. If they get a question wrong, it resets to 0.
    int total = 0;//the total amount of questions that the user gets right, think of it as their mark
    int answers[10] = {1,2,3,1,3,2,3,3,1,1};//The Answer key, this tells the computer which input from the user is the correct input to each question
    int uInput;//the actual variable that the user interacts with, it gets compared to the numbers from answers to see if the player answered correctly/
    int menuSel;//menu selection input
    char menuSel2;//alternative menu selection for characters

    //A NOTE ON MODDING THE GAMES QUESTIONS: I have tried to make the process as easy as possible if you wish to change the questions/answers. If you want to add/remove questions from the program, make sure to edit:int answers[], string questionBank/answersBank, and change the value of y in the for loop to ensure that everything works, MAKE SURE THAT THE ANSWERS AND QUESTIONS ARE IN THE SAME PLACE IN THEIR RESPECTIVE ARRAYS!!! It will only display them in the order that they are in, unless you are playing endless, where they are randomly selected. I suggest only playing endless mode if you make changes to the code, just for the sake of it being much easier.

    string questionBank[10] = {"If you are designing an app and you want your audience to be old ladies, how should you design your app?", "What is ASCII and how do you use it?", "What would the bare minimum requirements be for a calculator program?", "What is another feature you might expect in a basic calculator program?", "When is it appropriate to add an easter egg inside of your program?","You just finished the prototype for your new program, who should you show it to for the best feedback?","You just got a new assignment to create a program for a quiz game about the design process. What should you start working on first?","Who would be the most appropriate candidate to test your C++ program?","What would the ideal audience be for an economy-based game that you developed in C++?","What might be some requirements for a vending machine program?"
        };
    string answersBank[][3] = {{"Very easy to use and with simple controls","An advanced program with technical mechanics, using a large vocabulary ","Use slang terms and acronyms so it will take less time to read"},{"Data storing mechanism that allows you to store data locally","Special characters for decoration and design","A type of loop "},{"Input/Output, loops, and Math","Loops, Math, and Output","Math, and Output"},{"Multiple Step Equations","ASCII art and decoration","Shortcuts"},{"In a personal project with no affiliation to other organizations","In projects you are very proud of","Always"},{"Mr. Dawson","Your classmate who sits right next to you","Mrs. Speechley"},{"The aesthetics of the program","All the questions you want to use for the quiz game","The code itself for the program"},{"A random level 1 student","Mr. Dawson","A nearby level 2"},{"Other C++ fans and programmers","12-year-olds and little kids","Mobile Gamers"},{"Connecting different variables so they can interact with eachother (money, products, etc)","A model of the vending machine","Tracking each sale as a statistic"}};//all of the potential answers to all of the questions
//initializing input system and variables

    cout << "Welcome to The Amazing, Incredible, Super, Awesome, Exciting, Engaging, Fun, Extreme, Unbelievable, Epic, Design Process Game! Select the gamemode you would like to play today."; //Menu selection screen, this is where the bug starts happening. 
    cout << "\n1.Standard\n2.Endless";
    cin >> menuSel;
    if (menuSel==1){
        cout << "Taking you to Standard gameplay..." << endl << endl;//there are other glitches that I noticed going on in the standard mode, but these are not my main concern right now, if you can find a way to fix this also, it would be very appreciated!
        for (int y=0;y<10; y++){
            cout << questionBank[y] << endl;
            for (int x=0;x<3;x++){cout << x+1 << ". " <<answersBank[y][x] << endl;}
            cin >> uInput;
            
        cout << "Thank you for playing, your total is:" << total << "/10\n";
   
        if (total==10){
            cout << "Holy crap, a perfect score! You should try the endless mode for an even bigger challenge!\n";
            int reset(int);//old code. does nothing
            cout << "Press ENTER to return to menu";
            system("read");
        }
        else if (total<=9 && total>=5){
            cout << "Want to try again for a perfect score?";
            int reset(int);
            cout << "Press ENTER to return to menu\n";
            cout << "Score:"<<score<<" Total:"<<total<<" Streak:"<<streak;
            system("read");
        }
        else if (total<5){
            int reset(int);
            cout << "Better luck next time..";
            cout << "Press ENTER to return to menu";
            system("read");
        }
        else {
                cout << "Looks like somebody did something they shouldn't have.. :(\n";
            int reset(int);
            cout << "Press ENTER to return to menu\n";
            cout << "Score:"<<score<<" Total:"<<total<<"Final Streak:"<<streak;
            system("read");
            }
    }
            if (menuSel==2){
                cout << "WARNING: Endless mode is extremely dangerous and hard, if you get a single question wrong, your run ends. Are you sure you want to continue?" << endl;
                cout << "Y-Continue\nE-Return to Menu";
                cin >> menuSel2;
                if (menuSel2=='E'||menuSel2=='e'){
                    cout << "Returning to menu\n";
                    break;
                }
            else if (menuSel2=='Y'||menuSel2=='y'){ //the if statement in question that is being ignored
                cout << "Taking you to endless mode..\n";
                while (bool z=true){
                    int randomGrabr=(rand() % 10);
                    cout << questionBank[randomGrabr] << endl;
                    for (int x=0;x<3;x++){
                        cout << x+1 << ". " <<answersBank[randomGrabr][x] << endl;}
                    cin >> uInput;
                    if (uInput==answers[randomGrabr]){
                        total=total+1;
                        streak=streak+1;
                        if (streak<3){
                            score=score+250;
                        }
                        if (streak>=10){
                            score=(score*streak/100)+score;
                            cout <<"SUPER STREAK! " << streak << endl;
                        }
                        cout << "Correct! So epic! Score:" << score << endl << "Press ENTER to continue" << endl;
                        system("read");
                    }
                    if (uInput!=answers[randomGrabr]){
                        cout << "Incorrect! Game over. You got a total of: " << streak << "answers correct!";
                        
                        }
                    }
                }
            }
        }

}
return 0;

}`

Real answer is use a debugger and step through the program to try to replicate and pinpoint the issue.

The surface answer might be related to

for (;;)

which might be an anti-pattern for something this large. It’s essentially a giant while(true).
I would consider keeping a state variable that explicitly terminates it when expected.

So for example

bool isOver = false;

while (!isOver)
{
  //program
  if (endCondition)
    isOver = true;
    break; // in the case you're in some nested loop
}
1 Like

Hi,
I think it may be an issue with where your closing braces are.
For instance;
The closing brace for the code block of

if (menuSel==1){

is towards the end of the file.

Whereas the one just before;

if (menuSel==2){

looks like that’s where the first should end, but is, in fact, closing the for-loop.

for (int y=0;y<10; y++){

It may be how it’s formatted on here, but try and be consistent with how you indent code blocks as it’ll make reading your code easier - and tracking down bugs.

Hope that helps

1 Like