Reputation: 1
I am trying to create something (I'm thinking probably a loop?) that will allow me to have the user enter several numbers and then enter something like "done" to get all the numbers added up.
for example if I have a float (call it x for now) they could enter "7 enter 5 enter 9 enter done enter" and it would add those numbers and make x that value. Where I am coming into problems is that I need the user to be able to be able to enter as many numbers as they want (between 1 and 70 as an example) without specifying how many numbers they want to enter, just entering something in when they are done.
Thank you all
Upvotes: 0
Views: 239
Reputation: 75130
You'll need to use an infinite loop (while (true)
or for (;;)
) to read the next input into a string.
Check if the string is done
. If so, break
the loop.
Then try to parse that string into a double
(don't use float
) with the function std::stod
.
If the parsing fails, optionally print an error message like "Bad input, try again"
and restart the loop. If the parsing succeeds, add the number to the counter and restart the loop.
Upvotes: 2