reading from several lines

Pages: 12
just a quick example:

"7 34 67 789 45 5 sadasd 5 asdasd 6 sdf 5 sadasd



sdfsdf 5sad fdsfsd dsf sd5 sdf sdfsd exit"

it give me 1
If I understand correctly you want to be able of reading numbers after invalid input, right?
Do you want to get '5' from "sd5" or that each input is separated by a whitespace ( "sd5" would be invalid ) ?
sd5 would be invalid - i want to make such program, that any kind of combination of invalid (sd5 too) and valid inputs vould be recognised by the program
OK, you'll need to change a bit the structure of the loop. This way it will also become simpler:

1
2
3
4
5
6
7
8
9
10
11
while ( cin >> input ) // read a string per time, separated by whitespaces 
{
    stringstream myStream(input);
    if ( myStream >> integer )
    {
        if (howmanytimes == 0 || integer < lowest) lowest= integer , howmanytimes = 1;
        else if (integer == lowest) howmanytimes ++;
    }
    else if (input == "exit")
        break;
} 
one problem: if the lowest number is included at the beginning of a string (5sadas or even 577sad but not 56), this is considered as a number

so theres no way to do this with getline (im certain it is, so just say if its possible but that it is too frustrated :) )

Last edited on
To discard input like "577sad" you can use this condition in the if:
if ( myStream >> integer && myStream.eof() )
so it will perform lines 6-7 of the code in my last post only if after reading the number there are no other characters in the stream ( so you reach the end of it )

Topic archived. No new replies allowed.
Pages: 12