Averaging Code Not Running

There are no errors that I can see or noted in this program but for some reason the for loop doesn't run.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <iomanip>

int main()
{
    //Curly braces prevent "Garbage" Integers," These cause no real anwer to ever be stated
    int numOfData{};
    int sum{};
    int average{};
    double num[100]{};
    int i = 0;
    sum = 0.000

    //Prints Message

    std::cout << "\n How many numbers would you like ? max 100 : ";
    std::cin >> numOfData;

    while (numOfData > 100 || numOfData <= 0)
    {
        std::cout <<        "\n Error! number should in range of (1 to 100).";
        std::cout << "\n Enter the number again:";
        std::cin >> numOfData;
    }
    //Loop executed until all numbers are filled
    for (i = 0; i < numOfData; i++)
    {
        std::cout << ". Enter number " << i + 1 << ": ";
        std::cin >> num[i];
        sum += num[i];
    }
   
    average = sum / numOfData;
   
    std::cout << "\n" << average;
    std::cout << "\n The average is: " << average;

    return 0;
}
Last edited on
This can't be your code, because it doesn't even compile. You're missing a semi-colon on line 12.

Also, you still have the issue that somebody else mentioned in the other thread: Make your average variable be a double, not an int.

1
2
3
4
5
//double average; // remove

// ...

double average = static_cast<double>(sum) / numOfData;
Last edited on
I copy/pasted your code in my machine.
once I fix the ";" in line 12 it works for me.
some things I do not understand:
* why average is int?
* why are your init lists empty?
* sum is an int, why do you assign a float to it?
You have a missing semicolon at the end of line 12. Apart from that your program should run just fine.
@Ganado,

Strange it compiled for me when I wrote it, more to the point after I changed the original, but line 12 was added later and not by me.

I would like to take credit for line 9 being intentional, but it was not. I posted the code before I realized I did not change that line.

Andy
Topic archived. No new replies allowed.