CodeGuru Forums - C++ (Non Visual C++ Issues) https://forums.codeguru.com/ Ask or answer C and C++ questions not related to Visual C++. This includes Console programming, Linux programming, or general ANSI C++. en Wed, 21 Feb 2024 07:04:18 GMT vBulletin 60 https://forums.codeguru.com/images/misc/rss.png CodeGuru Forums - C++ (Non Visual C++ Issues) https://forums.codeguru.com/ if statement being ignored despite conditions being true https://forums.codeguru.com/showthread.php?566244-if-statement-being-ignored-despite-conditions-being-true&goto=newpost Wed, 14 Feb 2024 16:55:16 GMT > menuSel; if (menuSel==1){ cout << "Taking you to Standard gameplay..." << endl << endl; for (int y=0;y<10; y++){ cout << questionBank[y] << endl; for (int x=0;x<3;x++){cout << x+1 << ". " <> 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);//A relic from when I was trying to have the score and streak/etc reset automatically after you complete the quiz using functions, I scrapped it when I realised that was stupid to create a function to re-declare variables that don't need to be declared differently than they already were, so I created an endless loop that includes the initialization of the variables, since that makes way more sense and is more efficient. Today, it does absolutely nothing so I chose to keep it in the program as a reminder to myself. 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:"<> menuSel2; if (menuSel2=='E'||menuSel2=='e'){ cout << "Returning to menu\n"; break; } else if (menuSel2=='Y'||menuSel2=='y'){ 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 << ". " <> 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; } --------- ]]> Hi everyone, I've been working on a quiz program for my class and something really strange happened to my program when I opened it today, Whenever I attempt to access the alternate gamemode (endless), the console ignores the if statement that should take you to the game. There are no warnings (besides an srand initialization) that tells me anything, and the program was not behaving this way yesterday, which was the last time I touched this program. I'm seriously lost here. I asked my teacher, and they had no idea. So I come to you, codeguru forum: What the hell is up with my program?? If it helps, I am writing this on xCode version 12.4
Code:

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.";
        cout << "\n1.Standard\n2.Endless";
        cin >> menuSel;
        if (menuSel==1){
            cout << "Taking you to Standard gameplay..." << endl << endl;
            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);//A relic from when I was trying to have the score and streak/etc reset automatically after you complete the quiz using functions, I scrapped it when I realised that was stupid to create a function to re-declare variables that don't need to be declared differently than they already were, so I created an endless loop that includes the initialization of the variables, since that makes way more sense and is more efficient. Today, it does absolutely nothing so I chose to keep it in the program as a reminder to myself.
                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'){
                    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;
}

]]>
C++ (Non Visual C++ Issues) BakedBintoBean https://forums.codeguru.com/showthread.php?566244-if-statement-being-ignored-despite-conditions-being-true
<![CDATA[[RESOLVED] No such file or directory with Hello World!]]> https://forums.codeguru.com/showthread.php?566236-RESOLVED-No-such-file-or-directory-with-Hello-World!&goto=newpost Sun, 04 Feb 2024 11:10:18 GMT I have just started to use vs code for a project but I have a simple issues that I am sure someone here will resolve in two minutes.

I have a folder: c:\documents\projects\vmConsole. Inside vmConsole directory I have 3 directories called include, src and .vscode.

When I separate my .h and ,cpp files I get No such file or directory in vs code. I am certain it's because I have not told g++ to look in include and src directories. I am aware of -I for g++ but I don't understand JSON files.

I know the "-I ${fileDirname}./include", is wrong, can you advise as I have spent 2 days on this issue.

launch.json file:

{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe build active file",
"command": "D:\\msys64\\ucrt64\\bin\\g++.exe",
"args": [
"-fdiagnostics-color=always",
"-g",
"-I ${fileDirname}./include",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": ["$gcc"],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}




I hope someone can advise. ]]>
C++ (Non Visual C++ Issues) Gerald Bates https://forums.codeguru.com/showthread.php?566236-RESOLVED-No-such-file-or-directory-with-Hello-World!