无法将int添加到int. [英] Cannot add int to a int.
问题描述
我正在编写一个程序来查找输入数字的平均值,但是我很难获取要显示的所有数字的总和.您能帮我用我编写的这段代码吗?
I am writing a program to find the average value of numbers entered but I am having difficulty getting the total value of all the numbers to show. Could you please help me with this code I have written?
谢谢
//program to find the average of values entered
#include <stdio.h>
int main()
{
int sumOfValues = 0;
int numberOfValueEntered = 0;
int storage = 0;
float averageOfValues = 0.0;
printf("Enter a number or enter 0 to find the average of numbers entered\n");
scanf("%i", &storage ); // reads the value and saves it in storage
while (storage != 0) // when storage is not 0 carry out loop
{
++numberOfValueEntered ; //adds one to numberOfValueEntered
(storage += sumOfValues ); //adds the value of storage to sumOfValues
printf("%i,%i" ,numberOfValueEntered,sumOfValues); //test to see if numberOfValueEntered,sumOfValues is working correctly
storage = 0; // return value of starage to 0
printf("Please enter the next number or type 0 to exit\n");
scanf("%i", &storage ); // reads the value and saves it in storage
}// exits the loop when 0 is entered
averageOfValues = (sumOfValues / numberOfValueEntered); //works out the average and stores it in averageOfValues
//prints all the values averages etc
printf("The results are as follows\n");
printf("you have entered %d numbers, The total value of numbers entered is %i, and the average is %f",
numberOfValueEntered,sumOfValues, averageOfValues);
return 0;
}
在尝试了答案之后,sumOfValues的值仍为0.有任何想法吗?
After trying the answers I am still getting a value of 0 for sumOfValues. Any ideas?
推荐答案
这正常工作
int sumOfValues = 0;
int numberOfValueEntered = 0;
int storage;
float averageOfValues;
printf("Enter a number or enter 0 to find the average of numbers entered: ");
scanf("%i", &storage ); // reads the value and saves it in storage
while (storage != 0) // when storage is not 0 carry out loop
{
++numberOfValueEntered ; //adds one to numberOfValueEntered
sumOfValues += storage; //adds the value of storage to sumOfValues
printf("%i, %i\n" ,numberOfValueEntered,sumOfValues); //test to see if numberOfValueEntered,sumOfValues is working correctly
printf("Please enter the next number or type 0 to exit: ");
scanf("%i", &storage ); // reads the value and saves it in storage
}// exits the loop when 0 is entered
averageOfValues = (float)sumOfValues / (float)numberOfValueEntered; //works out the average and stores it in averageOfValues
//prints all the values averages etc
printf("The results are as follows\n");
printf("you have entered %d numbers\nThe total value of numbers entered is %i, and the average is %f\n",
numberOfValueEntered, sumOfValues, averageOfValues);
此行:
(storage += sumOfValues ); //adds the value of storage to sumOfValues //
错误.它的意思是:
is wrong. it translates to:
storage = storage + sumOfValues
我怀疑您的意思是:
sumOfValues = sumOfValues + storage.
,它的写法是这样的:
which would be written like this:
(sumOfValues += storage);
感谢您的答复,我已将其添加到我的代码中,但输入的总数仍为0,这反过来又使我平均获得了0.0,
thank you for the reply i have added it into my code and i am still getting a value of 0 for the total numbers entered which in turn is giving me an average of 0.0,
我不明白为什么它不会将值读入int.
i cant understand why it will not read the value into the int..
这篇关于无法将int添加到int.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!