Hi. As example i wrote a simple program which says "Put in any word" and then the word gets read. How can i check fot a special thing what the user wrote? for example if the user writes the word "example test" and i want that something happens at test. How can i divide that? I want that the program runs normal and if ever the word test gets iput the program should do something
#include <iostream>
#include <string>
#include <fstream>
usingnamespace std;
int main()
{
string line;
ifstream myfile("test.txt");
// <--- Check if the stream is open and usable here and leave the program if it is not
if (!myfile) // <--- This is all you need.
{
// <--- An error message.
return 1;
}
while (getline(myfile, line))
{
cout << line << '\n';
}
//myfile.close(); // <--- Not required as the dtor will close the file when the function looses scope.
return 0;
}
Nit sure what's being asked - but as a starter perhaps:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
include <iostream>
#include <string>
int main()
{
std::string text;
std::cout << "Put in any words: ";
std::getline(std::cin, text);
if (text.find("test") != std::string::npos) {
// test entered. Do special processing here
std::cout << "test entered\n";
}
}