• C++ Reading the line below
    3 replies, posted
I have created a program that you search all the users that have ever used the program, but you need to enter your details to search. It is all fine and not buggy, but I have one thing I would like to know how to do. I would like to know so that when it searches it will enter everything on the line the string was found and the line below. [CODE]inFile.open("data.txt"); cout << "\n"; cin.get(entr); //dont worry about this. this is just something you don't need to know about. cout << "\nYou may now search a user\n"; cout << "\nEnter a students first or last name ((Case sensitive))\n"; getline (cin, search); cout << "\n Searching for " << search <<"\n"; { size_t pos; while(inFile.good()) { getline(inFile,line); // get line from file pos=line.find(search); // search if(pos!=string::npos) // string::npos is returned if string is not found { cout << search << " Found!"; //this is where I want it to display the line the string was found and the line below, because the line below has the age. break; } }[/CODE]
You already have code that handles when the string was found, and you've already got an object for the line. You can write [I]line[/I] to your output to get the entire current line, call another [I]getline(inFile,line);[/I] (you may want to check if infile is still good, just to be safe), and then output [I]line [/I]again.
In a completely unrelated question, is there a reason you're using the newline char rather than std::endl? You're using the standard output library already, but I wanted to know if I am missing anything.
[QUOTE=TeamEnternode;48267512]In a completely unrelated question, is there a reason you're using the newline char rather than std::endl? You're using the standard output library already, but I wanted to know if I am missing anything.[/QUOTE] std::endl also flushes the buffer which isn't always needed really.
Sorry, you need to Log In to post a reply to this thread.