- 1

I need some help. Not sure whats wrong with my code

I need to make this code to take 100 numbers and average them, but they keep outputting to the wrong average. Even when I don't have 100 numbers. I'm not sure what is causing it and how to fix it. Here is my code and attempt: https://code.sololearn.com/ca24a8A16a15/#cpp

21st Feb 2021, 1:18 AM
Carlye
16 Antworten
+ 1
I put the cin>>x; in the while loop but nothing happens after entering some numbers. It won't average. What is causing that?
21st Feb 2021, 1:32 AM
Carlye
+ 1
Some numbers are not enough...Because you are asking for 100 numbers, so you must enter all 100 numbers before it'll output the average, for testing purpose, you can consider making that number to 5
21st Feb 2021, 1:34 AM
Hoh Shen Yien
Hoh Shen Yien - avatar
0
You should be putting your cin >> x in the while loop, in your codes, it'll only ask for x once, therefore not giving you average since there's only 1 number
21st Feb 2021, 1:23 AM
Hoh Shen Yien
Hoh Shen Yien - avatar
0
Okay, thanks!
21st Feb 2021, 1:46 AM
Carlye
0
I have another step to my project, I have to make a similar program but converted into a do-while instead of a while loop. I don't think the order is correct. Here is my attempt: https://code.sololearn.com/cA3a27a16A4A/#cpp
21st Feb 2021, 1:49 AM
Carlye
0
yeah, the sequence are wrong. Firstly, you are going to ask the N before do while loop, in your codes, the N will be changing constantly which I don't think that's what you want? Secondly, the calculation of average should be after the do while loop. This is so that the average will only be calculated after all numbers are inputted.
21st Feb 2021, 1:54 AM
Hoh Shen Yien
Hoh Shen Yien - avatar
0
Having the calculation of the average in the if-else statement, does that mean moving the whole if-else statement below the while loop part or after the entire do-while loop?
21st Feb 2021, 2:02 AM
Carlye
0
moving the entire if else below the dowhile loop
21st Feb 2021, 2:03 AM
Hoh Shen Yien
Hoh Shen Yien - avatar
0
what makes the average output to be 2.07334e-317
21st Feb 2021, 2:32 AM
Carlye
0
Can you send the updated version of your codes?
21st Feb 2021, 3:19 AM
Hoh Shen Yien
Hoh Shen Yien - avatar
0
Your line 23, you shouldn't display the average before giving it a value, so you should only print out the value after calculating it. The seemingly random average output you got is actually the original value of average in memory that you are supposed to override.
21st Feb 2021, 3:32 AM
Hoh Shen Yien
Hoh Shen Yien - avatar
0
Ok, thank you! That worked. I thought that might be the case but wasn't sure.
21st Feb 2021, 3:35 AM
Carlye
0
No problem 👌
21st Feb 2021, 3:36 AM
Hoh Shen Yien
Hoh Shen Yien - avatar
0
Might have to ask you for more help! You've been very helpful.
21st Feb 2021, 3:36 AM
Carlye
0
Write "cin>>x" in the while loop
23rd Feb 2021, 1:12 AM
sudhi narayan
sudhi narayan - avatar