我应该做哪些更改,以便当我输入"a"时它说面包,当我输入时"b"它说葡萄酒?
What changes should I do so that when I enter "a" its says bread and when I enter "b" it says wine?
这是我的代码。我在if语句上输入的公式不起作用。
#include <iostream>
using namespace std;
int main(){
int value;
cout << "a) Bread" << endl;
cout << "b) Wine" << endl;
cout << endl;
cout << "Please enter the letter of the type of product you want to buy: " << endl;
cin >> value;
if(value == a){
cout << "You chose bread";
} else{
cout << "You chose wine";
}
return 0;
}
#include <iostream>
using namespace std;
int main(){
char value; // int => char
cout << "a) Bread" << endl;
cout << "b) Wine" << endl;
cout << endl;
cout << "Please enter the letter of the type of product you want to buy: " << endl;
cin >> value;
if(value == 'a'){ // a => 'a'
cout << "You chose bread";
} else{
cout << "You chose wine";
}
return 0;
}
您应该始终包括您在代码中遇到的错误,以及您尝试解决问题的一切。
您的错误是,如果您想检查'a'
,则需要用单个引号包围它。您正在尝试将value
与称为a
的不存在的变量进行比较。
相关文章:
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 2D数组来自文本输入,中间有空格
- 如何使用 < 和 > 命令获取 c++ 中的输入和输出?
- 检查输入是否不是整数或数字
- 正在尝试了解输入验证循环
- 读取文件并输入到矢量中
- C++如何通过用户输入删除列表元素
- 用c++从输入文件中读取另一行
- 读取文件的最后一行并输入到链接列表时出错
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- 如何使用用户输入在C++中正确填充2D数组
- C++MySQL C api用户输入行
- 输入到文件并输出到另一个文件,并将流文件传递给函数
- 用户定义函数中的指针和输入
- 如何在C++中检查2D数组中负值的输入验证
- 如何只允许用户输入正整数
- 在while循环中输入带有std::cin的字符串后,控制台会输出大量胡言乱语
- 输入中的字符串数未知(以字母表示)
- cpp二进制搜索问题,计算给定数组中输入元素的出现次数
- 我应该做哪些更改,以便当我输入"a"时它说面包,当我输入时"b"它说葡萄酒?