C++输入 三个数排序问题

请问这是那有问题呢?


#include <iostream>
using namespace std;
int mian()

{   void sort(int x,int y,int z);
    int x,y,z;
    cin >>x>>y>>z;
    sort(x,y,z);
    return 0;
    
    
}
void sort (int x,int y,int z)
{
    int temp;
    if(x>y)    {temp=x;x=y; y=temp;}
    if (z<x)  cout <<z<<','<<x<<','<<y<<endl; 
    else if (z<y) cout <<x<<','<<z<<','<<y<<endl;
    else cout <<x<<','<<y<<','<<z<<endl;
        
            
}

img

你的main写错了,下次要注意
而且你void函数声明为啥在主程序里?
你试试这种改法:

#include <iostream>
using namespace std; 
void sort(int x,int y,int z);
int main()
{  
    int x,y,z;
    cin>>x>>y>>z;
    sort(x,y,z);
    return 0;
}
void sort(int x,int y,int z)
{
    int temp;
    if(x>y)    {temp=x;x=y; y=temp;}
    if (z<x)  cout <<z<<','<<x<<','<<y<<endl; 
    else if (z<y) cout <<x<<','<<z<<','<<y<<endl;
    else cout <<x<<','<<y<<','<<z<<endl;      
}

如果你需要排序的话也可以用sort,C++已经帮你写好的函数

main不是mian
而且
void sort (int x,int y,int z)
这里要用引用
void sort (int &x,int &y,int &z)

 
#include <iostream>
using namespace std;
void sort(int &x,int &y,int &z);
int main()
{
    int x,y,z;
    cin >>x>>y>>z;
    sort(x,y,z);
    cout << x << "," << y << "," << z << endl;
    return 0;
}
void sort(int &x,int &y,int &z)
{
    int temp;
    if(x>y){temp=x;x=y;y=temp;}
    if(y>z){temp=y;y=z;z=temp;}  
    if(x>y){temp=x;x=y;y=temp;}        
}
不知道你这个问题是否已经解决, 如果还没有解决的话:

如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^