Find Specific line in csv

Hi. I've created a log in program that allows the user to first, create an account, then log in using the same username and password. when they try to log in with their username and password, I need the program to find that username and password in the csv file and match the two passwords. in the csv file, the username is on one row ( User: ) and the password on the row under ( pass: ), all the same column.
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
#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;
    cout << "Enter New Password: ";
    cin >> s2;
    cout << "Re-Enter Password: ";
    cin >> s3;
    if (s2==s3) {
        MyFile << "User: ";
        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;
    cout << "Enter Password: ";
    cin >> s2;
    ifstream MyFile;
    MyFile.open("User.csv", ios::in); // open file
if(MyFile) {
  string s="";
  while(getline(MyFile, s)) {
    cout<<s<<endl;
  }
}
        }
}


the getline function at the end displays all username's and password's. I need to match the one specific password underneath the input username. Thanks
A simple note on syntax. It is much nicer to list the {} on seperate lines and have the stuff inbetween incremented by 3 spaces.
1
2
3
4
5
6
7
8
9
10
if
{
   stuff
   stuff
}
else
{
   stuff
   stuff
}

Simply easier to read. No worries though.
Looking at your code you run the chance of not closing your file. If you put in 1 it doesn't close. If you put in 2 it closes a not open file.
3 spaces is stupid lol just use a tab.
thanks for the tip on syntax, but it doesn't really answer my question. I need to find and read the password from the csv file.
I do use a tab like edithsong's example. must've forgot one line. Thanks for the tips.
I think you would benefit from having functions to show the menu and to process the users selection. then have a function to carry out each menuoption.

There are problems with the braces, causing you to not get the logic you want.

Also do some design like this with comments:

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
//includes

//using

//function declarations

int main() {
//ShowMenu
//ProcessMenu


return 0;
}
//function definitions
//ShowMenu
{

}
//ProcessMenu
{

}

//CreateAccount
{

}

//Login
{

}

//Exit
{

}



You can put more deataild comments in the functions which show how you are going to do it.

Then go through and write the code that does what's written in the comments.

This is good basic code design practice and most novice programmers seem to miss out on it altogether.
Last edited on
Topic archived. No new replies allowed.