C++ I need help reading a file with rows and columns | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
0

C++ I need help reading a file with rows and columns

I'm able to output all the rows and columns from the file but what I need is to have the user input a name and if it matches a column name from the text file it will output that particular name.

7th Dec 2020, 1:01 AM
Julie Ann
Julie Ann - avatar
5 Antworten
+ 3
Julie Ann there are numerous ways to do this. one way is: a) create a class with 3 attributes id, fname, lname. b) use std::getline() and construct the object and put them in a std::vector c) write a function /operator that returns f/lname based on query. second way: a) use std::getline() to get each line. b) split the line c) use a std::map d) apply query to get the result
7th Dec 2020, 2:07 AM
Flash
+ 2
Julie Ann if all the lines are formatted the same you can use a vector of structs and use getline to read each line and parse the data
7th Dec 2020, 5:55 AM
Piyush
Piyush - avatar
+ 1
Maybe someone here can help you, your chances for answer may increase if you just attach a saved code link in the Description. Including file content format may also help others to understand your situation and assist you accordingly.
7th Dec 2020, 1:07 AM
Ipang
+ 1
Text file contains 3 rows with id, first name, last name 1 Sam Smith 2 Jane Wilson 3 Tom Craft User input a name and if it matches the last name of any of the names from the text file the first name will output. User inputs Jane Output is Wilson
7th Dec 2020, 1:18 AM
Julie Ann
Julie Ann - avatar
+ 1
Assuming variable <row> represents a line, and <word> is the string to find, you can try make a loop to check the rows as follows: if( <row>.find( <word> ) != std::string::npos ) std::cout << row << std::endl; You may prefer regular expression, but Idk enough of it to suggest. Wait for others' opinion ...
7th Dec 2020, 1:28 AM
Ipang