Howdy, Stranger!

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

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

A function in C/C++ like getch()that take integers

Hello everybody,
Actually I m in search of such a function that work like getch() function.
But that should return integer instead of character...

For Example..

void main()
{
int a;
a=getch();
cout<<a;
getch();
}

Above code will give the ascii value of entered number. I do not want ascii of no. but a no.

Please help me!

Comments

  • BitByBit_ThorBitByBit_Thor Posts: 2,444Member
    : Hello everybody,
    : Actually I m in search of such a function that work like getch()
    : function.
    : But that should return integer instead of character...
    :
    : For Example..
    :
    : void main()
    : {
    : int a;
    : a=getch();
    : cout<<a;
    : getch();
    : }
    :
    : Above code will give the ascii value of entered number. I do not
    : want ascii of no. but a no.
    :
    : Please help me!
    :

    What's wrong with the following?
    [code]
    char ch;
    ch = getch();
    cout << ch;
    [/code]
    Best Regards,
    Richard

    The way I see it... Well, it's all pretty blurry
  • LundinLundin Posts: 3,711Member
    There is no such standard function, apart from scanf() and fgets(). Though converting an ASCII number to an integer isn't hard:

    [code]
    #include

    using namespace std;


    int main()
    {
    int a;

    a = getch();

    if(isdigit(a))
    {
    a = a - '0';
    cout << a << endl;
    }
    getch();
    }
    [/code]
  • bilderbikkelbilderbikkel Posts: 754Member
    : Hello everybody,
    : Actually I m in search of such a function that work like getch()
    : function.
    : But that should return integer instead of character...
    :
    : For Example..
    :
    : void main()
    : {
    : int a;
    : a=getch();
    : cout<<a;
    : getch();
    : }
    :
    : Above code will give the ascii value of entered number. I do not
    : want ascii of no. but a no.
    :
    : Please help me!
    :
    Someone has to say it...
    bilderbikkel
  • BitByBit_ThorBitByBit_Thor Posts: 2,444Member
    : Someone has to say it...
    : bilderbikkel

    My hero *bows done*

    Best Regards,
    Richard

    The way I see it... Well, it's all pretty blurry
  • LundinLundin Posts: 3,711Member
    : Someone has to say it...
    : bilderbikkel

    Been a while since we had this debate :-)

    Very strictly speaking, the return type of main in both C and C++ can be of any type, though if it isn't int, then it is implementation-defined behavior. That is, the standard doesn't forbid it, it simply doesn't cover it. Though the standard states that all implementation-defined behavior must be documented. If it isn't documented, it violates ISO C/C++. For example, ancient TC accepts void main() without warnings and also lacks documentation stating the reasons why, so it isn't an ISO conformant compiler.

    Implementation-defined behavior is not to confuse with undefined behavior. The latter is the same as a programming error, the code won't be portable and the result will be random. For example, fflush(stdin) is undefined behavior.

    However, listed among undefined behavior is this:

    ISO 9899:1999 section J2:

    "A program in a hosted environment does not define a function named main using one of the specified forms (5.1.2.2.1)".

    The specified forms are

    int main(void)
    int main(int argc, char* argv[])

    So if you are writing a program for a [b]hosted enviroment[/b] (PC, Unix, Linux etc), the program must return int and may only have argc and argv as parameters, if any parameters at all.
  • VinayKhareVinayKhare Posts: 69Member
  • VinayKhareVinayKhare Posts: 69Member
    : Thanks BitbyBit for making me informed bout such a law or section of ISO...!
    :

  • BitByBit_ThorBitByBit_Thor Posts: 2,444Member
    : : Thanks BitbyBit for making me informed bout such a law or section of ISO...!
    : :
    :
    :

    Yeah you're welcome. I'm always happy to steal the credit away from Lundin ;)
    Best Regards,
    Richard

    The way I see it... Well, it's all pretty blurry
  • ISHUISHU NIT ROURKELAPosts: 1Member

    Hi friends I am doing a program for password check in which password would be integer string ,using getch()... but when i m entering the password on run screen ,it is showing anything.. AND same program worked for character string password ...help me!!

    include

    include<conio.h>

    using namespace std; int main() { cout<<"Enter the passkey to unlock: "<<endl; int passkey[7]={0,0,0,0,0,0}; for(int i=0;i<6;i++) { passkey[i]=getch(); cout<<"*"; } cout<<endl; cout<<"passkey entered is: "<<endl; for(int i=0;i<6;i++) { cout<<passkey[i]; } cout<<endl<<endl; int password[7]={1,2,3,4,5,6}; for(int i=0;i<6;i++) { if(passkey[i]==password[i]) { if(i!=5) { continue; } else { cout<<"SUCCESS!! You can proceed now."<<endl; } } else { cout<<"SORRY!! You entered wrong passkey. "<<endl; break; } } return 0; }

Sign In or Register to comment.