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.
#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?
#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.
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.)
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)
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.
#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;
}