Help With Averaging

I have made this post before with answers that solve a previous problem but now a new one has shown up. When you enter the first value the code doesn't go onto the next question.
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
#include <iostream>

int main()
{

    int numOfData{};
    int sum{};
    int answer{};
    double num[100]{};

    std::cout << "Enter the numbers of data: ";
    std::cin >> numOfData;

    while (numOfData > 100 || numOfData <= 0)
    {
        std::cout << "Error! number should in range of (1 to 100).\n";
        std::cout << "Enter the number again:\n";
        std::cin >> numOfData;
    }
    for (int i = 0; i < numOfData; i++)
    {
        std::cout << ". Enter number " << i + 1 << ": ";
    }

    answer = sum / numOfData;
    std::cout << "Average" << answer;

    return 0;
}
(If you're going to make a new thread about it, at least edit the last post of the previous thread to direct people here, else you still can create the normal issues of double-posting, since you kinda left the other thread still unresolved.)

When you enter the first value the code doesn't go onto the next question

Where do you ask for input?
1
2
3
4
    for (int i = 0; i < numOfData; i++)
    {
        std::cout << ". Enter number " << i + 1 << ": ";
    }
I see no cin in the above code.

Edit: Does anyone else see... actual duplicate links on the forum page?
Last edited on
Maybe you need to actually input your numbers (and add them to the sum):
1
2
        std::cin >> num[i];
        sum += num[i];


Further down the line you are also going to suffer integer division problems.
Last edited on
Sorry for making a duplicate post, I still am currently having problems with the solution here.

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

#include <iostream>

int main()
{

    int numOfData{};
    int sum{};
    int answer{};
    double num[100]{};
    int i = 0;

    std::cout << "Enter the numbers of data: ";
    std::cin >> numOfData;

    while (numOfData > 100 || numOfData <= 0)
    {
        std::cout << "Error! number should in range of (1 to 100).\n";
        std::cout << "Enter the number again:\n";
        std::cin >> numOfData;
    }
    for (i < numOfData; i++;)
    {
        std::cout << ". Enter number " << i + 1 << ": ";
        std::cin >> numOfData[i];
    }
    sum += numOfData[i];

    answer = sum / numOfData;
    std::cout << "Average: " << answer;

    return 0;
}


I'm not completely sure how arrays work but the message i keep on getting is that i need to pass the array.
Last edited on
Are you saying you are having an error message? If so, post your code and the error message.
(Also I think the double post was a forum glitch, actually. It seems to have resolved itself.)
Last edited on
In your latest code here:
1
2
3
4
5
6
    for (i < numOfData; i++;)
    {
        std::cout << ". Enter number " << i + 1 << ": ";
        std::cin >> numOfData[i];   // <==== THIS IS THE WRONG NAME FOR THE ARRAY
    }
    sum += numOfData[i];              // <==== THIS IS THE WRONG NAME FOR THE ARRAY AND IS IN THE WRONG PLACE 

(1) You are confusing numOfData (a scalar, i.e. single number) with num[] (which is the array.
(2) sum is being incremented outside the loop when it should plainly be inside (with the array, not the scalar as well)


And you are still ignoring my previous post ...
Last edited on
This for (i < numOfData; i++;) is also not doing what you seem to think it is. While it may syntactically correct it's not correct.

First, do you know that for() loop contains three "sections", with each section is separated by a semi-colon? The initialization section, i < numOfData in your example, the comparison section, i++ in your example, and the increment section, empty in your example.

expression must have pointer-to-object type Line-25

expression must have pointer-to-object type Line-27

expected 'while' Line 33

subscript requires array or pointer type

'i' undeclared identifier

These are the current error messages.
Another problem has recently shown up that I believe is much more important.

I get the error code '0x80070002 '

And the message is

C:\Users\Ana\source\repos\workpls/Debug\workpls.exe
Did you change your post after lastchance's and jlb's replies? You have to show us your code, or we are just guessing as to what the latest issue is.
All problems with the code have been fixed, A file was simply in the wrong place and the for loop was missing a statement.

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

#include <iostream>

int main()
{

    int numOfData{};
    int sum{};
    int answer{};
    double num[100]{};
    int i = 0;

    std::cout << "Enter the numbers of data: ";
    std::cin >> numOfData;

    while (numOfData > 100 || numOfData <= 0)
    {
        std::cout <<        "\n Error! number should in range of (1 to 100).\n";
        std::cout << "\n Enter the number again:";
        std::cin >> numOfData;
    }
    for (i = 0; i < numOfData; i++)
    {
        std::cout << ". Enter number " << i + 1 << ": ";
        std::cin >> num[i];
        sum += num[i];
    }
   
    answer = sum / numOfData;
    std::cout << "Average: " << answer;

    return 0;
}
Topic archived. No new replies allowed.