Login with txt writing/reading

I have to make a program that asks for your username (any)to register it. When you type it it will say something. I want to create a txt file also. so this is what I have:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <fstream>

using namespace std;

int main()
{


int a;

{
    ofstream myfile;
    myfile.open ("tareaf.txt");


        cout<<"Create your username;
    cin>>a;

myfile.close();
    return 0;
}
} 


now, how can I use the cout again to print something "Welcome to the program..."?
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
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
    string username;

    ofstream myfile;
    myfile.open ("tareaf.txt");
 
    cout << "Welcome to the program..." << endl << endl;

    if(myfile.is_open())
    {
         cout << "Create your username : ";

         getline(cin, username);
         
         myflile << username;

         myfile.close();
     }

    return 0;
}


the file must already exist in the same directory has the rest of the project files, there are parameters you can pass too to either start at the beginning of the file, append the content to the actual content of the file, or ios::trunc to delete the previous content of the file... default is beginning of file.

http://www.cplusplus.com/doc/tutorial/files/
Last edited on
Topic archived. No new replies allowed.