// reading a text file
#include <iostream>
#include <fstream>
#include <string>
usingnamespace std;
int main () {
string line;
ifstream myfile ("example.txt");
if (myfile.is_open())
{
while (! myfile.eof() )
{
getline (myfile,line);
if (line = something)
{
// read next line and print it... but how?
}
myfile.close();
}
else cout << "Unable to open file";
return 0;
}
Your if statement is incorrect. You probably meant == and not just =; also the getline() has already read the line into line; if you want to read the next line, just use getline() again. To print it out, just use std::cout.
how can i make it so if it finds any one of those 3 words it prints the next line? right now i just have one word and it works but i need different keywords. PS and how do i make it not cap sensitive?
like if it finds banana it types 1 or MOuSe it types 2
when i do this i declare a counter in my if statement then loop the counter in my while condition. Then i say in my final if statment if condition is true i grab that counter and then getline() and display its current line
now with my code the user enters in say 3. i need it to print out hello
or if they enter in 10 i need it to print hola
now i cant use pseudocode because the .txt file will be different everytime. so i guess what im asking for is a way to search the text file for a word. and if that word is anywhere in that line then it displays the next line.