Hello!
I'm a complete newbie to C++ and I am having so much trouble right now! A few days ago, I did awful on a lab test asking me to create a login system similar to this one. I really want to be able to work out this problem and understand what I did wrong. I would really, really appreciate any insight or help!
This program is supposed to:
- Show a menu with options to create an account, login to an existing account or quit the menu. The menu loops until the user quits.
- If the user creates an account, it saves the username and password to a txt file
- When the user logs into the account, it compares the user's input to the saved information in the txt file. This function loops until the user gets the password correctly.
What isn't working:
- The create account function works. It saves the username and password to a txt file. But later, when I want to read the data with getline so that I can compare it to the user's input, I can't. I want to be able to retrieve the data that was written into the file, and to compare the data to the entered username and password.
Thank you so much to anyone who reads this (:
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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
|
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
// Prototypes
void createAccount();
void mainMenu();
int main ()
{
// Variables
char main_choice;
string username;
string password;
string line;
// Loop that repeats until user quits menu
do
{
// Call main menu
mainMenu();
// Get user's menu choice
cin >> main_choice;
// Create a new account
if (main_choice == 'C' || main_choice == 'c')
{
createAccount();
}
// Log in
else if (main_choice == 'L' || main_choice == 'l')
{
// Loop until user logs in
do
{
// Prompt user for id and password
cout<<setfill('-')<<setw(40)<<"-"<<endl;
cout << "\t\t\tLogin: " << endl;
cout<<setfill('-')<<setw(40)<<"-"<<endl;
cout << "Username: " << endl;
cin >> username;
cout << "Password: " << endl;
cin >> password;
// Read NameFile
ifstream NameFile ("/Users/Me/Desktop/accountinfo.txt");
NameFile.open ("/Users/Me/Desktop/accountinfo.txt");
getline (NameFile, line);
cout << line;
NameFile.close();
// Compare user's entry with NameFile
if (username == line)
{
cout << "You have successfully logged in!" << endl;
}
else if (username != line)
{
cout << "Login failed!" << endl << endl;
}
}
while (username != line);
}
// Quit
else if (main_choice == 'Q' || main_choice == 'q')
{
cout << "Quitting..." << endl;
}
// Invalid input
else
{
cout << "Invalid response." << endl;
}
}
while (main_choice != 'Q' || main_choice != 'q');
}
//*******************
// Def. of mainMenu *
//*******************
void mainMenu()
{
// Main menu
cout<<setfill('-')<<setw(40)<<"-"<<endl;
cout << "\t\t\tSIGN IN\t" << endl;
cout<<setfill('-')<<setw(40)<<"-"<<endl;
cout << "C. Create new account." << endl;
cout << "L. Login." << endl;
cout << "Q. Quit." << endl;
cout << "Enter C, L or Q." << endl << endl << endl;
}
//************************
// Def. of createAccount *
//************************
void createAccount()
{
// Variables
string username;
string password;
// Ask user for username and password
cout<<setfill('-')<<setw(40)<<"-"<<endl;
cout << "\t\t\tCreate New Account: " << endl;
cout<<setfill('-')<<setw(40)<<"-"<<endl;
cout << "Desired username: " << endl;
cin >> username;
cout << "Desired password: " << endl;
cin >> password;
cout << endl << endl;
// Write to file
ifstream NameFile ("/Users/Me/Desktop/accountinfo.txt");
NameFile >> username >> password;
// Close file
NameFile.close();
}
|