代码有警告没有错误,运行结果和预期完全不一样,不知道怎么改正⊙︿⊙

原本代码是想实现两个数互换的,但是我不知道为什么实现不了,计算机入门新生瑟瑟发抖,请大家帮我解答一下(⋟﹏⋞)
代码如下:
#include<stdio.h>
void swap1(int *a,int *b)
{
int temp;
temp=*a;
*a=*b;
*b=temp;
printf("%d,%d\n",a,b);

}

void swap2(int &a,int &b)
{
int temp;
temp=a;
a=b;
b=temp;
printf("%d,%d\n",a,b);

}
int main()
{
int x,y;
printf("请输入两个整数:\n");
scanf("%d,%d\n",&x,&y);
swap2(x,y);
printf("%d,%d\n",x,y);
}

两处小细节错误
第一处:输出的整数值,可是上面直接输出了指针变量,是不对的,因此将框中
a,b改为*a,*b
第二处:

img


scanf格式输入,加了格式,要符合格式来,按题主所写,需要每次输入数据时都要加上逗号,和换行。
因此此处将scanf("%d,%d\n",&x,&y);改为scanf("%d%d",&x,&y);

img


改正如下:

#include<stdio.h>
void swap1(int *a,int *b)
{
int temp;
temp=*a;
*a=*b;
*b=temp;
printf("%d,%d\n",*a,*b);

}

void swap2(int &a,int &b)
{
int temp;
temp=a;
a=b;
b=temp;
printf("%d,%d\n",a,b);

}
int main()
{
int x,y;
printf("请输入两个整数:\n");
scanf("%d%d",&x,&y);
swap2(x,y);
printf("%d,%d\n",x,y);
}

scanf()中的换行去掉。
注意:输入的两个数之间用逗号分隔。
如:

3,5
#include<stdio.h>
void swap1(int *a,int *b)
{
int temp;
temp=*a;
*a=*b;
*b=temp;
printf("%d,%d\n",a,b);
}
void swap2(int &a,int &b)
{
int temp;
temp=a;
a=b;
b=temp;
printf("%d,%d\n",a,b);
}
int main()
{
int x,y;
printf("请输入两个整数:\n");
scanf("%d,%d",&x,&y);
swap2(x,y);
printf("%d,%d\n",x,y);
}
您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632