0

Im trying to build a program that takes input from the user adds it the list and sums it but when i add some letters or a number like this 2,2 instead of 2.2 the program stops, how can i make it so the program tells the user that is not valid and lets him type again?

Thanks

int main()
{

float transaction = 0 ;
float sum = 0;
list <float> account;


do{
cout << "Insert a transaction or press 0 to end: " << endl;
cin >> transaction;
sua += transaction;
account.push_back(transaction);
}
while (transaction != 0);
  • 1
    [Loop until integer input is in required range fails to work with non-digit character inputs](https://stackoverflow.com/questions/13212043/loop-until-integer-input-is-in-required-range-fails-to-work-with-non-digit-chara/13212189#13212189) – WhozCraig Nov 13 '20 at 19:07
  • The problem you have here is the `2,2` is not invalid input for a float. `>>` will happily read `2` as a valid float value and leave `,2` behind. So you cannot solve this problem by reading floats. The only way to make this work is to read a string, and check if the whole string can be converted to a float. If so then convert it, if not then discard it and ask for more input. – john Nov 13 '20 at 19:12

0 Answers0