Let me set this up. I'm working on a Battleship game program. I started it a long time ago and have gone through many iterations of the program (making it more efficient and adding new/more features). I've figured out most of everything out but on my current rewrite, I'm facing a problem that I hadn't encountered before.
I have a login system - in "File/Profile/Human/*USERNAME*/Information.txt", a user name (which I've thought about getting rid of since the folder name is the same), password, and the wins and loses is held. I prompt the user to input a username. It then checks to see if a *USERNAME* folder exists. If it does, the program grabs all the stuff in "Information.txt". Then asks for the users password. If it matches the imported password - you've successfully logged in.
If the user doesn't exist and the name inputted is within bounds (6-16 characters); it's asks if you want to create this user, try a different user name, or just play an exhibition game with this name. Create and exhibition works (with the limited testing I've done so far on it). The problem is when I choose to try a different name.
If from the very beginning I input a proper username, everything works out fine. If I choose an incorrect username and try to input a new name, it doesn't read correctly from the file. It grabs gibberish.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
|
while(UNEntry)
{ std::cout << "Enter User Name:\n --> ";
std::cin.clear();
getline (std::cin,TempName);
std::cin.clear();
std::string Player_Input = "Files\\Profiles\\Human\\"+TempName+"\\Information.txt";
Profile.open (Player_Input.c_str(),std::ios::in);
//std::cout << false << ":" << Profile.is_open() << "\n\n";
if(Profile.is_open())
{ Profile >> TempName >> TempPassword >> TempRecord[Zero] >> TempRecord[One];
UNEntry = false;
std::cout << TempName << TempPassword << TempRecord[Zero] << TempRecord[One];
}
else
{ Spacer();
std::string Create[] = {"Create New","1"};
std::string Different[] = {"Try a different Username","2"};
std::string Exhibition[] = {"Switch to exhibition","3"};
int Red = 12, Off_White = 7;
int Choice;
std::string Answer;
HANDLE hConsole;
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
if(TempName.size() < Six)
{ Spacer();
std::cout << "The user name \"" << TempName << "\", is too short to exist.\n";
Space();
}
else if(TempName.size() >= Seventeen)
{ Spacer();
std::cout << "The user name \"" << TempName << "\", is too long to exist.\n";
Space();
}
else
{ std::cout << "Login Error: " << TempName << " is not a profile.\n\n";
std::cout << "Do you want to:\n";
for(int Step = One ; Step < Four ; Step++)
{ Tab();
SetConsoleTextAttribute(hConsole, Red);
std::cout << Step;
SetConsoleTextAttribute(hConsole, Off_White);
std::cout << ". ";
if(Step == One)
{ std::cout << Create[Zero];
}
if(Step == Two)
{ std::cout << Different[Zero];
}
if(Step == Three)
{ std::cout << Exhibition[Zero];
}
Space();
}
std::cin.clear();
std::cout << "\n\t -> ";
getline (std::cin,Answer);
if(Answer.length() == One)
{ Choice = atoi(Answer.c_str());
switch(Choice)
{ case One:
Name = TempName;
Human::Create();
Entry = false;
UNEntry = false;
PWEntry = false;
return true;
break;
case Two:
Spacer();
std::cout << "Try something else then\n\n";
//PWEntry = true;
break;
case Three:
Entry = false;
UNEntry = false;
PWEntry = false;
return false;
break;
default:
Spacer();
std::cout << "Error: Improper input\n\n";
}
}
else
{ Spacer();
std::cout << "Error: Improper input\n\n";
}
}
}
Profile.close();
}
|
is the part of the function that's screwing up. The rest of the function handles the password input/check. I know I could split these parts up but it's really not needed.
I've tried implementing "eof()" but it would infinite loop when it was doing file input.
Last, I know there is probably a lot of programming rules I'm breaking. That doesn't bother me at all. This program is purely for fun and is only meant for such.
Thanks for any help.