Jan 28, 2014 at 7:14pm UTC
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?
Jan 28, 2014 at 7:21pm UTC
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 Jan 28, 2014 at 7:22pm UTC
Jan 28, 2014 at 7:55pm UTC
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 Jan 28, 2014 at 7:56pm UTC
Jan 28, 2014 at 8:18pm UTC
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 Jan 28, 2014 at 8:19pm UTC
Jan 28, 2014 at 8:22pm UTC
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.
Jan 28, 2014 at 8:25pm UTC
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 Jan 28, 2014 at 8:27pm UTC
Jan 28, 2014 at 8:28pm UTC
That's helpful, but do you know of a way to do it using sstreams?
Jan 28, 2014 at 8:55pm UTC
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 Jan 28, 2014 at 10:01pm UTC
Jan 28, 2014 at 9:28pm UTC
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?
Jan 28, 2014 at 9:43pm UTC
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 Jan 28, 2014 at 10:02pm UTC