如何在C ++中自动将字符串值大写[重复]

问题描述 投票:0回答:1

此问题已经在这里有了答案:

是否有一种自动将用户输入大写的方法?我试图用C ++编写学生成绩的if语句:

int main() {
  string grade;

  cout << "Insert your grade: ";
  cin >> grade;

  if (grade == "A") {
    cout << "You got a great grade\n";
  } else if (grade == "B") {
    cout << "You got a good grade\n";
  } else if (grade == "C") {
    cout << "You got a nice grade\n";
  } else if (grade == "D") {
    cout << "You got a bad grade\n";
  } else if (grade == "E") {
    cout << "You got a terrible grade\n";
  } else {
    cout << "Please Input a valid grade\n";
  }

仅当用户以正确的大写形式输入等级时,此代码才有效。因此,无论如何,我可以自动将小写字母转换为大写字母,例如python中的upper()方法吗?

c++ string c-strings string-literals toupper
1个回答
5
投票

用途

#include <cstring>

//...

grade[0] = std::toupper( ( unsigned char )grade[0] );
© www.soinside.com 2019 - 2024. All rights reserved.