#include <stdio.h>

int main (void)
 {
 //declare variables
 int sales = 0;
 int totalsales =0;
 double commission = 0.0;

 //enter input items
 printf("\nsales: ");
 scanf("%d", &totalsales);
 //determine whether the data is valid
 if (sales <= 0 && commission = -1);
 else if (sales <= 10000 && commission = sales*0.2);
 else if (sales <= 40000 && commission = sales*.05 + 2000);
 else if (sales >= 40000 && commission = sales*.1 + 17000);
  {
 //calculate and display the output
  sales = sales <= 0 ;
  commission =  -1 ;

  sales = sales <= 10000 ;
  commission = sales*0.2 ;

  sales = sales <= 40000 ;
  commission = sales*.05 + 2000 ;

  sales = sales >= 40000 ;
  commission = sales*.1 + 17000 ;

  printf("\nsales %d ", totalsales);
  printf("\ntotalcommissions: %.0f%% \n\n", commission);//zero decimal places
  }
 else
 printf("Input error!!");

 return 0;
 } 

Recommended Answers

All 3 Replies

hi folks,
The single = sign is not for condition checking it is for assignment.
for condition checking should use "=="
if (sales <= 0 && commission == -1);
else if (sales <= 10000 && commission == sales * 0.2);
else if (sales <= 40000 && commission == sales * .05 + 2000);
else if (sales >= 40000 && commission == sales * .1 + 17000);

While SenthilAnand's point is correct, and excellent advice, it is not the source of the compilation error; the compiler is perfectly happy to let you perform an assignment in the conditional, it just usually isn't what you intended and will have unexpected results when run.

The real problem is that you are putting semi-colons after each if() statement's conditional, ending the if statement. Now, once again, this is for the most part accpetable syntax, until you get to the last of the else if() clauses:

    if (sales <= 0 && commission == -1)
        ;
    else if (sales <= 10000 && commission == sales * 0.2)
        ;
    else if (sales <= 40000 && commission == sales * .05 + 2000)
        ;
    else if (sales >= 40000 && commission = sales*.1 + 17000)
        ;
    {
        //calculate and display the output
        sales = sales <= 0 ;
        commission = -1 ;
        sales = sales <= 10000 ;
        commission = sales*0.2 ;
        sales = sales <= 40000 ;
        commission = sales*.05 + 2000 ;
        sales = sales >= 40000 ;
        commission = sales*.1 + 17000 ;
        printf("\nsales %d ", totalsales);
        printf("\ntotalcommissions: %.0f%% \n\n", commission);//zero decimal places
    }
    else

(Note how I have indent the code for readability, and in particular, how I made the empty bodies of the if() statements explicit.)

Now, one thing many starting C programmers aren't aware of is that you can put a block (a section of starting with an open brace and ending with a close brace) anywhere you can put a normal statement; and in this case, that's exactly what you have done. The block that follows that tlast else if() is not in the body of the else if(), which means that the final else is disconnected from the if(), hence the error.

I would recommend never putting a semi-colon at the end of an if() statement, even if you intend for the body to be empty. In fact, as a basic principle of defensive programming in C, I would suggest that you always use a block for all conditionals and loops, even if there is only a single statement in the body.

#include <stdio.h>

int main (void)
{
    //declare variables
    int sales = 0;
    int items_scanned = 0;
    double commission = 0.0;

    //enter input items
    printf("\nsales: ");
    items_scanned = scanf("%d", &totalsales);

    //determine whether the data is valid
    if (items_scanned == 1) 
    {
        if (sales <= 0)
        {
            commission = -1 ;
        }
        else if (sales <= 10000)
        {
            commission = sales*0.2 ;
        }
        else if (sales <= 40000)
        {
            commission = sales*.05 + 2000 
        }
        else if (sales >= 40000)
        {            
            commission = sales*.1 + 17000 ;
        }
        printf("\nsales %d ", totalsales);
        printf("\ntotalcommissions: %.0f%% \n\n", commission);  //zero decimal places
    }
    else 
    {
        printf("input error");
    }
}

As it happens, you don't need the final else in any case, as you'd simply have a value of zero - the value you initialized sales with - if

commented: The final touch! +15

great ! thank you for the help :) i understand now .

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.