Hi all,
I am trying to make sure that users can only input integers in my program and no other types. Here is the program:
#include<iostream>
using namespace std;
int main()
{
int numbers[9];
int k;
cout<<"Enter 9 numbers: ";
for( k = 0; k<9; k++)
{
cin >> numbers[k];
}
cout<<"here are your numbers: ";
for (k=0; k<9; k++)
{
cout<<" "<<numbers[k];
}
}
I am having real trouble (I am still a beginner) to validate user input.
I have tried with the
isdigit()
function, with if
((numbers[k]<'48' || numbers[k]>'58'))
because I thought that the ascii table would have been of use but no joy. All the above included in a while loop of course...
I meant I tried things like:
while((numbers[k]<'48' || numbers[k]>'58'))
{
cout<<"wrong, again ";
cin >> numbers[k];
}
or
while((!isdigit(numbers[k]))
{
cout<<"wrong, again ";
cin >> numbers[k];
}
Any suggestion?
thanks