鱼C论坛

 找回密码
 立即注册
查看: 382|回复: 2

[已解决]c++的返回值问题

[复制链接]
发表于 2020-4-11 23:42:44 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 FISHER666 于 2020-4-11 23:45 编辑

报错:invalid conversion from 'char*' to 'char',问题在27行开始的那个函数

  1. /*
  2. 题目要求:编写一个函数,用数组形式接受字符串。
  3.           这个函数将字符串的所有单词的第一个字母转化为大写(如果已经是大写则保留)
  4.           让用户输入一个字符串,然后将字符串作为一个参数传递给子函数,最后将变换后的最终结果显示。
  5. */

  6. # include <iostream>
  7. using namespace std;
  8. # include <cstring>

  9. // 声明转换函数
  10. char switch_letter(char str[]);

  11. // 主函数部分
  12. int main()
  13. {
  14.         char str[100];
  15.        
  16.         cout<<"请输入一段字符串,我来帮你转换第一个字符为大写"<<endl;
  17.         cin.getline(str,100);
  18.         cout<<"最终的输出结果是"<<switch_letter(str)<<endl;
  19.        
  20.         system("pause");
  21.         return 0;
  22. }

  23. char switch_letter(char str[])
  24. {
  25.         // 新建一个与输入字符串等长的字符串
  26.         char str_out[100];
  27.         strcpy(str_out, str);
  28.         char letter1 = str[0];
  29.         if(isupper(letter1))
  30.         {
  31.                 strcpy(str_out, str); // 保留原字符串
  32.         }
  33.         else
  34.         {
  35.                 str_out[0] = char(int(letter1)-32);
  36.         }
  37.        
  38.         return str_out;
  39. }
复制代码
最佳答案
2020-4-11 23:50:50
/*
题目要求:编写一个函数,用数组形式接受字符串。
          这个函数将字符串的所有单词的第一个字母转化为大写(如果已经是大写则保留)
          让用户输入一个字符串,然后将字符串作为一个参数传递给子函数,最后将变换后的最终结果显示。
*/

# include <iostream>
using namespace std;
# include <cstring>

// 声明转换函数
char* switch_letter(char str[]);

// 主函数部分
int main()
{
        char str[100];
      
        cout<<"请输入一段字符串,我来帮你转换第一个字符为大写"<<endl;
        cin.getline(str,100);
        cout<<"最终的输出结果是"<<switch_letter(str)<<endl;
      
        system("pause");
        return 0;
}

char* switch_letter(char str[])//返回结果不能是char数组,应该用指针,同时,不该返回局部变量的地址
{
        // 新建一个与输入字符串等长的字符串
        char str_out[100];
        strcpy(str_out, str);
        char letter1 = str[0];
        if(isupper(letter1))
        {
                strcpy(str_out, str); // 保留原字符串
        }
        else
        {
                str_out[0] = char(int(letter1)-32);
        }
      
        strcpy(str, str_out);//不能返回局部变量的地址,所以再次赋值
        return str;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-4-11 23:50:50 | 显示全部楼层    本楼为最佳答案   
/*
题目要求:编写一个函数,用数组形式接受字符串。
          这个函数将字符串的所有单词的第一个字母转化为大写(如果已经是大写则保留)
          让用户输入一个字符串,然后将字符串作为一个参数传递给子函数,最后将变换后的最终结果显示。
*/

# include <iostream>
using namespace std;
# include <cstring>

// 声明转换函数
char* switch_letter(char str[]);

// 主函数部分
int main()
{
        char str[100];
      
        cout<<"请输入一段字符串,我来帮你转换第一个字符为大写"<<endl;
        cin.getline(str,100);
        cout<<"最终的输出结果是"<<switch_letter(str)<<endl;
      
        system("pause");
        return 0;
}

char* switch_letter(char str[])//返回结果不能是char数组,应该用指针,同时,不该返回局部变量的地址
{
        // 新建一个与输入字符串等长的字符串
        char str_out[100];
        strcpy(str_out, str);
        char letter1 = str[0];
        if(isupper(letter1))
        {
                strcpy(str_out, str); // 保留原字符串
        }
        else
        {
                str_out[0] = char(int(letter1)-32);
        }
      
        strcpy(str, str_out);//不能返回局部变量的地址,所以再次赋值
        return str;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-12 00:03:34 | 显示全部楼层
奇宝 发表于 2020-4-11 23:50
/*
题目要求:编写一个函数,用数组形式接受字符串。
          这个函数将字符串的所有单词的第一个字母 ...

太感谢了!!!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-23 19:20

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表