Home > Software engineering >  what is the reason user cannot enter input after the 2nd question and compiler finish the program?
what is the reason user cannot enter input after the 2nd question and compiler finish the program?

Time:05-04

I'd like to write a program that asks the user if he/she tends to work as a delivery service. If yes he/she should have some eligibilities. If the user have them he/she can be hired.

I've written some code, but when I compile and run it, after the user answered 2 first questions the cmd doesn't let the user to enter his/her answers for remaining questions (the cin code doesn't work anymore or as though there is no cin code.)

What's wrong with my code? Why cin doesn't work after 2 first question but it works at first 2 ones? How can I make it work?

#include <iostream>

using namespace std;

int main()
{
    string answer{}, ssn1{}, a, b;
    int age{0};
    bool parental_consent{false}, ssn{false}, accidents{false};
    cout << boolalpha;

    cout << "Do you want to work as a local delivery (Y/N)? \n";
    cin >> answer;
    if (answer == "Y" || answer == "y")
    {
        cout << "Do you have any social security number(yes/no)? \n";
        cin >> ssn;
        ssn = ("yes" || "Yes");

        cout << "How old are you? \n";
        cin >> a;
        cout << age;
        cout << "If you're between 15 and 18 years old please answer this question: \n Do have parental "
                "consent(yes/no)? \n";
        cin >> ssn1;
        //  cout << parental_consent;
        parental_consent = ("yes" || "Yes");
        cout << "Have you ever had any accidents? \n";
        cin >> b;
        accidents = ("yes" || "Yes");

        if ((age >= 18 || (age >= 16 && parental_consent)) && ssn && !accidents)
        {

            cout << "Yes, you can work.";
        }
        else if (age <= 15)
        {
            cout << "Sorry, your age is not eligble!\n";
        }
    }
    else
        cout << "sorry to hear that.";

    cout << endl << endl << endl;
    return 0;
}

CodePudding user response:

From cppreference:

If the type of v is bool and boolalpha is not set, then if the value to be stored is ​0​, false is stored, if the value to be stored is 1, true is stored, for any other value std::ios_base::failbit is assigned to err and true is stored.

From the code, i can see that the second std::cin doesn't have boolalpha and is assigning to a bool value. This causes td::ios_base::failbit to be assigned to err, preventing other input. You have to do std::cin.clear(); to be able to use std::cin >> a; again.

If it still skips Try using cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); after every std::cin to clear out whitespaces that causes it to return immediately.

It seems you wanted to use std::boolalpha to convert your questions into boolean. This obviously didn't work because you used it on std::cout to use it on input you need to use it on std::cin like this:

cin >> boolalpha >> ssn;

But you can't answer yes or Yes, you need to answer true or false so it's better to just std::cin >> answer and compare the string instead like this:

#include <iostream>


int main()
{
    std::string answer{};
    int age{ 0 };
    bool parental_consent{ false }, ssn{ false }, accidents{ false };


    std::cout << "Do you want to work as a local delivery (Y/N)? \n";
    std::cin >> answer;
    if (answer == "Y" || answer == "y")
    {
        std::cout << "Do you have any social security number(yes/no)? \n";
        std::cin >> answer;
        ssn = (answer == "yes" || answer == "Yes");

        std::cout << "How old are you? \n";
        std::cin >> answer;
        age = atoi(answer.c_str());
        if (age >= 15 && age <= 18) {
            std::cout << "If you're between 15 and 18 years old please answer this question: \n Do have parental "
                "consent(yes/no)? \n";
            std::cin >> answer;
            parental_consent = (answer == "yes" || answer == "Yes");
        }
        std::cout << "Have you ever had any accidents? \n";
        std::cin >> answer;
        accidents = (answer == "yes" || answer == "Yes");

        if ((age >= 18 || (age >= 16 && parental_consent)) && ssn && !accidents)
        {

            std::cout << "Yes, you can work.";
        }
        else if (age <= 15)
        {
            std::cout << "Sorry, your age is not eligble!\n";
        }
    }
    else {
        std::cout << "sorry to hear that.";
    }

    std::cout << std::endl << std::endl << std::endl;
    return 0;
}

CodePudding user response:

cin>>answer;
if(answer=="yes"||"Yes"== answer)
ssn=true;



This line will only make the left of the = being true:
ssn = ("yes" || "Yes"); 
  • Related