lundi 2 mars 2015

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

this is the full code. when the user enters u into the menu, he is prompted by a y or n, for being a new user. The y works fine, and writes the information to file properly, but the "n" does not. If u type "n" it gives you the options for the "y", which it should be ignoring. but it isnt...



#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;
}
}


}


Aucun commentaire:

Enregistrer un commentaire