Nesting Loop Statements - Need guidance

Disclaimer - this is a homework assignment. The professor doesn't teach in a manner that I am able to comprehend so am asking for guidance. I have researched and I am not able to find solutions. I do not want this solved for me because then I won't learn it. I am hoping that someone here can explain in a way I understand. I am not a programmer and have never done this before. Please do not slam me for asking for help - just delete if not allowed. Thank you for your help.

The assignment is to write a program using a loop that accepts a maximum of 5 numbers so that it displays the average of the positive and negative numbers.Do not count the number zero as positive or negative.

The output should look like this -
Enter a number - 17
Enter a number - -10
Enter a number - 19
Enter a number - 0
Enter a number - -4

Average of positive numbers - 18
Average of negative numbers - -7


Here is my program so far - My issue is in the if statements. I also don't know how to write code that will separate the neg from pos and add them. I don't know where to look to find the answer.

#include <iostream>
#include <iomanip>
#include <cmath>


using namespace std;

int main()
{

int num, sum = 0, sum2 = 0, count = 0; // identifiers
double avg = 0;

for (int count =0; count <=4; ++num)
{
cout << "Please enter a number - "; // request input
cin >> num;
cout << endl;
count++;

if (num >0) // if statement assigning values to Number entered
{
cout << (double)sum/count << "Positive numbers average - " << " " << endl;
cout << endl;
}
else (num <0);
{
cout << (double)sum2/count << "Negative numbers average - " << " " << endl;
cout << endl;
}
}



return 0;
}

Last edited on
else doesn't take a condition; it has no brackets.

1
2
3
4
5
6
7
8
if (something)
{
  // do things
}
else
{
  // do other things
}


See that else line? No conditions. Just the else
you probably meant
 
else if()
instead of else
Last edited on
Oh I get it! The else statement is the "answer" if the if statement isn't true so wouldn't have conditions. That makes sense. Thank you Repeater.

I did the else if () and that is part of what I was trying to do. Thank you adam2016.

I now have the issue of the program running through the for / if / else statements before going back to the for statement. I'm wanting all 5 of the "enter a number" inputs to be completed before going down to the if / else part. Would a nested for statement redirecting the program back to the first for statement work? Or is there better way?

Would someone please direct me to where I can learn how to write the program to recognize the difference between positive and negative numbers? I just need a hint so I can go digging.


Please enter a number = 17

Positive numbers average = 0

Please enter a number = -10

Negative numbers average = 0

Please enter a number = 19

Positive numbers average = 0

Please enter a number = 0

Please enter a number = -4

Negative numbers average = 0



Topic archived. No new replies allowed.