2 Replies - 402 Views - Last Post: 15 December 2008 - 10:19 AM Rate Topic: -----

#1 prosty  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 15-December 08

Program refuses to behave correctly! Please help.

Post icon  Posted 15 December 2008 - 09:37 AM

#include<stdio.h>

#define SIZE 10

int main()
{
	float basewage[SIZE] = {158.00, 147.75, 315.00, 162.25, 220.00, 181.60, 376.90, 168.70, 293.00, 214.30};
	float totalwage[SIZE], comm = 3.8;
	int sales[SIZE], index = 0, emp_num = 1;
	
	while(emp_num<11)
	{
		printf("Enter units sold for employee %i\n", emp_num); 
		scanf("%i",&sales[index]);
		fflush(stdin);
		
		if(sales[index]>=1)
			if(sales[index]<=5)
			   ;
			else
			if(sales[index]>=6)
			if(sales[index]<=10)
				comm='6';
			else
			if(sales[index]>=11)
			if(sales[index]<=15)
				comm='9';
			else
			if(sales[index]>15)
				comm = '15';
								
			totalwage[index] = (comm * sales[index]) + basewage[index];
			fflush(stdin);
			printf("total wage for employee%i is %f\n\n", emp_num, totalwage[index]);
			
			index++;
			emp_num++;
	}
	
	
}


I just cant't see were I've gone wrong. the program seems to work correctly up to a sales figure of 5, but anything over and it spits out wrong results :crazy: :crazy:
I know its probably something very basic, but i'm at a loss! Any help would be appreciated :D

Is This A Good Question/Topic? 0
  • +

Replies To: Program refuses to behave correctly! Please help.

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6063
  • View blog
  • Posts: 23,517
  • Joined: 23-August 08

Re: Program refuses to behave correctly! Please help.

Posted 15 December 2008 - 09:52 AM

You shouldn't use fflush(stdin).

This:
if(sales[index]>=1)
            if(sales[index]<=5)
               ;
            else
            if(sales[index]>=6)
            if(sales[index]<=10)
                comm='6';
            else
            if(sales[index]>=11)
            if(sales[index]<=15)
                comm='9';
            else
            if(sales[index]>15)
                comm = '15';



would be far more readable, as this:
if(sales[index]>=1 && sales[index]<=5)
{
    comm = 3.8;
}
else if(sales[index]>=6 && sales[index]<=10)
{
    comm=6.0;
}
else if(sales[index]>=11 && sales[index]<=15)
{
    comm=9.0;
}
else
{
    comm = 15.0;
}


It also fixes your calculation problem, which is because you're assigning a character value to a float variable when you do comm = '6';
Was This Post Helpful? 0
  • +
  • -

#3 prosty  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 15-December 08

Re: Program refuses to behave correctly! Please help.

Posted 15 December 2008 - 10:19 AM

Many thanks for all your help :D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1