当我尝试编译我的程序时,提示“无法将参数 ‘1’ 的 ‘std::string’ 转换为 ‘int’ 到 ‘int toupper(int)’”
#include <iostream>
#include <cstring>
using namespace std;
int main(){
string names[10];
for (int i=0;i<=9;i++){
cout<<"Please enter name for student "<<i+1<<": ";
cin>>names[i];
}
for(int j=0;j<=9;j++){
names[j]=toupper(names[j]);
cout<<names[j]<<endl;
}
return 0;
}
最佳答案
您必须将 char
传递给 std::toupper
而不是整个 string
:
for (auto &x : names)
std::transform(x.begin(), x.end(), x.begin(), ::toupper);
关于c++ - 我如何将 C++ 中数组中名称的每个字母大写?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23596653/