Can someone please explain whats happening to this input

Hi Guys,

I am trying to validate this input in these functions that I have created for my roulette program. The rest of the program is much more intricate and complicated with dynamic arrays of structures and what not. Everything works. Except for trying to validate this for the right data types.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
  int money()
{
   int validatedMoney;

   // Allow the user to enter the amount to enter the table with an amount of money.
   cout << "                             Welcome to Roulette!\n\n"
      << "Please enter the dollar amount to enter the table with.\n"
      << "Note, you may leave the table after any spin and are only alowed a max of 8 bets per spin.\n\n";

   validatedMoney = validateMoney();
   cout << "\n";

   return validatedMoney;
}

int validateMoney()
{
   int moneyAmount;

   cin >> moneyAmount;

   while (!(cin >> moneyAmount) || moneyAmount < 0 || moneyAmount > 10000)
   {
      if (moneyAmount < 0)
      {
         cout<< "\n";
         cout << "You cannot enter the table with a negative amount, please re-enter your amount\n";
         cin >> moneyAmount;
      }
      else if (moneyAmount > 10000)
      {
         cout<< "\n";
         cout << "You cannot enter the table with more than $10,000, please re-enter your amount\n";
         cin >> moneyAmount;
      }

      cin.clear();
      cin.ignore(numeric_limits<streamsize>::max(), '\n');
      cout<< "\n";
      cout << "Please enter an integer\n";
      cin >> moneyAmount;
   }
   return moneyAmount;
}


but it keeps making me input it twice when I do enter the correct range and type of number that I need. It checks it just fine. Cannot enter letters or floating point data types. But when I enter the correct numerical value, i have to twice. Can someone tell me why?
You have two calls to std::cin, one on Line 20 and another in the while loop at Line 22. The program is doing what you are telling it to.
When I eliminate it, it only asks once. If another invalid option is entered, then it continues on. I need to keep checking

I switched it to this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int validateMoney()
{
   int moneyAmount;

   //cin >> moneyAmount;

   while (!(cin >> moneyAmount) || moneyAmount < 0 || moneyAmount > 10000)
   {
      cin.clear();
      cin.ignore(numeric_limits<streamsize>::max(), '\n');
      cout<< "\n";
      cout << "Please enter a valid option\n";
   }
   return moneyAmount;
}


no good. Only checks once, if invalid option entered again, it doesnt care and keeps going and screws up the next function as well.
Last edited on
Then switch to a 'do while()' loop instead.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
    do
   {
      if (moneyAmount < 0)
      {
         cout<< "\n";
         cout << "You cannot enter the table with a negative amount, please re-enter your amount\n";
         cin >> moneyAmount;
      }
      else if (moneyAmount > 10000)
      {
         cout<< "\n";
         cout << "You cannot enter the table with more than $10,000, please re-enter your amount\n";
         cin >> moneyAmount;
      }

      cin.clear();
      cin.ignore(numeric_limits<streamsize>::max(), '\n');
      cout<< "\n";
      cout << "Please enter an integer\n";
      cin >> moneyAmount;
   }while (!moneyAmount || moneyAmount < 0 || moneyAmount > 10000);


Or something similar to that.
It does not work.

I enter 3.2 once, it checks. I enter it again and it continued on with the program.
with that loop
Yes, 3.2 cast to an integer becomes 3 and that passes your test of falling between 0 and 10000. The problem there is with your integrity check.
Topic archived. No new replies allowed.