Problem with c++ program

I've been trying for a while to create like an offline bank that will keep the amount of money each person has will be able to add or subtract money from the account and will have a personal account for each person that uses it.
Here is where my problem comes. How do I make my program create a text file every time some1 creates an account to keep their account's information there.
My code is the following:
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
#include <iostream>
#include <fstream>
#include <string>


using namespace std;

int main()
{
    ifstream accounts;
    string accName;
    string STRING;
    int choice;
    ofstream regAcc;
    string newAcc;
    
    cout << "\tWelcome to the Offline Virtual Bank" <<endl;
    cout << "Here you will be able to keep a constant record of what you have in your bank\naccount completely offline without";
    cout << " the need of an internet connection.\n\n";
    cout << "1-Login" <<endl;
    cout << "2-Register" <<endl;
    cout << "3-exit" <<endl;
    cout << "To begin, what would you like to do: ";
    cin >> choice;
    switch (choice){
           case 1:
    system ("cls");
    cout << "Now enter your login information: ";
    cin >> accName;

    accounts.open ("accounts.txt");
             {
	        getline(accounts,STRING);
	                         }
	accounts.close();
	
    if (STRING.compare(accName) == 0)
                       cout << "Welcome "<< accName << " what would you like to do" <<endl;
                       
                       else 
                       cout << "Sorry, invalid login information" <<endl;
    break;
    case 2:
    cout << "What would you like your username to be (note:will be needed to login): ";
    cin >> newAcc;
    regAcc.open ("accounts.txt");
    regAcc << newAcc;
    regAcc.close();
    cout << "Account registration successful. Restart the program to login with your new account." <<endl;
    break;
    case 3:
               cout << "Thanks for using the Offline Virtual Bank, hope you come back later" <<endl;  
               cout << "Press enter to exit" << endl;                 
cin.ignore();
cin.ignore();
         exit (1);
         break;

          }
      cout << "Thanks for using the Offline Virtual Bank, hope you come back later" << endl; 
      cout << "Press enter to exit" << endl;                
cin.ignore();
cin.ignore();
    return 0;
}
     


I know its not complete (its missing a lot) but I'm stuck in the part where the user creates the account and the program creates a text file for that account currently also as u can see it uses accounts.txt that's the one I'm using while i figure a way to create a different one for each account.
the text file would be like example.txt for a user whose user name is example.
Is there any other way of doing this besides the way i did it? (im kind of new so i searched what i wanted and used different methods i found online)
if so what other ways can i use to create files based on what a user inputs?

Finally, if you need more information on what I exactly want or something similar tell me even though i think Ive explained quite well what i need.

(case 2 is the problem)
Last edited on
Two ways to open a file, seeing as you use fstream then you probably want to use the c++ style, simply open for writing and you will generate a file if it does not already exist.
You don't get it, I know how to create a file but what i don't know is how to create a file with the name being "newAcc" instead of accounts (note:newAcc is the string that stores the user name the user would like to have) so in conclusion it would be newAcc.txt example: jufus09.txt
Yes. If you have their name in an std::string, then you can just do something like this:

1
2
3
std::string name = "joe";
name += ".txt"; //add .txt on the end, not required
std::ifstream file(name.c_str()); //open the file 
Well what you can do is retrieve the c style string. Like string.c_str(). open it by calling file.fopen( string.c_str() ). If you need to add an extension like .txt you can append to the string ".txt" before making your fopen call. You can do that using a couple different string member functions.
I did that but now i get a new error and a problem with case 3
I guess I'll stop for now do something else and once i get more used to working with I/O start it all over again.
Thanks for the help though
Topic archived. No new replies allowed.