Multiple account login problems

How can I be able to use more than one account? Please Help!
**Updated**
Code:
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#include <iostream>
#include <string>
#include <fstream>
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <cstdlib>
#include <string.h>
#include <windows.h>
#include <sstream>
using namespace std;

void menu();
void userinput();
void accountinput();
void accountlogin();
void Deposit();
void Withdraw();
void balanceholder();
void seconds_count();

double answer,deposit, withdraw,balance=0,chances = 3,Id,pass;
int choice;
const int MAX_WRONG = 5;
char cChoice;
string Pass, ID,Balance,s,IDin,Passin;
string username;
string password;

int main()
{
system ("color 2f");
printf("Do you want to register y/n -> ");
                cin>>cChoice;
                printf("\n");

                if(cChoice=='Y'||cChoice=='y')
                {
    ofstream myfile ("IDs_file.txt");
    if (myfile.is_open())
    {
    printf("\nEnter your new Pin -> ");
    cin>>ID;
    myfile << ID;
    printf("\nEnter your new Password -> ");
    cin>>Pass;
    myfile << ":" << Pass<<"\n";
    myfile.close();
    }
    else cout << "Unable to open file";
printf("\n\nRestart the program for the new pin and password to take effect\n\n");
system("pause");
return 0;}
accountlogin();
accountinput();
}
void accountlogin()
{
    ifstream myfile("IDs_file.txt");
    if(!myfile)
    {
        cerr << "Error opening ID input file.";
    }
    if(getline(myfile, username, ':') >> password){
    int wrong = 0;
    string userChoice;
    string userChoice2;
SYSTEM_LOOP:
    system ("cls");
    cout << "Username : " << endl;
    cin >> userChoice;
    cout<<endl;

    cout << "Password : " << endl;
    cin >> userChoice2;
    cout<<endl;

    if (userChoice == username && userChoice2 == password)
    {
        cout<<"correct !"<<endl;
        system ("pause");
        system ("cls");
    }
    else if (userChoice != username && userChoice2 != password)
    {
        cout<<"incorrect !"<<endl;
        wrong++;
        system ("pause");

        if (wrong == MAX_WRONG)
        {
            seconds_count();
        }
        goto SYSTEM_LOOP;
    }
    else if (userChoice == username && userChoice2 != password)
    {
        cout<<"incorrect !"<<endl<<endl;
        system ("pause");
        goto SYSTEM_LOOP;
    }
    else if (userChoice != username && userChoice2 == password)
    {
        cout<<"incorrect !"<<endl<<endl;
        system ("pause");
        goto SYSTEM_LOOP;
    }
}}
void accountinput()
{
  ifstream myfile("Balance.txt");
    if(!myfile){cerr << "Error opening ID input file.";}
    string data;
    getline(myfile, data, ':') >> balance;
    if(data==username)
    {

    }
    myfile.close();
userinput();
}
void userinput()
{
while (choice != 4)
{
cout << "Welcome to jasonwynn10's ATM Machine\n";
cout << "[1]Display Balance \n";
cout << "[2]Deposit Money\n";
cout << "[3]Withdraw Money\n";
cout << "[4]Exit\n";
cout << "Enter your Choice:";
cin >> choice;
switch (choice)
{
case 1:
cout << "Your Balance is :" <<balance<< endl;
system("pause");
system("cls");
break;
case 2:
Deposit();
break;
case 3:
Withdraw();
break;
case 4:
cout << "Your Balance is :" <<balance<< endl;
balanceholder();
system("pause");
break;
default:
cout<<"Say Again? \n> ";
cin >> choice;
break;
}
}
}
void Deposit()
{
cout << "Your Current Balance is " << balance << endl;
cout << "How much Money you want to deposit:" << endl;
cin >> deposit;
balance = balance + deposit;
cout << "Your new balance is :" << endl;
cout << balance;
cout << endl;
system("pause");
system("cls");
userinput();

}
void Withdraw()
{
cout << "Your Current Balance is " << balance << endl;
cout << "How much Money do you want to withdraw:" << endl;
cin >> withdraw;
if (withdraw > balance)
cout << "Sorry you don't have enough money to withdraw" << endl;
else if (withdraw <= 0)
cout << "sorry amount can not be zero or negative" << endl;
else
balance = balance - withdraw;
cout << "You Withdraw :" << withdraw << endl;
cout << "Your new balance is :" << endl;
cout << balance<<endl;
system("pause");
system("cls");
userinput();
}
void balanceholder()
{
    ofstream myfile ("Balance.txt");
    if (myfile.is_open())
    {
    myfile <<username<<":"<<balance;
    myfile.close();}
    else cout << "Unable to open file";
}
void seconds_count()
{
    for (int c = 30 ; c > 0 ; c--)
    {
        cout<<"Please wait "<<c<<" seconds"<<endl;
        Sleep (1000);
        system ("cls");
    }
}

