Need help!! with number inputs & no letters....

What i want to do is a function that checks if its only
numbers in a CIN.. And if there are some letters in it,
i want the program to print "no letters" or something like that..
And then jump back to the CIN... And when the correct
input is done, the prog countinues to next step...




Comments

  • : What i want to do is a function that checks if its only
    : numbers in a CIN.. And if there are some letters in it,
    : i want the program to print "no letters" or something like that..
    : And then jump back to the CIN... And when the correct
    : input is done, the prog countinues to next step...
    :
    :
    :
    :
    : Ok Try this

    int cin(void)
    {
    int no;
    char sno[10];
    START:
    cin>>sno; //sno is string of nos
    int index=0;
    while(index!strlen(sno))
    {
    if(sno[index++]<0 && sno[index++] >9)
    {
    goto START;
    }
    }
    no=atoi(sno);
    return no;
    }
    }
    Which is a very simple program if any problem than never hasitate to asl ,
    thanks
  • : What i want to do is a function that checks if its only
    : numbers in a CIN.. And if there are some letters in it,
    : i want the program to print "no letters" or something like that..
    : And then jump back to the CIN... And when the correct
    : input is done, the prog countinues to next step...
    :
    :
    :
    :
    :

    Hi,

    You could use the function [b]isdigit()[/b] and it requires the header file [b]#include [/b].

    Try out this piece of code:

    [code]
    //Example reads in a character and checks to see if it is a digit

    // You need this header file in order to use 'isdigit()'
    #include
    #include

    int main()

    {

    char a_char;

    cin>>a_char;

    if(isdigit(a_char))

    cout<<"Is a digit!";

    else

    cout<<"Is not a digit!";


    return 0;
    }
    [/code]

    Hope this helps,
  • when will you learn that iostream.h doesnt exist any more and ctype is deprecated?

    : : What i want to do is a function that checks if its only
    : : numbers in a CIN.. And if there are some letters in it,
    : : i want the program to print "no letters" or something like that..
    : : And then jump back to the CIN... And when the correct
    : : input is done, the prog countinues to next step...
    : :
    : :
    : :
    : :
    : :
    :
    : Hi,
    :
    : You could use the function [b]isdigit()[/b] and it requires the header file [b]#include [/b].
    :
    : Try out this piece of code:
    :
    : [code]
    : //Example reads in a character and checks to see if it is a digit
    :
    : // You need this header file in order to use 'isdigit()'
    : #include
    : #include
    :
    : int main()
    :
    : {
    :
    : char a_char;
    :
    : cin>>a_char;
    :
    : if(isdigit(a_char))
    :
    : cout<<"Is a digit!";
    :
    : else
    :
    : cout<<"Is not a digit!";
    :
    :
    : return 0;
    : }
    : [/code]
    :
    : Hope this helps,
    :

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories