ngbeslhang
ngbeslhang

Reputation: 59

Display the output when type specifed word as first letter and ignore the other words after it

After this question I figured out that using char as input will avoid the infinite loop caused by typing characters for input which is using int. But now I had met another problem.

When I typed the code below:

#include <iostream>
void language();

int main() {
    language();
}

void language() {
    char choice;

    // Ask user for something and input        
    std::cout << "Press 1 to exit the program\n\n";
    std::cin >> choice;

    // Getting user's input and run the code below and find for specific words
    switch(choice) {
        case '1':
            std::cout << "\n\nEnding program...";
            return 0;
            break;
        default:
            std::cout << "\n\nPlease type the specific number.\n\n";
            language();
            break;
    }
}

When I compile it I didn't get any errors or warnings. But when I type 12 or similar word with 1 at first, the program will be ended.

And before answering me, I still learning C++. (By the way I don't think I really need to say this?) And because this I didn't know how to solve this. What's happening to my code?

Upvotes: 1

Views: 594

Answers (4)

Pierre Fourgeaud
Pierre Fourgeaud

Reputation: 14510

As you want a char from the input, std::cin will just get the first character you type in the input and assign to choice. It will ignore the following characters.

That is, you will enter in the first case of your switch/case condition and return.

It depends on what are the inputs you expect from the user. If you expect only numbers, I suggest you to use an int :

#include <limits>    // needed for std::numeric_limits

void language() {
    int choice;
//  ^^^^

    // Ask user for something and input        
    std::cout << "Press 1 to exit the program\n\n";
    std::cin >> choice;

    std::cin.clear();
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

    // Getting user's input and run the code below and find for specific words
    switch(choice) {
    case 1:
        std::cout << "\n\nEnding program...";
        break;

    default:
        std::cout << "\n\nPlease type the specific number.\n\n";
        language();
        break;
    }
}

Working live example.

Upvotes: 1

P0W
P0W

Reputation: 47814

char choice;

will just receive a single character from standard input.

so all digits starting with 1 will end your program

instead use int choice; and change case to case 1:

    #include<limits>
    //...

    int choice;
    //...   
    std::cout << "Press 1 to exit the program\n\n";
    while(true)
    {
        if (std::cin >> choice)  
          break ;
        else {
            std::cout<<"Not an integer !"<<std::endl;
            std::cin.clear() ;
            std::cin.ignore(std::numeric_limits<std::streamsize>::max(),
                     '\n') ;
        }
     }

    switch(choice) {
        case 1:
            //...
            break;
        default:
            std::cout << "\n\nPlease type the specific number.\n\n";
            language();
            break;
        }

Upvotes: 0

marcadian
marcadian

Reputation: 2618

You want the choice as string, not char since char only contains one character. Also you don't need break after the return statement

Upvotes: 0

Mario Rossi
Mario Rossi

Reputation: 7799

Your code is exiting when you enter input starting with character 1.

Upvotes: 0

Related Questions