IDs_file.txt:
Id: 1911
Pass: 1234
Id: 2202
Pass: 4452

Balance.txt:
1911: 1234332
2202: 2253
Last edited on
anyone there?
ow could one create a Login system using my program I started in the accountlogin() function.


Probably the easiest thing to do would be to start over from the beginning. With all the bad practices demonstrated in the code you supplied along with the horrible, shall I say lack of any, indentation that makes reading the program a chore, starting over is the best option IMO.

Before you start screaming "What bad practices", start with your includes, here your mixing C++ and C standard headers. If you're going to write a C++ program stick with the C++ standard headers (<cstdlib>) and leave the C standard headers (<stdlib.h>) home.

Next stop using the global variables, put the variables into the correct functions and pass the required variables to and from your functions.

Next stop mixing C-stdio functions and C++ streams this seems to be a C++ program so stick with the C++ streams.

I didn't build this entire program so I don't know what I can and can't remove some parts of the program that he made, and so I will not remove the standard C headers. Here's a code update aswell.
there is a problem on line 68. I don't understand it. can anyone help me?
What don't you understand? Is balance a std::string? The getline() function requires a std::string for it's second argument.

then how do you convert a string to a double?
Assuming a C++11 compiler, here is an example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <string>
#include <fstream>

int main() {
    std::fstream myfile("file.txt");

    std::string line;
    std::getline(myfile, line);

    double val = std::stod(line);

    // or you could just do this (which is easier and makes more sense): 
    myfile >> val; 
}


So, for your case the best solution for line 68 (and the other places) is probably this:
while(std::cin >> balance)
Last edited on
My compiler does not accept stod(), I've tried before. But I found a solution. I used stringstream.
I need to be able to read the file using Id: and Pass: as a guide to where the inputs are. any help?
Why do you need to convert the string to a double? Why not just extract the double from the file. First extract the text to a string, ie Id: then extract the double.

example:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int main()
{
    ifstream myfile("IDs_file.txt");
    if(!myfile)
    {
        cerr << "Error opening ID input file.";
        return(10);
    }

    string data;
    int number; 
    if(getline(myfile, data, ':') >> number) 
    {
        // now use the data however you want!
       if( data == "Id")
          this is the Id number.
       else
           this is the Pin number.

    }


thanks, I really appreciate the help!
What about the balance.txt file? same thing?
You should be able to use the extraction operator to extract the first number, a trash character (:) and then the last number.

1
2
3
4
5
6
int Id;
char trash;
double balance;

YourFileStreamName >> Id >> trash >> balance;
ok, now I need a way to be able to have more than one account usable, and I also need to be able to pull up their balances.
Once again, Thank you for your help.

Code update:
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#include <iostream>
#include <string>
#include <fstream>
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <cstdlib>
#include <string.h>
#include <windows.h>
#include <sstream>
using namespace std;

void menu();
void userinput();
void accountinput();
void accountlogin();
void Deposit();
void Withdraw();
void balanceholder();
void seconds_count();

double answer,deposit, withdraw,balance=0,chances = 3,Id,pass;
int choice;
const int MAX_WRONG = 5;
char cChoice;
string Pass, ID,Balance,s,IDin,Passin;
string username;
string password;

int main()
{
system ("color 2f");
printf("Do you want to register y/n -> ");
                cin>>cChoice;
                printf("\n");

                if(cChoice=='Y'||cChoice=='y')
                {
    ofstream myfile ("IDs_file.txt");
    if (myfile.is_open())
    {
    printf("\nEnter your new Pin -> ");
    cin>>ID;
    myfile << ID;
    printf("\nEnter your new Password -> ");
    cin>>Pass;
    myfile << ":" << Pass<<"\n";
    myfile.close();
    }
    else cout << "Unable to open file";
printf("\n\nRestart the program for the new pin and password to take effect\n\n");
system("pause");
return 0;}
accountlogin();
accountinput();
}
void accountlogin()
{
    ifstream myfile("IDs_file.txt");
    if(!myfile)
    {
        cerr << "Error opening ID input file.";
    }
    if(getline(myfile, username, ':') >> password){
    int wrong = 0;
    string userChoice;
    string userChoice2;
SYSTEM_LOOP:
    system ("cls");
    cout << "Username : " << endl;
    cin >> userChoice;
    cout<<endl;

    cout << "Password : " << endl;
    cin >> userChoice2;
    cout<<endl;

    if (userChoice == username && userChoice2 == password)
    {
        cout<<"correct !"<<endl;
        system ("pause");
        system ("cls");
    }
    else if (userChoice != username && userChoice2 != password)
    {
        cout<<"incorrect !"<<endl;
        wrong++;
        system ("pause");

        if (wrong == MAX_WRONG)
        {
            seconds_count();
        }
        goto SYSTEM_LOOP;
    }
    else if (userChoice == username && userChoice2 != password)
    {
        cout<<"incorrect !"<<endl<<endl;
        system ("pause");
        goto SYSTEM_LOOP;
    }
    else if (userChoice != username && userChoice2 == password)
    {
        cout<<"incorrect !"<<endl<<endl;
        system ("pause");
        goto SYSTEM_LOOP;
    }
}}
void accountinput()
{
  ifstream myfile("Balance.txt");
    if(!myfile){cerr << "Error opening ID input file.";}
    string data;
    getline(myfile, data, ':') >> balance;
    if(data==username)
    {

    }
    myfile.close();
userinput();
}
void userinput()
{
while (choice != 4)
{
cout << "Welcome to jasonwynn10's ATM Machine\n";
cout << "[1]Display Balance \n";
cout << "[2]Deposit Money\n";
cout << "[3]Withdraw Money\n";
cout << "[4]Exit\n";
cout << "Enter your Choice:";
cin >> choice;
switch (choice)
{
case 1:
cout << "Your Balance is :" <<balance<< endl;
system("pause");
system("cls");
break;
case 2:
Deposit();
break;
case 3:
Withdraw();
break;
case 4:
cout << "Your Balance is :" <<balance<< endl;
balanceholder();
system("pause");
break;
default:
cout<<"Say Again? \n> ";
cin >> choice;
break;
}
}
}
void Deposit()
{
cout << "Your Current Balance is " << balance << endl;
cout << "How much Money you want to deposit:" << endl;
cin >> deposit;
balance = balance + deposit;
cout << "Your new balance is :" << endl;
cout << balance;
cout << endl;
system("pause");
system("cls");
userinput();

}
void Withdraw()
{
cout << "Your Current Balance is " << balance << endl;
cout << "How much Money do you want to withdraw:" << endl;
cin >> withdraw;
if (withdraw > balance)
cout << "Sorry you don't have enough money to withdraw" << endl;
else if (withdraw <= 0)
cout << "sorry amount can not be zero or negative" << endl;
else
balance = balance - withdraw;
cout << "You Withdraw :" << withdraw << endl;
cout << "Your new balance is :" << endl;
cout << balance<<endl;
system("pause");
system("cls");
userinput();
}
void balanceholder()
{
    ofstream myfile ("Balance.txt");
    if (myfile.is_open())
    {
    myfile <<username<<":"<<balance;
    myfile.close();}
    else cout << "Unable to open file";
}
void seconds_count()
{
    for (int c = 30 ; c > 0 ; c--)
    {
        cout<<"Please wait "<<c<<" seconds"<<endl;
        Sleep (1000);
        system ("cls");
    }
}
Topic archived. No new replies allowed.