Aryan
Aryan

Reputation: 1

How to prevent the user from entering more than one character in the below sample code?

I am facing problem in the below code. If the user enter more than one charater then my loop gets executed number of times equal to the length of the string entered by the user. My code is written in GNU c/c++ compiler.

Thanks in advance.

int continue_option()
{
    char c;
        loop:
        fflush(stdin);
                cin.ignore();
        cout<<"\n\n\t\t\t\tPress (Y/y) - Continue / Press (N/n) - Exit :";
                cin>>c;
        if(c=='y'||c=='Y')
        {
                          system("clear");
                   }
        else if(c=='n'|| c=='N') 
        {
            exit(0);
        }
        else
            {
                printf("\n\t\t\t\tInvalid Option.Try Again.....");
                                goto loop;
                        }
        fflush(stdin);
}

Upvotes: 0

Views: 5219

Answers (4)

Bhargav
Bhargav

Reputation: 10199

Try using cin.get() or getch() to read just one character at a time. Also, I guess you'd be better off replacing the whole thing with a simple loop like:

char ch = '\0';
do
{
   ch = getch();
}while((tolower(ch) != 'y') || (tolower(ch) != 'n'))

if(tolower(ch) == 'y')
{
   //additional handling
}
else
{
  exit(0);
}

Upvotes: 1

ereOn
ereOn

Reputation: 55726

Not exactly the same behavior, but should put you on track:

#include <iostream>
#include <iomanip>

bool is_valid_answer(char c)
{
    switch(c)
    {
        case 'y':
        case 'Y':
        case 'n':
        case 'N':
            return true;
        default:
            return false;
    }
}

bool continue_option()
{
    std::cout << "Press (Y/y) to continue, (N/n) to exit: " << std::flush;

    char c = '\0';

    while (std::cin.get(c) && !is_valid_answer(c));

    return ((c == 'y') || (c == 'Y'));
}

int main()
{
    std::cout << "Continue option: " << continue_option() << std::endl;
}

Upvotes: 0

Bo Persson
Bo Persson

Reputation: 92211

You can't stop the user from typing more than one character.

What you can do is ignore the rest of the line. You have already use cin.ignore() which ignores one character. You can use cin.ignore(large number) to ignore up to the large number or the end-of-line, whichever appears first.

Unlike flushing output files, fflush(stdin) doesn't really do anything.

Upvotes: 1

King_DuckZ
King_DuckZ

Reputation: 238

First thing, don't use jumps. They are old style, and they make Dijkstra spin in his grave, on top of all the other bad consequences. I don't mean "vintage", I really mean old in the bad sense.

As of your question, I'd rather put the result in a std::string and only consider the first character in there:

std::string input;
std::cin >> input;
switch (input[0]) {
case 'y':
case 'Y':
    //your code
    break;
case 'n':
case 'N':
    exit(0);
default:
    std::cout << "Invalid text" << std::endl;
}

I would also refrain from using exit(), I'd rather rely on a function's return value to finally cause a return 0; in the main(), or some equivalent technique.

Upvotes: 3

Related Questions