Simple account manager problem

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.

this is the code i came up whith:

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
#include <iostream>
using namespace 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.

Thanks!
Save to a file and load when the program is run?
http://www.cplusplus.com/reference/iostream/fstream/fstream/
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?

thanks.
You could just create a funtion for loading/saving and call it when you need one or the other.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void save( string user, string password )
{
    ofstream file( "FILE_PATH\\pass.txt", ios::out );

    if( file.is_open() )
    {
        file << user << ' ' << password << '\n'; //write to file

        file.close();
    }
    else
    {
        cout << "Unable to open file" << '\n';
    }
}


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.
Last edited on
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 don't see how you have errors, I just ran it and it worked fine!

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
#include <iostream>
#include <string>
#include <fstream>
using namespace std;


void save( string user, string password )
{
	ofstream file( "pass.txt", ios::out );

	if( file.is_open() )
	{
		file << user << ' ' << password << '\n'; //write to file

		file.close();
	}
	else
	{
		cout << "Unable to open file" << '\n';
	}
}

int main()
{
	string p, u;

	u = "Dave";
	p = "723js";

	save( u, p );
   
	return 0;
}
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.
Last edited on
The way it's saved in the file:
Dave 723js


Use
1
2
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.
But would the program search all the text file for a matching pass and acc name?

and could you tell me how to tell he program to disallow a user to create 2 the same account names?
Well, you would load them all in to a string array:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void load( std::string u[], std::string p[], const int 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
const int MAX = 5;

user[ MAX ];
pass[ MAX ];


Is why I am passing const int M to the function. So I don't run passed the size of the array. Access Violation error.
Last edited on
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
#include <iostream>
#include <fstream>
using namespace std;

string nazwakonta , nazwakonta1 , haslo, haslo1 ;


void save( std::string u[], std::string p[], const int M )
{

const int 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 ===|

Could someone tell me what im doing wrong?
Last edited on
As above:
1
2
3
4
const int MAX = 5;

user[ MAX ];
pass[ MAX ];


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!
look at my code: line 10-14

what do you mean by: send this to functions too!
Last edited on
what do you mean by: send this to functions too!
Pass to functions.

Line 66, you pass nothing to "save();" When it's expecting save( string *, string *, const int )

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.
Last edited on
save( string *, string *, const int )

do i have to change something in that or leave it as it is?
why there are 2x strings? And what is this const int?

Maybie im just tired? ^^
I'll just show you my code for this! lol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int main()
{
	//const int for usernames and passwords arrays
	const int 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;
}


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
void load( std::string u[], std::string p[], const int 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[], const int 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!
Last edited on
Thanks alot!

I think ill make my program again from scratch.
ill post it all here when its done so other pple would know how its done.
No problem, and good luck!
Topic archived. No new replies allowed.