in C++ i am getting two errors regarding illegal else without matching if , and i have dont i myself the last step of my project is to obtain the average of all non positive numbers and average of all negative numbers as well as the sum of just the negatives

1- am i doing this write in theory if i create 2 if statements that if the numbers are less then 0 just take the average of those thus the reason for declaring answer3 /4

even though i am having errors is my algorithm right?

``````#include <iostream>
using namespace std;

int main ()
{

cout<<"this program will have you input in 10 whole numbers.\n";
cout<<"please enter in ten whole numbers followed by a comma";//asking you to input the numbers
cout<<"enter in num1.\n";//enter in the numbers
cin >> num1;
cout<<"enter in num2.\n";
cin >> num2;
cout<<"enter in num3.\n";
cin >> num3;
cout<<"enter in num4.\n";
cin >> num4;
cout<<"enter in num5.\n";
cin >> num5;
cout<<"enter in num6.\n";
cin >> num6;
cout<<"enter in num7.\n";
cin >> num7;
cout<<"enter in num8.\n";
cin >> num8;
cout<<"enter in num9.\n";
cin >> num9;
cout<<"enter in num10.\n";
cin >> num10;
answer=num1+num2+num3+num4+num5+num6+num7+num8+num9+num10;//formula to get the sum of the number
cout<<"the sum of the ten numbers are"<<answer<<endl;

if (num1,num2,num3,num4,num5,num6,num7,num8,num9,num10 < 0);//an if statement stating that if the number is greater then 0 or less then zero to show how much greater or less than it is
{
cout<<"the sum of all positives" <<answer3<<endl;
}

else

(num1,num2,num3,num4,num5,num6,num7,num8,num9,num10 > 0);
{
cout<<"the sum of the negatives greater than 0 is"<<answer4<<endl;
}
return 0;
}``````

First off I assume you are taking a course and haven't covered arrays.

Otherwize please use `int num[10];` and a for loop.

But that is how to write this problem in 10 lines. It doesn't make you code incorrect.

What you have done wrong is use the comma separator between the numbers `if (num1,num2,m3,num4,num5,num6,num7,num8,num9,num10 < 0)` What you need is to test each individually `if (num1>0 && num2>0)` Finally, a quick note about the comma operator. It allows the evaluation of multiple values e.g. `int a = b,c ;` will evaluate b and then c and set the answer for the evaluation of c into a. It is not used that often but when it is it is often only make the code more compact to write. [Expert comment: When it is overloaded it is outstanding, e.g Blitz++]

first i was thinking the if else judgment should before the answer and answer1.
then everything are int, so if i input number1=2.1
it will cause problem

first i was thinking the if else judgment should before the answer and answer1.
then everything are int, so if i input number1=2.1
it will cause problem

im not really sure what you are asking me to change to make this correct

``````if (num1>0 && num2>0 && num3> &&num4>0 &&num5>0 &&num6>0 &&num7>0 &&num8>0 &&num9>0 &&num10>0              );//an if statement stating that if the number is greater then 0 or less then zero to show how much greater or less than it is
{
cout<<"the sum of all positives" <<answer3<<endl;
}

else

(num1>0 && num2>0 && num3> &&num4>0 &&num5>0 &&num6>0 &&num7>0 &&num8>0 &&num9>0 &&num10>0 );
{
cout<<"the sum of the negatives greater than 0 is"<<answer4<<endl;
}
return 0;
}``````

illegal syntax and illegal if/else did i put this in right?

ok
first if input the num1 as 1.2 then the code didn't handle the situation like that.

You have made a couple of errors:

First the syntax of an if statement is

``````if (condition)
{
Stuff to execute
}``````

You have written

``````if (condition);
{ ... }``````

Your semi colon means that the test just doesn't matter. So remove the semi-colon.

Secondly, you need something like

``````if (condition)
{
do stuff if  true.
}
else
{
do stuff if false.
}``````

What you have incorrectly (why does your compile not tell you which line?) is to put the condition (with a semicolon -- wrong again) after the else. You need to just write else.

You really should go with a summary loop.

``````int num[10], sum = 0;
...
for(int i = 0; i < 10; i++)
if(num[i] > 0)
sum += num[i];

cout << "Sum: " << sum;``````

Try the same for input.

If a number is negative, shouldn't you find `|num[i]|` , for each value? Unless you want the sum to be negative.

You REALLY need to move this to an array...

From the text printed next to answer3 and answer4, what you really need is something more like this:

``````if (num1 > 0) {
} else {
}``````

and repeat it for the other 9 numbers

but if you can get to an array, you could do something more like the following which does it for all ten numbers:

``````for (int ii = 0; ii < 10; ii++) {
if (num[ii] > 0) {
} else {
}
}``````

thanks your help i will ask the instructor for further clarification while i do know about arrays and loops this is the second lab for the course and hasnt covered that material so i am not sure how advanced he wants this lab to be

thanks your help i will ask the instructor for further clarification while i do know about arrays and loops this is the second lab for the course and hasnt covered that material so i am not sure how advanced he wants this lab to be

Fair enough, I know sometimes they want you to do things "the hard way" so you can appreciate how much easier it is with the other tools.

Small comment:
you can simplify :

``````for (int ii = 0; ii < 10; ii++) {
if (num[ii] > 0) {
} else {
}
}``````

``````if (num1>0)
Then the negative number sum is `answer4= answer3-answer1` Saves lots of typing.