#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、添加命名空间
2、主函数和大括号对调,并改主函数类型为 int
完整的修改后代码如下:
#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
有一个{位置错误。