-2

There is an input.txt, with string sorted like this:

Name1 Name2 "number1:number2" "number1:number2"...

.

.

.

NameX NameY "number1:number2" " number1:number2"...

Baically we don't know how much names are in it, and how much "number:number" strings are in a row. The task is to write out the first name in each line, and write out the number of times, when number1 is bigger than number2 in each row. The thing is, I don't know how should I read in the pairs, without knowing how many are there from them in each row.

Thanks for the help in advance.

edit: My problem is not actually the part where I don't know how long the file is, I can read it in line by line, I don't know how should I read in a row, where I don't know how many strings are.

edit2, exaple: Steven Jack 2:5 6:4 7:2

gerath
  • 3
  • 2

1 Answers1

0

You could get the seperated elements with a stringstream one by one, and check if it starts with a decimal digit character or not (works only if there isn't any name that starts with a number).

like so:

std::string line; // assuming you got the line

std::stringstream ss(line);
std::string str;

while (ss >> str) {
    // use isdigit() to check if it's a decimal digit character or not
    if (isdigit(s.at(0))) {
        // do something with NUMBERS
        // getting the actual numbers from these strings is another problem
    } else {
        // do something with NAMES
    }
}

EDIT : as you said getting lines one by one is not a problem, so the "line" variable in my code assumes that you somehow got a whole line in the memory and works with this line.

EDIT2 : it's not much different if you don't have quotation marks, in this case, you check if the first character is a number or not (btw if any name starts with a number it's gonna be a problem)

geri
  • 57
  • 1
  • 2
  • 6
  • edited the answer, it's almost the same, but you can use isadigit to check if it starts with a decimal digit character (0, 1, 2, ... , 9) or not. here is a link for this method: http://www.cplusplus.com/reference/cctype/isdigit/ – geri Jun 16 '17 at 16:14
  • oh but I didn't think of negative numbers, can it be negative? cuz if any of that starts like this: -3:4 it's gonna check the '-' first wich is obviously not a number. – geri Jun 16 '17 at 16:25