Reading package lists... Done
Building dependency tree
Reading state information... Done
freeglut3 is already the newest version.
freeglut3-dev is already the newest version.
0 upgraded, 0 newly installed, 0 to remove and 367 not upgraded.
嗨,我想逐个输入数字,但我得到的输出是......
#include<stdio.h>
int calsum(int x, int y, int z);
void main()
{
int a,b,c,i, sum;
for(i = 1; i <= 3; i++)
printf("\n Enter Number %d\n", i);
scanf("%d %d %d", &a, &b, &c);
sum= calsum(a,b,c);
printf("\nSum=%d", sum);
}
int calsum(int x, int y, int z)
{
int d;
d = x + y + z;
return (d);
}
我希望它一次向我询问输入。请帮我。我是新手。
答案 0 :(得分:0)
您可以使用数组
int a[3], i, sum;
for (i = 0; i < 3 ; i++)
{
printf("\n Enter Number %d\n", i+1);
scanf("%d", &a[i]);
}
sum = calsum(a[0], a[1], a[2]);
答案 1 :(得分:0)
您的for
循环内部只有一行代码,
printf("\n Enter Number %d\n", i);
所以输出正确。
main()
必须返回int
。
scanf()
。你可以使用这样的东西
#include <stdio.h>
int calsum(int x, int y, int z);
int getinteger();
int main()
{
int a, b, c, z;
a = getinteger(1);
b = getinteger(2);
c = getinteger(3);
z = calsum(a, b, c);
printf("\nSum = %d", z);
return 0;
}
int calsum(int x, int y, int z)
{
int d;
d = x + y +z;
return d;
}
int getinteger(int index)
{
int value;
printf("Enter the %dth number > ", index);
while (scanf("%d", &value) != 1)
{
int chr;
while (((chr = getchar()) != '\n') && (chr != EOF))
continue;
printf("Invalid input -- try again\n");
printf("Enter the %dth number > ", index);
}
return value;
}