-4

I want the user to enter a key and I want to check whether the key is a number or not, throw a message if it is not and exit if it is 0.

I read an answer that suggested the below method here : isdigit() c++, probably simple question, but stuck

int key;

while (true){

    cout << "Enter Key (Press 0 to Exit) : ";

    if (cin>>key){

        if (key == 0){ break; }

        //Code goes here
    }

    else{cout<<"Key should be a digit "<<endl;}
}

but my code goes into an infinite loop as soon as I enter an alphabet and I am not able to figure out why.

Any help would be appreciated or if there is a better alternate method for the same then do suggest.

Community
  • 1
  • 1
Manav Saxena
  • 453
  • 2
  • 8
  • 21

2 Answers2

0
cin>>key

try to read an int from the console.

If you enter a non number character the next read from cin will set the cin stream into error state and nothing can be read from cin anymore until you clear the error flags of the stream.

cin.clear();

resets the error state.

You also have to ignore the entered chars which results in failure mode with

cin.ignore();

Example:

int main()
{   
    int i;

    while (1) 
    {   
        std::cin >> i;

        if ( std::cin.fail())
        {   
            std::cout << "Something went wrong with cin" << std::endl;
            std::cin.clear();
            std::cin.ignore();
        }
        else
        {   
            std::cout << "input works, read: " << i << std::endl;
        }
    }
}

If you try to read a single digit form console look also here:

Capture characters from standard input without waiting for enter to be pressed

Community
  • 1
  • 1
Klaus
  • 24,205
  • 7
  • 58
  • 113
0

my code goes into an infinite loop as soon as I enter an alphabet

That's because you declared key as an int, so when std::cin fails to read an integer number the stream is set into an error state and the break statement inside the if's is no longer reachable.

A possible alternative is to read a line from the input stream as a string and then try to convert it to a number.

Now, given OP's question:

I want the user to enter a key and I want to check whether the key is a number or not, ...

It's not clear to me (my fault, probably) if key has to be considered a single or a multi digit number. In the following snippet I'll show the latter case. Please, note that it may include the former too.

#include <iostream>
#include <string>

int main()
{
    std::string line;
    while ( std::getline(std::cin, line) )
    {
        if ( line.empty() ) continue;
        try
        {
            int key = stoi(line);
            if ( !key ) break;
            // code that uses the key...
        }
        catch ( const std::invalid_argument &e )
        {
            std::cout << "Key should be a digit!\n";
        }
        catch ( const std::out_of_range &e )
        {
            std::cout << "The value entered can't be represented by an int.\n";
        }
    }

    return 0;
}
Bob__
  • 12,361
  • 3
  • 28
  • 42