Check single words

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
Last edited on
Hello NMI21,

The question you should ask your-self is do I read until I find a period or until I find a new line (\n)?

Without seeing the input file, hint hint nudge nudge, I have no idea what you have to work with.

You could read a line from the file then search that string to see if there is a matching word.

If you need something more consider a vector of strings to hold each line that you read then work with that.

Consider this as a start:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <string>

#include <fstream>

using namespace 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;
}


Andy
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";
	}
}

Topic archived. No new replies allowed.