best way to get user input and search a .csv for it

Hello
I have a .csv file about 3 columns and more than 2000 rows (and growing). I need to write a program to get input from user and search the .csv file for that input, display the input and everything on that line.
.csv file example: street names , location , type of controller
1- alta & jones , NW , apogee
2- alta & pecos , SW , next phase

and it goes on and on for 2000 rows.
I need to ask the user which streets he wants to look up, and the program should display the whole line.
example of user input and program output:
user inputs (street names): alta & jones
program output: alta & jones , NW , apogee

This is what I have so far (thanks to cplusplus forum experts) and all this program does is it will print to display everything on the .csv file, Oh and did I mention that I was an absolute beginner.

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
  #include <fstream>
#include <iostream>
#include <sstream>
#include <string>


using namespace std;
int main()
{
    string a, b, c, location;
    char answer = 'n';
    while (answer == 'n' || answer == 'N')
{
    cout << " \n give me first street name: ";
    cin >> a;
    cout << " \n give me second street name: ";
    cin >> b;
    c = a + " & " + b;
    cout << c  ;
    cout << " \n is that right (y/n) " << endl;
    cin >> answer;
    if (answer == 'y' || answer == 'y')
    {
        cout<<"  ok  " << endl;
    }
    else
    {
        cout<<" oh no  " << endl;
    }

    std::string line_;
    ifstream  file_("cab location1.csv");
    if(file_.is_open())
    {
       while(getline(file_,line_))
        {
              std::cout<<line_<< '\n';
        }
     file_.close();
    }
 else
    std::cout<<"file is not open "<< '\n';
 std::cin.get();
 }
}

Again any help is greatly appreciated.
the commas are a bit funnily placed, not exactly next to the words but with a space in b/w - i suppose it's a feature of the .csv format? anyways, you can play around with the following and see if it fulfills your needs:
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
#include <iostream>
#include <algorithm>
#include <fstream>
#include <sstream>
#include <string>

int main()
{
    std::cout << "Enter query \n";
    std::string query{};
    getline(std::cin, query);

    std::ifstream inFile{"D:\\test.csv"};
    while (inFile)
    {
        std::string line{};
        getline(inFile, line);
        std::istringstream stream{line};
        std::string dummy{}, street_name{}, location{}, controller{};
        getline(stream, dummy, ' ')
            && getline(stream, street_name, ',')
                && getline(stream, location, ',')
                    && getline(stream, controller);
       // std::cout << street_name << " " << location << " " << controller << "\n";
        if(street_name.size() > 0) street_name.pop_back();
        if(inFile && (query == street_name))
        {
            std::cout << street_name << "," << location << "," << controller << "\n";
        }
    }
}
Hi Gunner, thanks for your reply.
I am getting an error that says:
line 10,13,16,18,19 "error: in C++98 'query' (<- that word is different on every line) must be initialized by constructor, not by '(...)'
query on line 10
infile on line 13
line on line 16
stream on line 18
dummy, street_name, controller, and location on line 19

I am using code::blocks for my compiler.
C++98
... the out-dated compiler is the stumbling block here. in all lines, barring 13 and 18 remove the opening and closing braces altogether. on lines 13, 18 replace the braces with corresponding parentheses and hopefully that should work
I replaced all the braces with parentheses (only where they are needed)
now I'm getting different errors, here is one of them:
line 11 - error: no matching function for call to 'getline(std::istream, std::string (&)())'
same one on line 17, 20, 21, 22, 23
on line 18 - error: no matching function for call to 'std::basic_istringstream<char>::basic_istring (&)())'
i've tried to de-C++11 the program as much as possible:
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
#include <iostream>
#include <algorithm>
#include <fstream>
#include <sstream>
#include <string>

int main()
{
    std::cout << "Enter query \n";
    std::string query;
    getline(std::cin, query);

    std::ifstream inFile("D:\\test.csv");
    while (inFile)
    {
        std::string line;
        getline(inFile, line);
        std::istringstream stream{line};
        std::string dummy, street_name, location, controller;
        getline(stream, dummy, ' ')
            && getline(stream, street_name, ',')
                && getline(stream, location, ',')
                    && getline(stream, controller);
       // std::cout << street_name << " " << location << " " << controller << "\n";
        if(street_name.size() > 0) street_name.pop_back();
        if(inFile && (query == street_name))
        {
            std::cout << street_name << "," << location << "," << controller << "\n";
        }
    }
}

just rechecked above and it works fine; not sure i want to go more retro than this, at some point you need to get an up-to-date compiler
Ok that took care of all the other problems.
the only one I still have left is on line 25 error: 'std::string' has no member named 'pop_back'
I am running the latest version of code blocks version 16.01.
is there a better compiler, if so can you give me the name.
thanks again for all your help.
C::B should have C++11 – go to Settings → Compiler → Compiler Settings → Compiler Flags and check the box that says: Have g++ follow the C++11 ISO … etc
I updated my compiler from MinGW to TDM-GCC-32 and that fixed the error with C::B not recognizing pop.back. Now when I do the build on the program I am not getting any errors.
But when I run the program, it will ask me for query then it will return nothing. No matter what I put in the query.
If I put something that is not on the .csv file it will not loop back.
Topic archived. No new replies allowed.