Hi there,

I need getch() type function for int through which I can take input from user, to store it in int variable. I actually want to use in menu from which user selectes any number to call that function to carray that task.

Reason of using getch() function is that user did not have to press enter, they just press number and that task is carried out. Thanks !

char no;    //I want to declare int

    cout<<"Press 1 to add items in stock."<<endl;
    cout<<"Press 2 to display stock."<<endl;
    cout<<"Press 3 to exit."<<endl;
    no = getch();   //alternative of this function for int

    if(no == 1)
    {
        s->addItems();
    }
    else if(no == 2)
    {
        s->displayStock();
    }
    else if(no == 3)
    {
        cout<<"\nProgram terminated !"<<endl;
    }
    else
    {
        cout<<"\nInvalid input !"<<endl;
    }

It's a little tricky because integers can be represented with multiple characters. Unless you only need a single digit, how do you plan to signal the end of the integer using raw input?

how do you plan to signal the end of the integer using raw input?
Can you please explain I did not understand ?

getch reads a single key press. If you don't want to press Enter, how do you plan to know when an integer (that takes multiple key presses) ends?

What Deceptikon is saying is, for example, what happens if the user has to type '10'? You'll read the first input and think the user meant '1'

Anyway, that aside, you don't need an int. Just compare against the char. For example, if the user enters the char '1',

if(no == '1')

Edited 3 Years Ago by Moschops

This article has been dead for over six months. Start a new discussion instead.