Good day, everyone.
I am refreshing my c++ skills from a year break or so :$
I am having some troubles remembering on how to write to a file. I have the general idea down, but I am not sure what I am doing wrong. I would be much appreciative if anyone can help me figure this out.
(by the way I am using Netbeans IDE 6.8)
Here is my code: (the red text is where the trouble is)
/*
* Program to let user login and logout.
* User's information will be put into a separate .txt file called "loginfo.txt"
*/
#include <stdlib.h>
#include <iostream>
#include <fstream>
using namespace std;
struct LogIn
{
string username;
string password;
};
void menu();
// displays the menu and double checks if the .txt file is empty or not
// to ensure if there are any previous users registered in the program
int main()
{
menu();
return 0;
}
void menu()
{
LogIn log; // declaring a struct object
int choice = 1;
fstream loginfo; // file declared
loginfo.open("loginfo.txt"); // opening txt file
if(loginfo.eof())
exit(0);
//making sure the user inputs the correct
while (choice == 1 || choice == 2)
{
cout << "~~~~~~~~~~~~~~~~~~\n"
<< "~SHOP CENTER PLUS~\n"
<< "~~~~~~~~~~~~~~~~~~\n\n";
cout << "Would you like to:\n"
<< "1. Log in\n"
<< "2. Register (NEW USERS ONLY)\n"
<< "3. Exit\n"
<< "?> ";
cin >> choice;
if(choice == 1)
{
bool isEmpty = loginfo.peek() == EOF;
if(isEmpty == true)
cout << "\nERROR: No one is registered in the system\n"
<< "Please go back and register as a new user.\n\n\n";
else
{
//section is "under construction"
cout << "\nUsername: ";
cin >> log.username;
cout << "\nPassword: ";
cin >> log.password;
}
}
else if (choice == 2)
{
cout << "Please set the following for your Log In information:\n"
<< "\nUsername: ";
cin.ignore();
getline(cin, log.username);
loginfo << log.username; // not sure why this isn't displaying in txt file
cout << "\nPassword: ";
getline(cin, log.password);
loginfo << log.password;
}
else if(choice == 3)
{
cout << "\nHave a good day.\n\n\n";
exit(0);
}
else
cout << "\nInvalid input. Please try again.\n\n";
}
}