使用用户输入(如YES和NO)来控制C ++中的程序流 [英] using user input such as YES and NO to control program flow in C++

查看:345
本文介绍了使用用户输入(如YES和NO)来控制C ++中的程序流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个使用if else语句的小程序,但不是使用数字来控制流程,我希望能够使用yes和no来控制工作;



例如:

  cout< 你想继续吗? << endl; 
cout<< \ nYES或NO< endl;
int input = 0;
cin>>输入;
string Yes =YES;
string No =NO;

if(input == no)
{
cout< testone< endl;
}
if(input == yes)
{
cout< test two<< endl;
//程序的其余部分在这里我猜?
}
else
{
cout< 你输错了东西,重新开始< endl;
//可能是某种类型的循环结构返回
}

我似乎没有得到任何变化的工作,我可以让用户输入0或1,但这似乎真的很蠢,我宁愿它是尽可能自然,用户不会说数字?



也需要能够简单地添加更多的单词,例如没有没有没有没有n都将意味着没有



希望这有一些意义



我也喜欢使用一个窗口,但我只学习基本的c ++到目前为止没有

解决方案

你不是在一个 string ,您正在阅读 int



 字符串输入; 

而不是

  int input = 0; 

此外,C ++区分大小写,因此您不能定义,然后尝试使用 yes



btw,您的第二个如果语句应为 else if ,否则如果你输入NO,那么它仍然会进入最后 else $ b

I'm making a small program that uses a if else statement, but instead of using numbers to control the flow i want to be able to make the control work with with yes and no;

for example:

cout << "would you like to continue?" << endl;
cout << "\nYES or NO" << endl;
int input =0;
cin >> input;
string Yes = "YES";
string No = "NO";

if (input == no)
{
    cout << "testone" << endl;
}
if (input == yes)
{
    cout << "test two" << endl;
         //the rest of the program goes here i guess?
}
else
{
    cout <<  "you entered the wrong thing, start again" << endl;
              //maybe some type of loop structure to go back
}

but I can't seem to get any variations of this to work, i could make the user type a 0 or 1 instead but that seems really stupid, i'd rather it be as natural as possible, users don't speak numbers do they?

also i need to be able to simply add more words, for example "no NO No noo no n" all would have to mean no

hopefully that makes some sense

also i would love to make this using a window but i've only learned basic c++ so far not even that and i cant find any good resources online about basic windows programming.

解决方案

You're not reading in a string, you're reading in an int.

Try this:

string input;

instead of

int input = 0;

Also, C++ is case-sensitive, so you can't define a variable called Yes and then try to use it as yes. They need to be in the same case.

btw, your second if statement should be an else if, otherwise if you type in "NO" then it will still go into that last else block.

这篇关于使用用户输入(如YES和NO)来控制C ++中的程序流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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