if(user =='y'){}即使不等于'y'也会继续运行

时间:2015-03-03 02:21:04

标签: c++ if-statement

这是完整的代码。当用户将u输入菜单时,由y或n提示他是新用户。 y工作正常,并将信息写入文件正确,但" n"才不是。如果你输入" n"它为你提供了" y"的选项,它应该忽略它。但它不是......

#include <iostream>
#include <fstream>
#include <string>
#include <ctime>
#include <cstring>
#include <cctype>
#include <cstdlib>

using namespace std;

string first, last, gender, loginid, pass;
char menu, user;
int pid, passScore;

const string inFileErrMsg = "\nExisting User Input File Error!\n\n";
const string exitMsg = "\nExiting the Application\n";
const string mobAppMsg = "\n Mobile App Sales: ProjectWon.\n";
const string gameMsg = "\n  ...COMING SOON!...\n  PROJECT-game-THREE\n"
"~revenge of the project~";

int main() {
    ofstream outfile("currUser.txt");
    while (menu != 'x') {
        cout << "\n==================================";
        cout << "\n- Choose from one of the following:";
        cout << "\n\tEnter u for User Information.";
        cout << "\n\tEnter m for Mobile App Sales.";
        cout << "\n\tEnter g for GameTime.";
        cout << "\n\tEnter x to Exit the program.";
        cout << "\n==================================";
        cout << "\n>";

        cin >> menu;

        menu = tolower(menu);
        switch (menu){
        case 'g':
            cout << gameMsg << endl;
            break;
        case 'm':
            cout << mobAppMsg << endl;
            break;
        case 'u':
            cout << "Are you a new user (y or n)?" << endl;
            cin >> user;
            user = tolower(user);
            while (user = 'y' || 'n') {
                if (user = 'y'){
                    ifstream file;
                    ofstream newuser;
                    string username, password, passwordconfirm;
                    file.open("currUser.txt", ios::app);
                    newuser.open("currUser.txt", ios::app);
                    bool uservalid = false;
                    while (!uservalid)
                    {
                        cout << "First Name: ";
                        cin >> first;
                        cout << "Last Name: ";
                        cin >> last;
                        cout << "Gender: ";
                        cin >> gender;

                        gender = toupper(gender[0]);

                        loginid = first[0] + last + gender[0];
                        username = loginid;
                        cout << "Password: ";
                        cin >> password;

                        if (gender == "m"){
                            pid = 2 * (1000 + rand() % (9999 - 1000 + 1));
                        }
                        else{
                            pid = 2 * (1000 + rand() % (9999 - 1000 + 1)) + 1;
                        }

                        uservalid = true;
                    }
                    newuser << first << " " << last << " " << gender << " " << pid << " " << username << " " << password << endl;

                    cout << "\nUser Login: " << username << "\n";
                    cout << "\nName: " << first << " " << last;
                    cout << "\nPID: " << pid << "  Gender: " << gender;
                    cout << "\nUser Password: " << password << "\t";

                    file.close();
                    newuser.close();
                    break;
                }
                if (user = 'n'){
                    ifstream file;
                    string userid, password;
                    int n = 0;
                    file.open("currUser.txt");
                    if (file.is_open())
                    {
                        while (!file.eof())
                        {
                            file >> first >> last >> gender >> pid >> userid >> password;
                            n++;
                            if (loginid == userid && pass == password)
                                return n;
                        }
                        break;
                    }
                    if (user != 'n' || user != 'y')
                    {
                        cout << inFileErrMsg << endl;
                        break;
                    }

                }
            };
            break;
        case 'x':
            cout << exitMsg << endl;
            system("PAUSE");
            return(0);
        default: break;
    }
}

}

2 个答案:

答案 0 :(得分:2)

您的代码中包含作业=而非比较==

而且:

while (user = 'y' || 'n')

应该是这个

while (user == 'y' || user == 'n')

答案 1 :(得分:1)

在您的代码中,无论您在何处使用过条件(在if和while内),请确保检查相等性而不是赋值。有2个这样的ifs和1个这样的,而你已经分配了R.H.S.到L.H.S.而不是检查他们的平等。