Hi every one, im new on this forum and i've got a problem that i could not find an answer to anywhere.Im 15 and im learning C++ as my first programming language and im just on the first few lesons.
I have made a challenge for myself: make a simple account manager.
My compiler is Code::Blocks 10.05.
I'm running it on Windows 7 64bit.
#include <iostream>
usingnamespace std;
short wybor; ///declaring
string nazwakonta , nazwakonta1 , haslo, haslo1 ; ///variables
void login(){ ///names of the switch cases \/ /////
cout<<"Log in to your account;";} ///
void rejestracja(){ ///
cout<<"Register your account";} ///
void wyjscie(){ ///
cout<<"Exit";} ///names of the switch cases /\ ////
int main(){
cout<<"1. Register\n"; /// displaying the menu.
cout<<"2. Log in\n";
cout<<"3. EXIT\n";
cout<<"Selection: ";
cin>>wybor;
switch(wybor){ /// the switch case.
case 1:
rejestracja();
cout<<"\nPlease enter your new account name. "; /// asks to type in your new acc name.
cin>> nazwakonta;
cout<<"\nPlease enter your new password. "; /// asks to type in your password for the newly created acc.
cin>>haslo;
cout<<"Please remember your account information which is shown below:\n"; /// shows the acc name and password.
cout <<"Account: "<< nazwakonta <<"\n" <<"Password: "<< haslo<<"\n";
cout<<"Please log in now.\n"; ///tells to log in onto the newly created acc.
cout<<"Account Name: "; ///gets acc name.
cin>> nazwakonta1;
cout<<"\nPassword: "; ///gets acc password
cin>> haslo1;
if(nazwakonta1 == nazwakonta && haslo1 == haslo){ ///if your acc name and password matches with the one you.
cout<<"\nYou have signed in as: "; ///created before,a message that you have logged in shows up.
cout<< nazwakonta;}
else{
cout<<"The account name and password does not match.\n"; ///if the acc name and pasword does not match with the one you created.
cin.get();} ///earlier,an invalid acc name or password shows up.
break;
case 2:
login();
cout<<"Account Name: "; ///case 2 asks you to log in if you have an account//////
cin>> nazwakonta1; /*PROBLEM IS HERE -.- */
cout<<"\nPassword: "; /*I need to know how to save the already created account & pass and how to load it up*/
cin>> haslo1;
if(nazwakonta1 == nazwakonta && haslo1 == haslo){ /// checks if the acc name and password is matching.
cout<<"\nYou have signed in as: "; ///if it does ,a message that you have logged in shows up.
cout<< nazwakonta;}
else{
cout<<"The account name and password does not match.\n"; /// if it does not match, an invalid acc name or pass message is displayed.
cin.get();}
break;
case 3:
wyjscie(); /// choosing to quit the program.
break;
}}
My problem is that i don't know how to save the acc name & password and how to load it up again so you can actually login onto a account you made earlier.
Yeah but could You show me an example on how it can be done on my code or a code similar to mine??
Because how do i say what directory the password and username has to be saved into?
and where should i put the fstream thing in?
Line 3:
FILE_PATH can be filled in, or left out. If you just have a filename, i.e. file.txt. It will be created/saved to the same directory as your .exe.
i chose to type in the pass.txt in the third line of ur code and copied he rest of the code as t is and got 2 errors.
line 7 :error :empty character constant
in function 'void save(std::sting, std:: string)';
line 3 :error :variable 'std::ofstream file' has initialized but incomplee type
==build finished: 2 errors, 0warnings ==
i cant know where i went wrong because i never used this function before whatsoever.
any ideas?
i didnt know that i have to add the include fstream directive .but its ok now.when the user chooses case 1,it saves the password and username in a file.
BUT look at case 2. of my code.
i made anoter case which opens the file and i dont know how to tell the program to read the fileand compare the accounts and ppassword in it to the one which user has typed in.
Also when i make the same accounts ,they accumulate.how to tell the program to not allow to make a username which already exists??
edit:when i added file<< nazwakonta and then underneath i added file<<haslo;
it only tries to compare the first account name and password to the input,completly ignoring the rest of the file.
getline( file, username, ' ' ); //get from file, and store in username everything up until the ' '(space)
getline( file, password, '\n' ); //get all after the space, up until the newline character, store in password
And for using file, you need to use:
ifstream - input.
ofstream - output.
fstream - for use with both.
void load( std::string u[], std::string p[], constint M )
{
std::ifstream file( "log", std::ios::in );
std::string input;
if( file.is_open() )
{
//while i is < max users && it's not the end of the file
for( int i = 0; i < MAX, ( ! file.eof() ); ++i )
{
std::getline( file, input, ' ' );
u[ i ] = input;
std::getline( file, input, '\n' );
p[ i ] = input;
}
}
else
std::cout << "\n\tUnable to open file to load values...\n";
}
This would load all available lines in the file to be loaded in to string arrays.
Also, to make sure that there are no duplicate names. When you create a user, before saving it, use a for loop and make sure that the name doesn't match another name. Which should already be loaded in to arrays.
EDIT:
1 2 3 4
constint MAX = 5;
user[ MAX ];
pass[ MAX ];
Is why I am passing constint M to the function. So I don't run passed the size of the array. Access Violation error.
#include <iostream>
#include <fstream>
usingnamespace std;
string nazwakonta , nazwakonta1 , haslo, haslo1 ;
void save( std::string u[], std::string p[], constint M )
{
constint MAX = 5;
nazwakonta[ MAX ];
haslo[ MAX ];
std::ifstream file( "log", std::ios::in );
std::string input;
if( file.is_open() ){
for( int i = 0; i < MAX, ( ! file.eof() ); ++i )
{std::getline( file, input, ' ' );
u[ i ] = input;
std::getline( file, input, '\n' );
p[ i ] = input;
}
}
else
std::cout << "\n\tUnable to open file to load values...\n";
}
void check()
{
ifstream file("pass.txt");
file>> nazwakonta;
file>>haslo;
}
short wybor;
void login(){
cout<<"Log in to your account;";}
void rejestracja(){
cout<<"Register your account";}
void wyjscie(){
cout<<"Exit";}
int main(){
cout<<"1. Register\n";
cout<<"2. Log in\n";
cout<<"3. EXIT\n";
cout<<"Selection: ";
cin>>wybor;
switch(wybor){
case 1:
rejestracja();
cout<<"\nPlease enter your new account name. ";
cin>> nazwakonta;
cout<<"\nPlease enter your new password. ";
cin>>haslo;
save();
cout<<"Please remember your account information which is shown below:\n";
cout <<"Account: "<< nazwakonta <<"\n" <<"Password: "<< haslo<<"\n";
cout<<"Please log in now.\n";
cout<<"Account Name: ";
cin>> nazwakonta1;
cout<<"\nPassword: ";
cin>> haslo1;
if(nazwakonta1 == nazwakonta && haslo1 == haslo){
cout<<"\nYou have signed in as: ";
cout<< nazwakonta;
cin.get();}
else{
cout<<"The account name and password does not match.\n";
cin.get();}
break;
case 2:
check();
login();
cout<<"Account Name: ";
cin>> nazwakonta1;
cout<<"\nPassword: ";
cin>> haslo1;
if(nazwakonta1 == nazwakonta && haslo1 == haslo){
cout<<"\nYou have signed in as: ";
cout<< nazwakonta;
cin.get();}
else{
cout<<"The account name and password does not match.\n";
cin.get();}
break;
case 3:
wyjscie();
break;
}
cin.get();
return 0;}
main.cpp||In function 'void save(std::string*, std::string*, int)':|
main.cpp|19|warning: left-hand operand of comma has no effect|
main.cpp||In function 'int main()':|
main.cpp|8|error: too few arguments to function 'void save(std::string*, std::string*, int)'|
main.cpp|66|error: at this point in file|
||=== Build finished: 2 errors, 1 warnings ===|
I declare this so that I can use it in loops etc. so that I never run passed the end of the array. If you have done this, then send this to functions too!
Line 66, you pass nothing to "save();" When it's expecting save( string *, string *, constint )
Within the save function, everything you are doing is loading. Instead of saving to an Ofstream object, you are loading getline( ... ) from an Ifstream object.
Line 8:
I declared the const int MAX within another function, so I passed it to the save function! If you're declaring the array size within the save function, there is no need to pass a const int. Although, if you're using a const int to declare an array size, you might as well pass it to the function, in case you change it from where you first declare it. This will cause Access Violation Errors!
Lines 11-13:
You've declared the usernames and passwords... These should be passed to the function! When you declare them in that function and pass it nothing, they are garbage values! No where near what you'd expect.
int main()
{
//const int for usernames and passwords arrays
constint MAX = 20;
//create an array of strings( 20 ) - 20 users, 20 passwords
//init to NULL ( \0 )
std::string username[ MAX ] = { "\0" };
std::string password[ MAX ] = { "\0" };
//load from file
load( username, password, MAX );
//view usernames and passwords from the file
view( username, password, MAX );
return 0;
}
void load( std::string u[], std::string p[], constint M )
{
//create an in file
std::ifstream file( "log", std::ios::in );
std::string input;
if( file.is_open() )
{
//while i is < max users && it's not the end of the file
for( int i = 0; i < M, ( ! file.eof() ); ++i )
{
//get the line up until the space
//and store in the username array
std::getline( file, input, ' ' );
u[ i ] = input;
//get the line up until the space
//and store in the password array
std::getline( file, input, '\n' );
p[ i ] = input;
}
}
else
std::cout << "\n\tUnable to open file to load values...\n";
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
void view( std::string u[], std::string p[], constint M )
{
std::cout << '\n';
for( int i = 0; i < M; ++i )
{
//if u[ i ] is empty( "\0" in main() ) break from the loop
if( u[ i ].empty() )
break;
std::cout << "\tUsername: " << u[ i ] << '\n';
std::cout << "\tPassword: " << p[ i ] << "\n\n";
}
//just a function I created to pause the console
wait();
}
In main, line 12, see how I pass to the function. That's how you have to pass them!