用户创建程序失败

时间:2017-01-13 02:29:23

标签: c++ c++11

我正在编写这个程序,允许用户输入他们的名字,然后把它放到一个文件中。然而,这不起作用。它只需要名字,什么都不做!它也只运行第一个if语句。

#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <Windows.h>

using namespace std;

class user{
public:
    string name;
    int userID = rand() % 1001;

    void setName(string n) { name = n; };
    string getName() { return name; };
    int returnID() { return userID; };
};

int main() {
    HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
    string udef;
    user mat;
    int id = mat.returnID();

        while (udef != "exit" || "Exit") {
            SetConsoleTextAttribute(h, FOREGROUND_BLUE | FOREGROUND_GREEN |        FOREGROUND_INTENSITY );
            cout << "Type a command to continue. You may also type help for   a list of commands." << endl << "User: ";
            getline(cin, udef);
            if (udef == "new" || "New") {
                cout << "Type your name:" << endl << "User: ";
                getline(cin, udef);
                mat.setName(udef);
                ofstream userfile(id + ".txt");
                if (userfile.is_open()) {
                    cout << "Generating User info file..." << endl;
                    Sleep(10);
                    cout << "File Name : '" << id << "'" << endl;
                    userfile << mat.getName();
                    Sleep(10);
                    cout << "Appending information..." << endl;
                    cout << "Done! " << mat.getName() << " has been written to line(s) 1 of " << id << ".txt!" << endl;
                }
                userfile.close();
            }
            else if (udef == "help" || "Help") {

            }
        }

    return 0;
}

1 个答案:

答案 0 :(得分:1)

尝试

while ( (udef != "exit") && (udef != "Exit") ) 

而不是

while (udef != "exit" || "Exit") 

简单明了"Exit" true udef Exitexitif (udef == "new" || "New") 不同。

P.s。:显然,与

相同的问题(以及类似的纠正)
else if (udef == "help" || "Help") 

if ( (udef == "new") || (udef == "New") ) 

他们成了

else if ( (udef == "help") || (udef == "Help") ) 

panmove