Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I've been written code i need to enter only digit if the user enter any

thing else char or symbols this message will be shown

"your enter is not digit try again"

the code I've been written it check the first number then entered an

infinite loop i want to check if digit then continue else

enter the number again


for example:
C++
int id;

start:
    cout<<"Enter Your id: ";
    cin>>id;
    if  (isdigit(id))
    {
        if (1>id)
        {
            cout<<"Your id is wrong.. "<<endl;
            goto start;
        }
        else if (id>32787)
        {
            cout<<"Your id is wrong.. "<<endl;
            goto start;
        }
        else
        {
            cout<<"Your id must be digit"<<endl;
            goto start;
        }
    }


this if to check if the number as it definition (int is between +-32787)

i am using Borland c++

thanks in advance
i hope you can help
Posted
Updated 15-Dec-10 3:07am
v4
Comments
Abdul Quader Mamun 15-Dec-10 9:03am    
Use pre tag properly.

Hi,

You may simply check that input is a valid unsigned int and nothing else, for instance:
C++
#include <iostream>
#include <limits>

using std::cin;
using std::cout;
using std::endl;

unsigned int IdInput()
{
    unsigned int id = 0;
    for(;;)
    {
        cout  <<"Enter Your id: ";
        cin >> id;
        cin.clear();
        cin.ignore(std::numeric_limits<int>::max(), '\n');
        if (cin.gcount() != 1)
            cout<<"Your id must be digit"<< endl;
        else if (!id || (id > 32787))
            cout << "Your id is wrong.. " << endl;
        else
            return id;
    }
}

int main()
{
    cout << "Valid id : " << IdInput() << endl;
}


Code edited to handle input beginning with a non-digit.

cheers,
AR
 
Share this answer
 
v2
Comments
ShilpiP 16-Dec-10 4:24am    
Good answer :)
lolinalo 16-Dec-10 13:10pm    
Hi AR
first,thank you for your trying.
but like The Title what i want is enter only digit..
in your answer above try to enter a char then see the loop..
every body can help!!
Alain Rist 28-Dec-10 3:02am    
Code edited to handle input beginning with a non-digit :)
lolinalo 28-Dec-10 13:34pm    
Thank u so much
Merci beaucoup
^_^
The first thing to learn is how to format your code so it is clearly readable. Now that I have done it for you I guess you can see why it is in a loop.
 
Share this answer
 
i am trying you can see this

<pre />
int read(){
  
   int id;     
    
   for (int i=1; i<4 ;i++)
     {
          cout<<"Enter Your id: ";
          cin>>id;
         
     if (isdigit(id))
            
         if ((id>1) && (id<32787))
              {
              return id;
              }
            
         else
             {
              cout<<"Your id is wrong.. "<<endl;
              cout<<"Try again"<<endl;
             }
    else
        {
 cout<<"Your id is wrong must be digit.. "<<endl;
cout<<"You are Trying to Enter Your id number "<<i<<" times incorrect You have only 3 Times"<<endl;
        continue;
     }
}
}
 
Share this answer
 
v3
To ensure you won't crash. You might want to read into a string, check whether there are non-numeric characters, and if not cast using atoi.
 
Share this answer
 
Hi Friends
When You Entered char OK This is Done but if You Entered char and numbers "The Char ignore" How can i Stop This and if There is Char You must goto new Enter

XML
#include <iostream.h>
#include <conio.h>
#include <stdio.h>
#include <ctype.h>
void main()
{
    int a;
    cout<<"Enter a value of a = ";
          while(!(cin>>a))
          {
             cout<<"\nError char input of a value ...\n";
            cin.clear();
            cin.ignore();
            cout<<"\nEnter a value of a = ";
          }
   cout << "you entered = " << a;
getch();
}
 
Share this answer
 
Comments
Alain Rist 28-Dec-10 3:05am    
See my edited answer handling input beginning with a non-digit.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900