do while循环中的if语句以yes或no结尾 [英] If statements in a do while loop with a yes or no ending

查看:178
本文介绍了do while循环中的if语句以yes或no结尾的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是编码的新手,我正在尝试使用嵌套的 if 语句执行较长的 do while 循环,但是我在使循环实际执行时遇到问题环形.

I am new to coding and I'm trying to do a long do while loop with nested if statements but I'm having issues with getting my loop to actually loop.

我没有直接在代码很长的项目上获得帮助,而是制作了一个类似它的简单版本.它也不会循环.它将结束并询问用户是否要再次尝试,但是当输入"y"时,它将忽略if语句.

Instead of getting help directly on my project, which has a very long code, I made a simple kind of like it version. It also does not loop. It will get to the end and ask the user if they want to try again but when "y" is entered it ignores the if statements.

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

int main()
{
    string sodaChoice;
    char answer = 'n';

    do
    {
        cout << "Please choose your favorite soda from the following: Rootbeer, Diet Coke, Coke, Sprite" << "\n";
        getline(cin, sodaChoice);

        if (sodaChoice == "Rootbeer")
        {
            cout << "Me too!" << "\n";
        }
        else if (sodaChoice == "Diet")
        {
            cout << "Not me :(" << "\n";
        }
        else if (sodaChoice == "Coke")
        {
            cout << "It's alright" << "\n";
        }
        else if (sodaChoice == "Sprite")
        {
            cout << "only if I'm sick" << "\n";

        }
        else
        {
            cout << "That was not on the list";
        }
        cout << "would you like to try again?";
        cin >> answer;



    } while (answer == 'y'|| answer == 'Y');




    return 0;
}

我想也许我需要在if语句周围的do循环中执行一个do循环,但是后来我不知道该怎么做.任何帮助,将不胜感激.我花了很多小时试图弄清楚.

I thought maybe I needed a do loop in the do loop around the if statements but then I didn't know how to go about it. Any help would be appreciated. I have spent many, many hours trying to figure it out.

我试图问我的老师,但是我的大学从院长根据她的书写的一些通用课程中教书.他对帮助或教学不是很热心.

I tried to ask my teacher, but my college teaches from some generic curriculum that their dean wrote based on her book. He is not very enthusiastic about helping or teaching.

谢谢大家的回答!至于这是一个重复的问题,我尚未学习cin.ignore,所以我不知道它与我的问题有关.谢谢大家教我!

Thank you all for the answers! As for this being a duplicate question, I haven't learned cin.ignore yet, so I wasn't aware that it was relevant to my issue. Thank you all for teaching me!

推荐答案

使用

cin >> answer;

您准确地阅读了一个字符.问题是您在写答案时至少输入了两个个字符.实际的'y''n' plus Enter 键,该键作为换行符添加到输入缓冲区.

you read exactly one character. The problem is that you entered at least two characters when writing the answer. The actual 'y' or 'n' plus the Enter key, which is added as a newline in the input buffer.

然后在下一个 getline 调用中以空行的形式读取此换行符.

This newline is then read by the next getline call as an empty line.

有一些解决此问题的方法,例如使用 ignore 读入 answer .或者,如果您只想输入一个单词,则也可以对 sodaChoice 使用>> 格式化的输入,因为默认情况下它将跳过前导空白(例如换行符.)

There are a few solutions to this problem, for example using ignore after reading into answer. Or if you want only one-word inputs then you could use formatted input using >> for sodaChoice too, since it will by default skip leading white-space (like newlines).

这篇关于do while循环中的if语句以yes或no结尾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