【发布时间】:2016-11-11 03:49:51
【问题描述】:
我是 C 的初学者(学习免费资源),我正在尝试编写一个简单的代码来将用户输入的一组数字添加到一组数组中......我做错了什么?
int numbers;
int number=1;
int counter = 0;
int pot[100];
int calculate;
int result;
printf("how many numbers do you want to calculate?:"); //asking user how many different integers would be calculated
scanf("%d",&numbers);
while (counter < numbers)
{
printf("input number %d\n", number);//asking user to input figures to be calculated
scanf("%d",&pot[0]+1); //user would input values into spaces in the array
counter++;
number++;
}
printf("Please press 1 for addition and 2 for multiplication");
scanf("%d",&calculate);
switch (calculate)
{
case 1: result = pot[0]+ pot[0]+1;//this is supposed to add all the individual values within the arrays
printf("the result is %d", result);
break;
case 2: result = pot[0]* pot[0]+1;//this is supposed to multiply all the individual values within the arrays
break;
}
return 0;
【问题讨论】:
-
输入、预期输出和实际输出是多少?
-
什么是
&pot[0]+1? -
抱歉,我不明白这个问题(新手问题)!
-
pot[0]+1 是我试图告诉 C 将用户输入的值分配给数组中的下一个可用空间...假设第一个空间是 pot[0],因此下一个是 pot[1] 等。
-
(1) 检查
numbers是否为<100。 (2) 检查scanf的返回:while(counter < numbers) { if(scanf("%d", &(pot[counter])) != 1) return 1; counter++; },刚刚开始。请注意,变量counter是您数组的订阅索引。