i was wondering how to make a program, that would read numbers(which user would input) and for example output how many of them user inputed - but the point is to make such a program, in which lines and speces would be ignored (for example, between two numbers there would be 3 lines inbetween, and 7 spaces between the second and third...). Numbers can have several digits.
i would like to know, how to make it work like: "when i struck a number, i will read all of its digits and mark than as 1 number and then search for the next one"
(just want to make sure that you understand)
between two numbers there would be 3 lines inbetween, and 7 spaces between the second and third...).
Numbers can have several digits.
@ Claimz
What you are describing is exactly how the >> operator works, reads stuff discarding all spaces/new lines.
Using >> with cin is not really good, for a better solution see http://www.cplusplus.com/forum/articles/6046/
You would need a loop to read all the number found in a line from the stringstream
yes i know all of that, but my quastion is, how to make it (i hope im not asking too much!)
how to read first number and then we can do what ever we want with it, and then move onto another - that is, how to convert from string to integer all the numbers(seperated with as many spaces as you you want) that have been inputed, particulary, how to make that loop of input type getline()'s
my approach would be to ignore all the spaces at beginning(it there were any) until id encounter a number, and read it until next space (though i dont know how i would manage more then one digit long numbers!!!), and so forth
You can replace the while condition with cin >> integer which will return true if the input was OK.
Then you should go for the getline way, you would need two loops for that: the first reads the lines from cin, the second reads the integers from the line
// ... same until line 9
while ( true ) // continuous loop
{
getline ( cin, input ); // read a line
if ( !cin ) // you may want also to check input == "exit" or something like that to terminate your program
break; // exit loop if input failed
// from line 10 to line 16
}
cout << howmanytimes << endl;
//...
myStream.clear(); // set stream state to good ( you are out the inner loop so previous input failed )
myStream >> input; // read a string from the stream
// ... check it ...
Sample inputs:
some valid numbers ... "exit"Will work"exit" any input Will work
valid numbers..invalid input.."exit"Won't work
myStream.clear();
while ( myStream >> input && input != "exit" ); // read until failure or input == "exit"
if (input == "exit")
break; // exit from the main loop