C++ Beginner else statement not working [closed]

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-11 14:51:35

问题


I'm designing a simple RPG just to get me started in C++ coding, however my else statement is not working, and I feel my code is bulkier than it should be. Could you wonderful people give me some tips on how to improve my code and how to fix the else statement. I am aware of the "System("cls")" hatred across everything, but I put it there because it makes it look more cleaner than other methods.

I'm still a complete novice at C++ by the way

#include <iostream>
#include <windows.h>
#include <string>

using namespace std;
int save = 0;
int menuinput;
int raceinput;
string race;
int cont1;

int main(void)
{
        int save = 0;
        int menuinput;
        int raceinput;
        string race;
        int cont1;
        cout << "(Insert Generic RPG Name Here) \n";
        if (save == 1){
            cout << "Main Menu\n";
            cout << "[1.] Continue\n";
            cout << "[2.] New Game\n";
            cout << "[3.] Options\n";
            cin >> menuinput;


        }

        if (save == 0){
            cout << "Main Menu\n";
            cout << "[1.] New Game\n";
            cout << "[2.] Options\n";
            cin >> menuinput;
            system("cls");


        }

        if (menuinput == 1)
        {
            cout << "Please select your race\n";
            cout << "[1.] Human\n";
            cout << "[2.] Elf\n";
            cout << "[3.] Dwarf\n";
            cout << "[4.] Orc\n";
            cin >> raceinput;

            if (raceinput == 1){
                race = "Human";
                cont1 = 1;

            }
            if (raceinput == 2){
                race = "Elf";
                cont1 = 1;
            }
            if (raceinput == 3){
                race = "Dwarf";
                cont1 = 1;
            }
            if (raceinput == 4){
                race = "Orc";
                cont1 = 1;
            }
            else {
                system("cls");
                cout << "Invalid Input, please try again.";
                Sleep(1000);
                main();
            }
            if (cont1 = 1){
                system("cls");
                cout << race;
                cin >> race;

            }
            else
            {
                system("cls");
                cout << "Invalid Input, please try again.";
                Sleep(10000);
                main();
            }
        }


}

EDIT: To reiterate, when I try to use the first else statement (where the main menu is present) the else statement does not activate, and immediately ends the program. The else statement message does not show. And in the second else statement, the else statement message shows, but the Sleep function does not work, it shows for about about half a millisecond then disappears.


回答1:


This line: if (cont1 = 1){

You have = where you should have ==.

So, your else never happens because you're not checking if cont equals 1, you're making it equal.



来源:https://stackoverflow.com/questions/37759474/c-beginner-else-statement-not-working

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!