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.
5 Answers
+ 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
+ 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
+ 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.
+ 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
+ 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 ...