Error checking for a beginner

Can someone tell me how to make program only except digits. I am a beginner so I will need it in laymans terms. Thanks in advance

-James
You mean accept?
There's no way to force the user to only input a certain thing. They can input whatever they want. What you could do is make them keep inputting until they input an acceptable value. You can do such a thing with a while loop.
http://www.cplusplus.com/forum/beginner/18258/

http://www.cplusplus.com/forum/articles/6046/

http://www.cplusplus.com/forum/beginner/13044/page1.html#msg62827
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
using namespace std;

int main()
{
   cout << "ENter a number: " << flush;
   double n;

   while (true)
   {
      cin >> n;
      if ((cin))
         break;
      cin.clear();
      cin.ignore( 1000, '\n' );
      cout << "Follow instructions please!: " << flush;
   }
   cout << "Good job you entered: " << n << endl;

   return 0;
} 


you can also just cast the double to an int for integer precision.
Last edited on
My program goes into an infinite loop when a character is entered. How do I make a while loop stop that?
You set the condition so that it only runs when the input is invalid.
1
2
3
4
5
6
7
cout << "Input a num from 1 to 5";
int num;
cin >> num;
while (num > 5 || num < 1)
{
    cin >> num;
}
will this work with the default of a case switch
I don't get what you mean, can you show some sample code to explain what your trying to achieve?
test1:
cin>>leaper_toggle;
switch (leaper_toggle)
{
case 1:
standard_year=28;
cout<<"What day (Numeric) within " <<month<< " do you want to use? " <<flush;
cin>>day;
break;

case 2:
leap_year=29;
cout<<"What day (Numeric) within " <<month<< " do you want to use? " <<flush;
cin>>day;
break;

default:


Within this default space if the user inputs a number other than 1 or 2 I can use cout<<you didnt......endl; goto test1: but what if they input a letter...I go into infinite looping.




break;
}
Last edited on
Can I somehow go to ascii chart and anything less than or equal to 47 or greater than or equal to 58 exlude?
or should I drop default and create a third case to not accept letters....I dont know....as I look at it it seems default offers a little error checking but it is nit sufficient
default will accept anything other than 1 or 2, I'm not sure about the ascii chart, but what you can do before the switch is make sure the number is valid by using the above methods posted, please read through the links I posted, they will provide you with a multitude of examples, and from that you should be able to figure out what to do.
Otherwise please ask again for more help.

As you have the above code, without validation for leaper_toggle if the user enters a non-numeric char then it will probably crash and I didn't think this is what you wanted.

Can someone tell me how to make program only except digits.
This is making sense
while (true)
{
cin>>leaper_toggle;

if ((cin) && (leaper_toggle >= 1) && (leaper_toggle <=2))
break;

cin.clear();
cin.ignore( 1000, '\n' );
cout << "you must enter a 1 or 2....no characters" << flush;
}


So this is what I came up with....Why though does it work.....I must know why...any takers?
Nevermind.......I broke it down and I understand it well....Thanks everyone for your help....
Topic archived. No new replies allowed.