我应该做哪些更改,以便当我输入"a"时它说面包,当我输入时"b"它说葡萄酒?

What changes should I do so that when I enter "a" its says bread and when I enter "b" it says wine?

本文关键字:输入 面包 葡萄酒 便当 我应该      更新时间:2023-10-16

这是我的代码。我在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的不存在的变量进行比较。