next word of input

I'm curious about reading in a line of input until certain words are reached and then saving the NEXT word/number of input into a variable. How would one go about doing this with stringstreams, if there is a way?
Something like this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <string>

const std::string toRead = "Read me x now!";

int main()
{
     std::string output = "";
     bool hitchar = false;
     for(unsigned int i = 0; i < toRead.size(); i++)
     {
          if(toRead[i] == 'x')
              hitchar = true;

          if(hitchar)
              output.append(toRead[i]);
     }

}


Something like that. Haven't compiled it but it should work with little to no issues.
Last edited on
closed account (EwCjE3v7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <string>

const std::string toRead = "Read me x now!"; // why this, this is bad style of coding

int main()
{
     std::string output = "";
     bool hitchar = false;
     for(unsigned int i = 0; i < toRead.size(); i++)
     {
          if(toRead[i] == 'x')
              hitchar = true;

          if(hitchar)
              output.append(toRead[i]);
     }

}


NEXT word/number of input into a variable


Can you explain in a little more detail
Last edited on
Can you explain how that is a bad style of coding? If you're saying so because it's a global variable, I only created one because this is most likely a homework-ish assignment, or a simple exercise. If you really want to, you can just put the global in the main function...
Last edited on
For example if it encounters the word "AVERAGE" then it should read the numbers following. If the word NAME is read then the next word should be stored as name.
Ah, I see. Just add a simple edit.

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
#include <iostream>
#include <string>

int main()
{
     const std::string toRead = "Read me AVERAGE now!";

     std::string output = "";
     bool hitchar = false;
     for(unsigned int i = 0; i < toRead.size(); i++)
     {
          if(toRead[i]     == 'A' &&
             toRead[i + 1] == 'V' &&
             toRead[i + 2] == 'E' &&
             toRead[i + 3] == 'R' &&
             toRead[i + 4] == 'A' &&
             toRead[i + 5] == 'G' &&
             toRead[i + 6] == 'E' &&)
             hitchar = true;

          if(hitchar)
              output.append(toRead[i]);
     }

}


EDIT: You can switch out the word "now" with the numbers you'd like.
Last edited on
That's helpful, but do you know of a way to do it using sstreams?
Yea, off the top of my head (I didn't compile & test)

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
#include <string>
#include <sstream>
#include <vector>

int main()
{
	std::vector<std::string> tokens;
	
	bool hitkey = false;
	
	std::string output;
	const std::string s = "You should AVERAGE read me.";
	std::string key = "AVERAGE";
	std::istringstream iss(s);
	
	while(iss)
	{
		std::string sub;
		iss >> sub;
		
		tokens.push_back(sub);
	}
	
	for(unsigned int i = 0; i < tokens.size(); i++)
	{
		if(hitkey)
		{
			output.append(tokens[i]);
			output.append(' ');
		}
	
		if(tokens[i] == key)
		{
			hitkey == true;
		}
	}
	
	std::cout << "Output : " << output;
	
	std::cin.get();
	
}
Last edited on
Big help thanks! Is there also a way to have it add to a score after average until it detects a non number? Is it valid to do an if somehing == int test?
Yes, like this:
1
2
3
4
5
6
7
8
9
bool isInt(const std::string str)
{
	for(unsigned int = 0; i < str.size(); i++)
	{
		if(!(str[i] >= '0' && str[i] <= '9')) return false;
	}
	
	return true;
}
Last edited on
So this is what I have before even formatting output.

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
using namespace std;

int main(){
    vector <string> tokens;
    string s;
    bool hitavg = false;
    bool hitname = false;
    string avgKey = "AVERAGE";
    string nameKey = "NAME";
    string sub, name;
    bool isInt = false, isAvgInt = false;
    string stuName;
    int score, avg, final, finalavg;

    cin >> s;
    istringstream iss(s);

    while(iss){
        iss >> sub;

        tokens.push_back(sub);

    }
    for(unsigned int i = 0; i < tokens.size(); i++) {
        if (hitavg){
            for (int a = i; a < tokens.size(); a ++)
            {
                if (!(tokens[i] >= '0' && tokens[i] <= '9')) return false;
                else avg += tokens[i];
            }


        }
        if (hitname){
            name = tokens[i+1];
        }

        if(tokens[i] == avgKey) hitavg = true;
        else if (tokens[i] == nameKey) hitname = true;
    }





return 0;
    }// end of main 



It will not compile and gives me several errors.
Topic archived. No new replies allowed.