Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/148.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用不正确的鞋面?工作代码,直到我进入toupper_C++_Toupper - Fatal编程技术网

C++ 使用不正确的鞋面?工作代码,直到我进入toupper

C++ 使用不正确的鞋面?工作代码,直到我进入toupper,c++,toupper,C++,Toupper,我的程序按预期运行,直到我将toupper部分添加到程序中。我试着查看我的错误代码,但它并没有真正起到帮助作用。错误是: 没有要调用的匹配函数 需要2个参数,提供1个 所以我知道错误在while循环中的这两条语句中。我做错了什么 我想成为一个像这样的名字 约翰布朗 去 约翰布朗 #包括 #包括 #包括 #包括 使用名称空间std; int main(){ 字符串firstname[5]; 字符串lastname[5]; ifstream fin(“data_names.txt”); 如果(!fi

我的程序按预期运行,直到我将toupper部分添加到程序中。我试着查看我的错误代码,但它并没有真正起到帮助作用。错误是:

没有要调用的匹配函数 需要2个参数,提供1个

所以我知道错误在while循环中的这两条语句中。我做错了什么

我想成为一个像这样的名字

约翰布朗 去 约翰布朗

#包括
#包括
#包括
#包括
使用名称空间std;
int main(){
字符串firstname[5];
字符串lastname[5];
ifstream fin(“data_names.txt”);
如果(!fin){
cout firstname[i])&(fin>>lastname[i])){
firstname[0]=toupper(firstname[0]);
lastname[0]=toupper(lastname[0]);
i++;
}

cout您需要ctype.h来获得
toupper()
的正确定义。它通常不是作为函数实现的,而是作为数组映射实现的

 #include <ctype.h>
#包括

该程序有几个缺陷:使用字符串数组而不是字符串,没有正确遍历字符串,没有声明但使用toupper()的C定义,当文件不存在时没有退出

改用这个:

#include <ctype.h>
#include <iostream>
#include <string>
using namespace std;

int main ()
{
  ifstream fin ("data_names.txt");
  if (!fin) 
  {
    cerr << "File missing" << endl;
    return 1;
  }
  // not sure if you were trying to process 5 lines or five words per line
  // but this will process the entire file
  while (!fin.eof())
  {
       string s;
       fin >> s;
       for (i = 0;  i < s.length();  ++i)
            s [i] = toupper (s [i]);
       cout << s << endl;
  }
  return 0;
}
#包括
#包括
#包括
使用名称空间std;
int main()
{
ifstream fin(“data_names.txt”);
如果(!fin)
{
cerr s;
对于(i=0;i#include
,您还需要修改
,而
循环的主体:

firstname[i][0] = toupper(firstname[i][0]);
lastname[i][0] = toupper(lastname[i][0]);
i++;
那么它应该像预期的那样工作

正如M.M在评论中指出的,在访问字符串的第一个字符之前,您还应该检查字符串是否为空,例如

if (!firstname[i].empty()) firstname[i][0] = toupper(...);
强烈推荐


请注意,如果你得到像麦当劳这样的名字,你可能需要更复杂的逻辑。
toupper
:)

错误指向哪一行?
toupper
需要一个
int
你在给它传递一个
字符串。
toupper
一次只能处理一个字符。如果你想对整个字符串进行上限化,你不需要o制作一个循环,通过字符串将
toupper
应用到每个character@M.M我只想将这两个字母大写。我如何突出显示字符串中的这些字符并正确地执行它?@JOHNSMITH8338哪两个字母?我相信现在它通常作为一个函数来实现,因为很多人称它为传递普通
char
作为参数(在数组实现中中断)BTW,C++头将是<代码> ccType < /C>,然后使用字符串IN/COD>时,预计<<代码> int >代码> @ @ JNYNYNY是的,它会。这将是一个不同的错误。e确实在尝试,但是
lastname[0]
是一个
std::string
。要获取它的第一个字符,您需要
lastname[0][0]
。不知道为什么要使用字符串数组。从技术上讲,它应该是
toupper((无符号字符)firstname[i][0]);
(包括cctype),或者
toupper(firstname[i][0],std::locale())
。另外,检查字符串是否为空以执行
[0]
@M.M我同意。问题的代码可以从更全面的审查中受益。@Mindrio抱歉,我错过了关于包含
cctype
@juanchopanza的部分实际上,看起来
cctype
甚至不是严格必要的(),它似乎被另一个标题拉了进来。@Mindrio是的,但这不可靠。顺便说一句,这就是另一个过载被拉进来的原因。
if (!firstname[i].empty()) firstname[i][0] = toupper(...);