求解这个c++程序哪里错了。(从键盘上输入3个字符串,从大到小排序进行输出。利用拷贝函数和比较函数实现

#include <iostream>
#include <string>
{ void main()
char str1[6], str2[6], str3[6], temp[6];
cout<<"Please input 3 strings:"<<endl;
cin>>str1>>str2>>str3;
if( strcmp(str1,str2) < 0)
{
strcpy(temp, str1);
strcpy(str1, str2);
strcpy(str2, temp);
}
if( strcmp(str2,str3) < 0)
{
strcpy(temp, str2);
strcpy(str2, str3);
strcpy(str3, temp);
if( strcmp(str1,str2) < 0)
{
strcpy(temp, str1);
strcpy(str1, str2);
strcpy(str2, temp);
}
}
cout<<"str1:"<<str1<<endl;
cout<<"str2:"<<str2<<endl;
cout<<"str3:"<<str3<<endl;

return 0;
}

程序修改的地方:

    1、添加命名空间

        using namespace std;   // 添加命名空间引用

    2、主函数和大括号对调,并改主函数类型为 int 

        int  main()           // 主函数和大括号的位置对调一下 
        {  

完整的修改后代码如下:

#include <iostream>
#include <string>
using namespace std;   // 添加命名空间引用 

int  main()           // 主函数和大括号的位置对调一下 
{
 char str1[6], str2[6], str3[6], temp[6];
 cout<<"Please input  3 strings:"<<endl;
 cin>>str1>>str2>>str3;   
if( strcmp(str1,str2) < 0)
 {
  strcpy(temp, str1);
  strcpy(str1, str2);
  strcpy(str2, temp);
 }
 if( strcmp(str2,str3) < 0)
 {
  strcpy(temp, str2);
  strcpy(str2, str3);
  strcpy(str3, temp);
  if( strcmp(str1,str2) < 0)
  {
   strcpy(temp, str1);
   strcpy(str1, str2);
   strcpy(str2, temp);
  }
 }
 cout<<"str1:"<<str1<<endl; 
 cout<<"str2:"<<str2<<endl; 
 cout<<"str3:"<<str3<<endl;
 
 return 0;
}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-09-25

#include <iostream>
#include <string>

using namespace std;

int  main()
{
 
 char str1[6], str2[6], str3[6], temp[6];
 cout<<"Please input  3 strings:"<<endl;
 cin>>str1>>str2>>str3;  
 if( strcmp(str1,str2) < 0)
 {
  strcpy(temp, str1);
  strcpy(str1, str2);
  strcpy(str2, temp);
 }
 if( strcmp(str2,str3) < 0)
 {
  strcpy(temp, str2);
  strcpy(str2, str3);
  strcpy(str3, temp);
  if( strcmp(str1,str2) < 0)
  {
   strcpy(temp, str1);
   strcpy(str1, str2);
   strcpy(str2, temp);
  }
 }
 cout<<"str1:"<<str1<<endl;
 cout<<"str2:"<<str2<<endl;
 cout<<"str3:"<<str3<<endl;
 
 return 0;
}

    cout,cin等是定义在命名空间std中,所以需要添加

    函数中用return 0;返回时,函数返回值类型不能为void,更改为int

    有一个{位置错误。

第2个回答  2013-09-25
#include <iostream>
#include <string>
using namespace std;

int main()
{
char str1[6], str2[6], str3[6], temp[6];
cout<<"Please input 3 strings:"<<endl;
cin>>str1>>str2>>str3;
if( strcmp(str1,str2) < 0)
{
strcpy(temp, str1);
strcpy(str1, str2);
strcpy(str2, temp);
}

if( strcmp(str2,str3) < 0)
{
strcpy(temp, str2);
strcpy(str2, str3);
strcpy(str3, temp);
}
if( strcmp(str1,str2) < 0)
{
strcpy(temp, str1);
strcpy(str1, str2);
strcpy(str2, temp);
}
cout<<"str1:"<<str1<<endl;
cout<<"str2:"<<str2<<endl;
cout<<"str3:"<<str3<<endl;
return 0;
}本回答被提问者采纳
相似回答