[英]Printing wrong values
I am trying to build a program that outputs No. of people I have entered and the average of their ages. 我正在尝试构建一个程序,以输出我输入的人数和平均年龄。 This is my code
这是我的代码
#include <iostream>
#include <string>
using namespace std;
int main()
{
int age;
int total = 0;
int No_of_People = 0;
while (age != -1){
total = total + age;
No_of_People++;
cout << "Enter a person's age or enter -1 to stop\n";
cin >> age;
}
cout << "Number of people entered:\n" <<No_of_People<< endl;
cout << "Average age of people = "<< total/No_of_People;
return 0;
}
However the computer prints the average wrong, anyone know what i did wrong? 但是,计算机平均打印错误,有人知道我做错了吗? This is the output
这是输出
I see two major problems in your code: First, age
is not initialized. 我在您的代码中看到两个主要问题:首先,
age
没有初始化。 Reading from it leads to undefined behavior (as mentioned by UnholySheep). 读取它会导致未定义的行为(如UnholySheep所述)。 Everything could happen from some seemingly random value to an access violation.
一切都可能发生,从某些看似随机的值到访问冲突。 I once forgot to initialize a boolean variable, it was initialized with
false
on my computer every time I ran the program (like I intended), on another team members it was initialized to true
and we wondered why it's working for me but not for him. 我曾经忘记初始化一个布尔变量,每次我运行该程序时(就像我想要的那样)在我的计算机上将其初始化为
false
,在另一个团队成员中,该变量初始化为true
,我们想知道为什么它对我有用但对他不起作用。 So best initialize it with 0 like you do for total
. 因此,最好像对
total
一样用0进行初始化。
Second, you're adding age
on total
before knowing its value. 其次,您要添加
age
在total
知道它的价值了。 So when you set age
to 0 in the beginning, you will increase the number of people one more time than you increase the total age. 因此,当您开始将
age
设置为0时,与增加总年龄相比,增加的人数就会增加一倍。 Add age
to total
after asking for a value. 要求值后,将
age
加到total
。
A third thing is, that you don't take care for -1
properly. 第三件事是,您没有适当照顾
-1
。 You're increasing the number of people even if -1
is typed in. You should check for that value before increasing the number of people or adding it to your total. 即使输入
-1
,您也在增加人数。您应该在增加人数或将其添加到总数之前检查该值。
Just thought I would add to Tobias Brösamle's answer. 只是想我会补充TobiasBrösamle的答案。
A good hint that you did not initialize age
might be that running the program multiple times with the same input values yields different results. 一个很好的暗示,您没有初始化
age
可能是使用相同的输入值多次运行该程序会产生不同的结果。
Have a look at these modifications, comments highlight changed code. 看看这些修改,注释突出显示已更改的代码。
Mostly I see that you're a beginner in coding, your errors can be attributed to not quite developing a "working methodology" - ie always initialising variables. 通常,我认为您是编码的新手,您的错误可以归因于未完全开发“有效的方法”,即始终初始化变量。 I also moved the code into a separate function, whenever I put test code in a main function it very quickly develops into needing to do this anyway.
我还将代码移到一个单独的函数中,每当我将测试代码放入主函数中时,它很快就会发展为需要执行此操作的过程。
#include <string>
#include <iostream>
using namespace std;
void age()
{
int age = 0; // not initalised
int total = 0;
int count = -1; // naming convention of previous variable didn't match other variables
do // loop terminates at bottom
{
cout << "Enter a person's age or enter 0 to stop\n"; // why \n here instead of endl?
cin >> age;
total += age;
count++;
} while (age > 0); // slightly easier code to write if 0 terminates instead of -1
cout << "Number of people entered:\n" << count << endl;
cout << "Average age of people = " << total / (float)count; // floating point result
}
int main()
{
age();
char temp;
cin >> temp;
}
you should copy the code 您应该复制代码
cout << "Enter a person's age or enter -1 to stop\n";
cin >> age;
before the while loop. 在while循环之前。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.