Hello,
Now, this may sound stupid, as it concerns basic file I/O, but my code doesn't work correctly- at least it isn't doing what I want it to. I wanted it to write a file the first time it runs, which it does perfectly, and every time it is run after that, it should read the written file and store values off it(like username and password) and correlate it to validate user input. However, I am facing some problems, which is very likely to be an elementary one, about how I am not getting the required values after reading the file.
for example: the file is as following
1 admin adminpass
where the three columns are id, admin username and admin password. It writes it perfectly, reads it as well, as I get no compilation error and no runtime error as well.
Here is the code:
#include <iostream>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iomanip>
using namespace std;
//////////////////////////Defenitions and Standards///////////////////////////
#define adminfile "admin_info.dat"
#define none 0
const int NotUsed = system("color 1B");
unsigned int id;
int q, first_run(), i=1;//Integer Functions and variables
double w=-65000;//Double Variables
void generate_choices(), admin_validator();//References Modules used
void long_slashes(), small_slashes(), smallest_slashes();//References Slashes
void check_admin(unsigned int, const char, char);
string userstring, passstring, fullstring;//Other Global variables
char usrnm, passwd;
char pwd, uname, password, username;
////////////////////////End Defenitions and Standards/////////////////////////
//////////////////////////////All fstream/////////////////////////////////////
ifstream fin(adminfile, ios::in | ios::out | ios::app);
ofstream fout;
/*fstream used in main()*/fstream file(adminfile, ios::in);
////////////////////////////End All fstream///////////////////////////////////
////////////////////////////////////Main/////////////////////////////////////
int main()
{
clock();
string choice;
if((file.fail())||(file.peek() <= none))
{
//////////////////////////////////////////////////////////////////////////
/* CONFIGURATION MANAGER
This checks for the config ini file, described below in the
configuration module.
*/
small_slashes();
smallest_slashes();
check_admin(id, username, password);
cout << "\n\n";
smallest_slashes();
small_slashes();
for(; w<=1000000; w++);
system("cls");
/* END CONFIGURATION MANAGER
This describes the end of Config manager, and is defined in
the configuration module.
*/
}
//////////////////////////////////////////////////////////////////////////
/* ADMIN VALIDATOR
This checks whether the password as defined in config matches
with what the user entered.
*/
/*cout << "\n\n\t\t";
smallest_slashes();
admin_validator();
cout << "\n\n\t\t";
smallest_slashes();
for(w=-65000; w<=1000000; w++);
system("cls");
/* END ADMIN VALIDATOR
This describes the end of the Admin Validator, whose function
is described in the modules below.
*/
///////////////////////////////////////////////////////////////////////////
long_slashes();
cout << "\n\n\t\t\t\tThe AdminTool\n\n\t\t\t\t\t\tV1.0\n\n";
long_slashes();
cout << "\n\n\t";
cout << "\n\n\tWelcome to the AdminTool!\n\n\t";
generate_choices();
cout << "\n\n\tWhich of these would you like to do? (1- ): ";
cin >> choice;
cout << pwd << "\t\t" << uname;
system("pause");
return 0;
}
//////////////////////////////////End Main///////////////////////////////////
//////////////////////////////////Slashes///////////////////////////////////
void long_slashes()
{
for(q=1; q<=70; q++)
{
cout << "/";
}
}
void small_slashes()
{
for(q=1; q<=45; q++)
{
cout << "#";
}
}
void smallest_slashes()
{
for(q=1; q<=35; q++)
{
cout << ".";
}
}
/////////////////////////////End Slashes//////////////////////////////////////
/////////////////////////////Configuration Checks/////////////////////////////
void assign(unsigned int id, const char username, char password)
{
uname=username;
pwd=password;
cout << setiosflags( ios::left ) << setw( 10 ) << id
<< setw( 13 ) << uname << setw( 7 ) << setw( 12 )
<< resetiosflags( ios::left )
<< pwd << '\n';
}
void check_admin(unsigned int id, const char username, char password)
{
if(fin.fail()) //File error
{
first_run();
}
else if(fin.peek() <= none)
{
first_run();
}
else
{
cout << setiosflags( ios::left ) << setw( 10 ) << "ID"
<< setw( 13 ) << "Username" << "Password\n"
<< setiosflags( ios::fixed | ios::showpoint );
while ( id >> username >> password )
{
assign(id, username, password);
}
fin.close();
fin.clear();
}
}
int first_run()
{
int numch;
cout << "\n\t";
cout << "\n\n\tHow many accounts would you like to create? ";
cin >> numch;
cout << "\n\n\t";
fout.open(adminfile , ios::out | ios::app );
for(; i<=numch; i++)
{
cout << "\n\n\tPlease Enter a new username: ";
cin >> usrnm;
cout << "\n\n\tPlease Enter a new password: ";
cin >> passwd;
userstring=usrnm+" ";
passstring=passwd;
fullstring=" "+userstring+passstring+"\n";
fout << i << fullstring;
}
//Close and clear the file write buffer
fout.close();
fout.clear();
cout << "\n\n\tSuccess!\n\n\t";
//And reopen the file
fin.open(adminfile , ios::in );
return 0;
}
//////////////////////////End Configuration Checks////////////////////////////
///////////////////////////////Admin Validator////////////////////////////////
void admin_validator()
{
char adminname[120], adminpass[100];
reDo:
cout << "\n\n\t\tPlease Sign in Below:\n\n\t\t";
cout << "Username: ";
cin >> adminname;
cout << "\n\n\t\tPassword: ";
cin >> adminpass;
cout << "\n\n\t";
/*if((adminname==uname))
{
}
else
{
cout << "\n\t\tError!!\n\n";
goto reDo;
}*/
}
/////////////////////////////End Admin Validator//////////////////////////////
///////////////////////////////Modules////////////////////////////////////////
void generate_choices()
{
cout << "1)\tEdit your Info\n\n\t";
cout << "2)\t.........\n\n\t";
}
Is the code wrong or have I done something illogical? Am I using the wrong method? Or is it the program? Any help would be well appreciated.
Thanks in Advance
PS: I know I am not the best in considering computer memory whilst programming.