2015-03-03 25 views
-4

这是完整的代码。当用户将u输入到菜单中时,y或n会提示他是新用户。 y工作正常,并将信息正确写入文件,但“n”不正确。如果你输入“n”,它给你选择“y”,它应该忽略。但它不是...if(user =='y'){}即使不等于'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

'='vs.'=='。我很惊讶你的编译器没有对此发出警告。 – 2015-03-03 02:22:32

+0

那么,在你问这里之前,你是否考虑过使用体面的调试器来完成这段代码? – 2015-03-03 02:22:35

+1

你的问题说'if(user =='y')'但是你的代码读取'if'(user ='y'||'n')''''''''''用户''''。 – 2015-03-03 02:22:50

回答

2

你,而不是比较==整个代码分配=

而且这样的:

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

应该是这个

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

在你的代码,无论你使用条件(内部if和while),请确保你正在检查的平等,而不是分配值。有2个这样的ifs和1个这样的情况,而你在哪里分配了R.H.S.到L.H.S.而不是检查他们的平等。

相关问题