让数组与toupper一起工作

Getting Arrays to work with toupper

本文关键字:一起 工作 toupper 数组      更新时间:2023-10-16

由于某种原因,我一直收到一个错误,上面写着"toupper不能用作函数"。但对我来说,understanding toupper是一个全局函数,它可以将小写字符转换为大写字符。

#include <cctype>
#include <iostream>              
#include <string>
using namespace std;                
int main ()
{

  string input;
  string output;
  int toupper;
  cout<<"Enter a String of Charchters to be Capitalized : ";
  cin>>input;
  string arrayinput[20]= input;
  output = toupper(arrayinput);
  cout<<"nnn"<<output<<"nnn";

cout<<"Press <Enter> to Exit";
cin.ignore();
cin.get();      
return 0;                        
}

您已经创建了一个名为int toupper的局部变量-将该变量重命名为其他变量。

编辑以添加:问题不止于此。input是字符串;您想要迭代该字符串的长度,并使用string::at(i)在每个位置获得char*。然后使用atoi将char转换为int,这是toupper作为参数的类型。

如果您想对字符串数组执行此操作,那么在修复变量名问题后,在循环中使用std::transform

for (auto& str : arrayinput)
    std::transform(std::begin(str), std::end(str), std::begin(str), ::toupper);

或者,如果你没有基于的范围,你可以使用for_each:

std::for_each(std::begin(arrayinput), std::end(arrayinput), [](string& str) {
    std::transform(std::begin(str), std::end(str), std::begin(str), ::toupper);
});

您声明了一个与函数同名的变量,从而导致了歧义。如前所述,只需更改变量的名称,就可以解决它。