switch 语句中从 'const char*' 到 'char' 的转换无效

Invalid conversion from 'const char*' to 'char' in switch statement

本文关键字:char 转换 无效 语句 switch const      更新时间:2023-10-16

我正在尝试制作一个切换语句,该语句采用char数组的第一个字符并大写。我仍然是一个初学者,所以我不知道我在做什么错!我遇到的错误是:

error: invalid conversion from 'const char*' to 'char' [-fpermissive]
    return "A";

这是我的代码。

#include <iostream>
using namespace std;
char capitalize(char x)
{
    switch(x)
    {
        case a:
            return "A";
            break;
        default:
        break;
    }
}
int main()
{
    char name[50];
    cout << "What is your name?" << endl;
    cin.getline(name, 49);
    cout << "Hello there, " << name << "." << endl;
    return 0;
}

我很抱歉,如果这似乎是一个愚蠢的问题。

return 'A';

替换 return "A";
  • 'A'的类型 - char,它是一个ASCII字符
  • "B"的类型 - const char *,这是一个无效的ascii字符串
  • L'C'的类型 - wchar_t,它是一个单一字符
  • L"D"的类型 - const wchar_t*,这是一个无效的Unicode字符串