简体   繁体   English

使用逻辑运算符时,我需要有关此 C++ 是/否问题的帮助

[英]I need help on this C++ yes/no problem while using logical operators

So the problem is: Write a program that prints the question "Do you wish to continue?"所以问题是:编写一个程序,打印问题“你想继续吗?” and reads the input.并读取输入。 If the user input is "Y", "Yes", "YES", then print out "Continuing".如果用户输入的是“Y”、“是”、“是”,则打印出“继续”。 If the user input is "N" or "No", "NO" then print out "Quit".如果用户输入是“N”或“No”,“NO”则打印出“Quit”。 Otherwise, print "Bad Input".否则,打印“错误输入”。 Use logical operators.使用逻辑运算符。

So far this is all the code that I have written.到目前为止,这是我编写的所有代码。 I know that it is not complete, and I do not know what else I need to add to the code.我知道它不完整,我不知道我还需要在代码中添加什么。

#include <iostream>

using namespace std;

int main() {

    char response;

    cout << "Do you wish to continue?" ;
    cin >> response;

    if (response == 'Y'){
        cout << "Continuing";
    }

    else if (response == 'N'){
        cout << "Quit";
    }
    else if (response != 'N' || 'Y'){
        cout << "Bad input";
    }

    return 0;
}

Update: so I edited my code and it is still giving me a bunch of errors.更新:所以我编辑了我的代码,它仍然给我一堆错误。 It's making me frustrated lol.这让我很沮丧,哈哈。 Keep in mind I'm a beginner and we haven't learned loops yet.请记住,我是初学者,我们还没有学习循环。 Sorry for the headache!抱歉头疼!

#include <iostream>
#include <string>
using namespace std;

int main() {

    char response;
    string help;



    cout << "Do you wish to continue?" ;
    cin >> response, help;


    if (response == 'Y' || help == "Yes" || help == "YES"){

        cout << "Continuing";

    }

    else if (response == 'N' || help == "No" || help == "NO"){
        cout << "Quit";
    }
    else if (response != 'N' || response != 'Y' || help != "Yes" || help != "YES" || help != "No" || help != "NO"){
        cout << "Bad input";
    }

    return 0;
}

First off I think this is a great start.首先,我认为这是一个很好的开始。 Sounds like you are new to C++ so here are some suggestions:听起来你是 C++ 的新手,所以这里有一些建议:

1) Your response variable can only contain a character. 1) 您的响应变量只能包含一个字符。 I would suggest including string and changing the response to take a string from the user for 'Y', "Yes", etc.我建议包括字符串并更改响应以从用户那里获取字符串,例如“Y”、“是”等。

2) I suggest wrapping your code in a while loop with an exit condition. 2)我建议将您的代码包装在带有退出条件的 while 循环中。

3) Each of your logic branches should include a return integer. 3)您的每个逻辑分支都应包含一个返回整数。 This will give the program an exit condition if the logical conditions are met.如果满足逻辑条件,这将为程序提供退出条件。

I know I haven't given you the answers fully.我知道我没有完全给你答案。 If you are truly stuck, reply back and we can walk through.如果您真的卡住了,请回复,我们可以走过去。

A simple way is to simply convert the user's answer to uppercase or lowercase.一种简单的方法是将用户的答案简单地转换为大写或小写。 By doing this, you can simply use the lower case.通过这样做,您可以简单地使用小写字母。 For your loop, you could for example use a "do..while".例如,对于您的循环,您可以使用“do..while”。

#include <iostream>
#include <string>
using namespace std;

int main() {
    int stop = 0;
    string response;

    //Continue until the user choose to stop.
    do{
        //-------------
        // Execute your program
        //-------------
        cout << "Do you wish to continue? ";
        cin >> response;

        //-------------
        //Convert to lower case
        for (string::size_type i=0; i < response.length(); ++i){
            response[i] = tolower(response[i]);
        }
        //-------------
        //Check the answer of the user.
        if (response.compare("y") == 0 || response.compare("yes") == 0){
            cout << "Continuing \n";
        }
        else if (response.compare("n") == 0 || response.compare("no") == 0){
            cout << "Quit \n";
            stop = 1;
        }
        else{
            cout << "Bad input \n";
        }

    }while(stop == 0);

    return 0;
}

Like you said in the question, we care about Y,Yes,YES,N,No and NO.就像您在问题中所说的那样,我们关心 Y、Yes、YES、N、No 和 NO。 For anything else we need to print "Bad Input".对于其他任何事情,我们都需要打印“Bad Input”。 Think about how you'd be storing these responses (hint: Sam Varshavchik's answer).想想您将如何存储这些响应(提示:Sam Varshavchik 的回答)。

Once you've taken care of extracting user input, you'd want to check what the user actually entered and proceed accordingly.处理完用户输入的提取后,您需要检查用户实际输入的内容并进行相应操作。 From your question it seems "if else" would do.从你的问题来看,“如果其他”似乎可以。 You need to change the conditionals for your "if else ifs" because you have 3 conditions for one type of response: Y, Yes and YES need one output - "continuing" while N, No and NO require a different output - "Quit" and for all others we print "Bad input".您需要更改“if else ifs”的条件,因为对于一种类型的响应有 3 个条件:Y、Yes 和 YES 需要一个输出——“继续”,而 N、No 和 NO 需要不同的输出——“退出”对于所有其他人,我们打印“错误输入”。 Think about what your conditionals should be and your if statement should look something like:想想你的条件应该是什么,你的 if 语句应该是这样的:

    if (response == "Y" || response == "Yes" || response == "YES")

and then handle the case accordingly.然后根据情况处理。 You'd want to do the same for your No conditions and finally handle the case for all others.您希望对 No 条件执行相同的操作,并最终处理所有其他情况。 I'd suggest having your code like so:我建议你的代码是这样的:

    if( conditionals for Yes){
      //Code for Yes input
    }

    else if( conditionals for No){
      //Code for No input
    }

    else{
      //Code for all other inputs
    }

It is tempting to give you the full answer but think about how your program needs to flow and proceed from there, you've almost got it!给你完整的答案很诱人,但想想你的程序需要如何流动并从那里开始,你几乎已经明白了!

If you have more questions post here and we'd be glad to help!如果您有更多问题,请在此处发帖,我们很乐意为您提供帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM