do while循环,其中选项在c++中使用topper

do while loop where the choices use toupper in C++

本文关键字:c++ topper 选项 循环 do while      更新时间:2023-10-16

是否有一种简单的方法将字符选择从小写q转换为大写q ?

我试过c版本的toupper,但我不能让它在c++中工作。我需要输入的所有字符都是大写的;因此,它们链接到main中的choice。

例如,如果他们输入c,它会被转换为c,并且c所链接的函数可以被访问或使用。

代码到目前为止没有任何改变:

include <iostream>
#include <stdlib.h>
#include <string>
#include "link.h"
using namespace std;
int main()
{
    link obr;
    string n;
    long int x;
    char choice;
    do{
    cout << "C: Create/Addn P: DisplaynQ: Quit";
    cin >> choice;
      if(choice == 'C'){
                cout << "Name";
                cin >> n;
                cin >> x;
                obr.push(n,x);
        }
    if (choice == 'P'){
        obr.display();
    }
} while(choice != 'Q');
    return 0;
}

如果你能确保你的字符集与C的toupper()兼容,那么你可以做得很简单:

std::string s = "this is a string";
std::transform(s.begin(), s.end(), s.begin(), ::toupper);

直接写

#include <cctype>
//...
choice = std::toupper( ( unsigned char )choice );

设变量choice的类型为char

您应该确保选项确实包含alpha字符而不是控件符号。