无论用户选择是否键入小写字母,这都会输出大写字母“S”或“P”。
当我在代码中引用其他语句时,输出有效
但是,我想在最终的cout语句中显示STANDARD或PREMIUM。

如何更改char的值以输出STANDARD或PREMIUM?

#include <string>
#include <iostream>

char meal;

cout << endl << "Meal type:  standard or premium (S/P)?  ";
cin >> meal;

meal = toupper(meal);
    if (meal == 'S'){
      meal = 'S';
  }

    else{
      meal = 'P';
}

我尝试过进餐=“标准”,进餐=“高级”
没用

最佳答案

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

int main(int argc, char* argv)
{
    char meal = '\0';
    cout << "Meal type:  standard or premium (s/p)?" << endl;;
    string mealLevel = "";
    cin >> meal;
    meal = toupper(meal);
    if (meal == 'S'){
        mealLevel = "Standard";
    }

    else{
        mealLevel = "Premium";
    }
    cout << mealLevel << endl;
    return 0;
}

关于c++ - C++将char值设置为字符串?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21350470/

10-11 16:29