Validate Integer Input

This is a simple program with a select statement. The problem is when the user enters a char when an int is expected the program goes into an infinite loop. I have tried a few ways to validate the user is entering an integer, but I have been unsuccessful. Does anyone have any ideas?

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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <iostream>
using namespace std;

//Function Prototype
void displayMenu();

int main()
{  
  // Constants for menu choices
  const int ENTER_RECORD   = 1,
            DISPLAY_RECORD = 2,
            DELETE_RECORD  = 3,
            CHANGE_RECORD  = 4,
            DISPLAY_ALL    = 5;
            
            int    userChoice = 6;
			
	do{	
          displayMenu();
       
            //Get the users choice
   	        do
   	        {
   	   	     cout << "Enter Your Choice (1-6): ";
		     cin >> userChoice;
	        }
	        while (userChoice < 1 || userChoice > 6);
		
	    //Process userChoice
	    switch(userChoice)
	    {
	       //*******************Enter Records to File*******************
		   case ENTER_RECORD:
           cout << "\nYou selected Enter a new Customer Account.\n\n";
           break;

           //******************Display a Particular Account*************
           case DISPLAY_RECORD:
           cout << "\nYou selected Display a Customer Account.\n\n";
           break;
    
           //******************Delete a Particular Account***************  
           case DELETE_RECORD:
           cout << "\nYou selected Delete a Customer Account.\n\n";
           break;
   
           //******************Change a Particular Account***************  
           case CHANGE_RECORD:
           cout << "\nYou selected Change a Customer Accounts.\n\n";
           break;
 
           //******************Display All Accounts**********************
           case DISPLAY_ALL:
           cout << "\nYou selected Display All Accounts.\n\n";
           break;
           
           
           //*****************Anything not between 1-5********************
           default: 
           break;
	    }//End of switch statement
	
      }
      while (userChoice != 6);
 
 return 0;
			            
}

////////////////////Function to Display Menu///////////////////////
void displayMenu()
{
  cout << "\n * * * * A C C O U N T  M E N U * * * * \n\n";
  cout << "1. Enter a New Customer Account\n";
  cout << "2. Display a Customer Account\n";
  cout << "3. Delete a Customer Account \n";
  cout << "4. Change a Customer Account\n";
  cout << "5. Display All Accounts\n";
  cout << "6. Exit the Program\n\n";
}
//////////////////////////////////////////////////////////////////// 
Many ways to get around it. You could create a loop that tells the user to re-enter until an integer is inputted.

You could use getline() instead of cin.

Also some other suggestions -

http://stackoverflow.com/questions/19521320/why-do-i-get-an-infinite-loop-if-i-enter-a-letter-rather-than-a-number

http://stackoverflow.com/questions/23354412/how-to-avoid-infinite-loop-when-user-inputs-a-character-instead-of-float-and-int

Next time I would suggest you use google before posting, there is lots of info there.
Yea, I have researched it a bunch. I have tried while loops and if statements and nothing seems to work.
Thank you!
If nothing seems to work, you should perhaps post the code of the non-working solutions you've tried. And we can help out :)
Topic archived. No new replies allowed.