Say I was tracking statistics from a game and I wanted to track damage dealt.
Example:
"2016-06-12 13:53:56 [System] [] You inflicted 52.0 points of damage."
And in order to track dmg I would go through a log row by row until i hit the keyword " You inflicted " and then directly after I would read the variable(52.0).
problem is that the computer wants the variable on the next line after the key is found and I'm not sure how I would go about reading on the same line
Example
"2016-06-12 13:53:56 [System] [] You inflicted
52.0 points of damage."
this would work but doesn't help me.
TLDR: I wish to read a variable from a file directly after a keyword is found and not on the row below
#include <iostream>
#include <string>
#include <cstdlib>
#include <sstream>
usingnamespace std;
int main(void)
{
string key = "You inflicted";
string row = "2016-06-12 13:53:56 [System] [] You inflicted 52.0 points of damage.";
auto pos = row.find(key);
double dmg = 0.0;
if (pos != string::npos)
{
row = row.substr(pos + key.size() + 1);
// row is now 52.0 points of damage.
istringstream iss(row);
iss >> dmg;
// use dmg
}
system("pause");
return EXIT_SUCCESS;
}
That would probably do!
I'm a total noob so few questions for the sake of learning:
does istringstream only converts the first iteration(being 52.0) and in to what?
52.0 would be no problem being an int but if it was 52.2 I need it to be a float
also why system("pause") and exit_success??
I was told I could use regex too but never used them
does istringstream only converts the first iteration(being 52.0) and in to what?
istringstream reads a double until it finds a character that is not a number - in this case the space.
52.0 would be no problem being an int but if it was 52.2 I need it to be a float
It is a double since in the example it was 52.0 it will show as 52. If you change to 52.2 it will show 52.2.
also why system("pause") and exit_success??
system("pause") is only needed if you use Visual Studio to keep the console open.
EXIT_SUCCESS is a way to say that the program ran successfully. You can return 0 instead.
I was told I could use regex too but never used them
Regex are quite advanced stuff and I would not recommend regex to a beginner.