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 ()
{	
	
	int num1,num2,num3,num4,num5,num6,num7,num8,num9,num10,answer,answer1,answer3,answer4;//declaring the numbers as intigers
	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;
	answer1=(num1+num2+num3+num4+num5+num6+num7+num8+num9+num10)/10;
	cout<<"the average is"<<answer1<<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
{	
	answer3=num1+ num2+num3+num4+num5+num6+num7+num8+num9+num10;
	cout<<"the sum of all positives" <<answer3<<endl;
}

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

Recommended Answers

All 11 Replies

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.
and the answer and answer1 is the else
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.
and the answer and answer1 is the else
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
{	
	answer3=num1+ num2+num3+num4+num5+num6+num7+num8+num9+num10;
	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 );
	{
	answer4=num1+ num2+num3+num4+num5+num6+num7+num8+num9+num10;
	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.
then read what stuxyz post

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) {
    answer3 += num1;
} else {
    answer4 += num1;
}

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) {
        answer3 += num[ii];
    } else {
        answer4 += num[ii];
    }
}

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) {
        answer3 += num[ii];
    } else {
        answer4 += num[ii];
    }
}

calculating answer3 only so write

if (num1>0)
{   answer3+=num1; }

Then the negative number sum is answer4= answer3-answer1 Saves lots of typing.

Better would be to do the typing and check the answer!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.