Write and read to/from file (CSV)

Hi, I'm pretty new to programming and currently making a program that stores username's and passwords in a csv file . it also allows the user to log in using a previously created username and password. At the moment, it runs fine but does not store any information.
Any help with writing ( create new account ) and then reading ( for log on ) would be great. Thanks.
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
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <windows.h>
#include <stdio.h>
#include <time.h>


using namespace std;

int main()
{
    cout << "Press 1 to create an account: ";
    cout << "" << endl << "";
    Sleep(150);
    cout << "Press 2 to log in: ";
    cout << "" << endl << "";
    Sleep(150);
    cout << "Press 3 to exit: ";
    cout << "" << endl << "";
    char c1;
    cin >> c1;
    cout << "\n";
    if (c1 == '1') {
    ofstream MyFile;
    MyFile.open ("Users.csv", ios::out | ios::ate | ios::app | ios::binary) ;
    string s1,s2;
    cout << "Enter New Username: ";
    cin >> s1;
    MyFile << "Username: ";
    MyFile << s1 << endl;
    MyFile.close();
    }
    if (c1 == '2') {cout << "Enter Username: ";
    string s1,s2;
    cin >> s1;
    }
}

Hi ACE123,

 
MyFile.open ("Users.csv", ios::out | ios::ate | ios::app | ios::binary) ;


Do really want to write toa binary file? Read up about streams.

Put the stuff that displays the menu into a function, and use a switch statement instead of if statements to do the processing.

The processing of each menu selection could also be functions.

You should check whether opening the file works.

Try to come with better names for your variables.


Hope all this helps.

TheIdeasMan
I took out the binary and changed a few things. It writes the information to the CSV file fine. But for the next stage ( the log in ), I need to read the password from the csv after the user has inputed their username and password. It then needs to cross check the input password with the password from the csv file.
Thanks IdeasMan for the help with the binary. I still don't understand switch statements, but I might read up about that later. Thanks

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
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <windows.h>
#include <stdio.h>
#include <time.h>


using namespace std;

int main()
{
    cout << "Press 1 to create an account: "; 
    cout << "" << endl << "";
    Sleep(150);
    cout << "Press 2 to log in: ";
    cout << "" << endl << "";
    Sleep(150);
    cout << "Press 3 to exit: ";
    cout << "" << endl << "";
    char c1;
    cin >> c1;
    cout << "\n";
    if (c1 == '1') {
    ofstream MyFile;
    MyFile.open ("User.csv", ios::out | ios::ate | ios::app) ;
    string s1,s2,s3;
    cout << "Enter New Username: ";
    cin >> s1;
    MyFile << "User: ";
    cout << "Enter New Password: ";
    cin >> s2;
    cout << "Re-Enter Password: ";
    cin >> s3;
    if (s2==s3) {
        MyFile << s1 << endl;
        MyFile << "Pass: ";
        MyFile << s2 << endl;
        cout << "Account Created";}
    else {cout << "Input Doesn't Match";}
    MyFile.close();
    }
    if (c1 == '2') {cout << "Enter Username: ";
    string s1,s2;
    cin >> s1;
    }
}

Here is a switch statement:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

switch ( MenuOption )
        {

            case 'A':
                UseCalculator();
                break;

            case 'B':
                PlayGuessingGame();
                break;

            case 'C':
                EnterText();
                break;

            default:
            cout << "'" << MenuOption << " Is not a choice." << endl;
            break;
        }



Obviously , it does a main menu. This code should be inside a ProcessMenu function. Notice how it calls functions to carry out each menu option.

What this means is that you can call ShowMenu and ProcessMenu functions in main , which then go off and call the other functions. This way, you can divide & conquer - and you won't have all the code in main.

Edit:

The ShowMenu function just prints the menu, nothing else.
Last edited on
Topic archived. No new replies allowed.