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

if (user == 'y'){} keeps running even if it does not equal 'y'

本文关键字:不等于 运行 继续 user if      更新时间:2023-10-16

这是完整的代码。当用户在菜单中输入u时,系统会提示他是新用户。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!nn";
const string exitMsg = "nExiting the Applicationn";
const string mobAppMsg = "n Mobile App Sales: ProjectWon.n";
const string gameMsg = "n  ...COMING SOON!...n  PROJECT-game-THREEn"
"~revenge of the project~";
int main() {
    ofstream outfile("currUser.txt");
    while (menu != 'x') {
        cout << "n==================================";
        cout << "n- Choose from one of the following:";
        cout << "ntEnter u for User Information.";
        cout << "ntEnter m for Mobile App Sales.";
        cout << "ntEnter g for GameTime.";
        cout << "ntEnter 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;
    }
}

}

您在整个代码中都有赋值=,而不是比较==

这个:

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

应该是这个吗

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

在代码中,无论您在哪里使用了条件(在if和while内部),都要确保检查相等性,而不是赋值。有2个这样的如果和1个这样的while,其中你将R.H.S.分配给L.H.S.,而不是检查它们的相等性。