Селектор меню не принимает допустимый ввод - PullRequest
2 голосов
/ 08 июля 2019

Когда я ввожу «1» (без кавычек) в качестве ввода, я перехожу в меню ошибок (неверный ввод).

Перефразировать: Когда я запускаю программу, в начальном меню приглашения формы или в меню «неверный ввод», я не могу перейти к выбору «1», если ввожу его как ввод.

Кто-нибудь знает, что здесь происходит?

#include <iostream>
#include <string>
#include <limits>

using std::cout;
using std::cin;
using std::string;
using std::endl;

int main()
{
    string username;
    cout<<"Hello.\nMy name is Pythagoras.\nI will be helping you build shapes today.\n\nWhat is your name?\n"<<endl;
    cin>>username;
    int shapeselect;
    cout<<"Hello, "<<username<<".\nWhat shape would you like to build today?\n1)Rectangle/Square\n2)Triangle\n3)Random!\n(Please select a number.)\n"<<endl;
    cin>>shapeselect;

    while (shapeselect!=1||shapeselect!=2||shapeselect!=3)
    {
        cin.clear();
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
        cout<<"Sorry, "<<username<<", your entry was invalid.  Please select a valid number.\n";
        cout<<"What shape would you like to build today, "<<username<<"?\n1)Rectangle/Square!\n2)Triangle!\n3)Random!"<<endl;
        cin>>shapeselect;
    }

    if (shapeselect==1)
    {
        int width;
        cout<<"\nPlease enter the desired WIDTH of the rectangle, between 4 and 10.\n(Please select a number.)\n";
        cin>>width;
        cout<<"\nYou have selected a width of "<<width<<"."<<endl;
        int length;
        cout<<"\nPlease enter the desired LENGTH of the rectangle, between 4 and 10.\n(Please select a number.)\n";
        cin>>length;
        cout<<"\nYou have selected a length of "<<length<<"."<<endl;
        if (width==length)
        {
            cout<<"Please note that you have selected a width of "<<width<<" and a length of "<<width<<".\nNote that this shape will be a square.\n"<<endl;
        }
    }
    return 0;
}

1 Ответ

1 голос
/ 08 июля 2019

Из того, что я понимаю, вам нужен цикл while для выполнения, когда shapeselect является некоторым числом, кроме 1,2,3.

Для решения этой проблемы замените все ИЛИ вентили в состоянии while с И вентилями.

Причина: потому что логический элемент ИЛИ выводит true, когда любой из его входов равен true. Таким образом, если shapeselect равен 1, то он не будет равен 2 и 3, что делает shapeshelect!=2 и shapeselect!=3 оценкой true. Цикл while будет работать независимо от того, какой номер вы вводите.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...