Validate user input is an integer

I am having some problems checking whether or not the user has entered an integer. Here is my code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void GetUserCode(int codes[], double prices[])
{
	int code = 0;
	int userCode = 0;
	do
      {
          cout << "Enter a product code: ";
          cin >> code;
          
          if(code)
             userCode = code;
                 
      }while(code);
}


The if statement should be true if the user input is composed of only integers. The while should be anything that is not an integer, or if there are non-integers anywhere in the input. That way if the user inputs "PP" they will be asked again to enter a product code.
Topic archived. No new replies allowed.