# Similar programming - Sales commision calculator

[b][red]This message was edited by tokoG at 2006-1-31 22:34:11[/red][/b][hr]
Hi! Thanks for the advices on the previous one.
I need to modify it and make a similar programme to calculate the sales total/average/commision by the input figure. The differences are..

1) On the previous programme, "monthly period" could not include "1" or "2". If 1 or 2 were entered by scanf, it typed the error msg.
(Due to the average & commision calculation excluded highest & lowest sales amount.)

2) Now I have to include "number of month" of "1" & "2". If 1 or 2 were entered, the average & commision calculation will be different from those monthly period over 2.

[Case 1] ([red]Monthly period [b]<=[/b] 2[/red])
Enter the number of months in the financial period: 2 /*scanf entry*/
Enter the sales of month 1(\$): 100/*scanf entry*/
Enter the sales of month 2(\$): 200/*scanf entry*/
Total sales: /*sum calculation*/
Adjust average monthly sales: S150 [red]/*(sum / number of month)*/[/red]
Commision for 2 months: S24 [red]/* average * 0.08 * number of month*/[/red]

[Case 2] ([red]Monthly period [b]>[/b] 2[/red])
Enter the number of months in the financial period: 3 /*scanf entry*/
Enter the sales of month 1(\$): 100/*scanf entry*/
Enter the sales of month 2(\$): 200/*scanf entry*/
Enter the sales of month 3(\$): 300/*scanf entey*/
Total sales: \$600/*sum calculation*/
Adjust average monthly sales: S200 [red]/*sum - (highest+lowest)*/[/red]
Commision for 2 months: S24 [red]/* average * 0.08 * number of month*/[/red]
[red](This time, the "sum" & "average" exclude highest and lowest)[/red]

[b]ANYHOW[/b], I wrote the programme as follows. I separated the calculation in two, one for case1, other for case2. Not sure if this structure is OK... I can't check it. Before checking anything, I think somewhere else with month++; is wrong because all I get is the repeat of the following sentences. It remains [red]month 1[/red] and never goes to the next month!

[red]Enter the sales of month 1(\$):[/red]

Would anyone point me the incorrect part? Thank you so much!! xxx

/*************************************************************************

********************************************************************
#include

void main () {

int month;
int monthPeriod;

float sum, sales, commision, highestSales, lowestSales, average;
float adjTotal; /*sum - highest - lowest*/

/*valiate monthly period*/
while (1)
{
printf("Enter number of months in the financial period: ");
scanf("%d", &monthPeriod);
fflush(stdin);

if (monthPeriod <= 0) /*wong input of month period*/
{
printf("You entered a wrong number. The number must be greater than zero.
");
continue; /*to enter the month period again*/
}/*end of if*/
break; /*correct month period*/
}/*end of while(1) */

/*to calculate the highest and lowest sales amount input*/
sum = 0;
month = 1;
while (month <= monthPeriod)
{
printf("Enter the sales of month %d (\$): ", month);
scanf("%f", &sales);
if (sales < 0) /*wrong sales amount*/
{
printf("You entered a wrong sales amount. The amount must be greater than zero.
");
continue;
}
else /*correct sales input*/
{
if (month == 1)
highestSales = lowestSales = sales;
else if (monthPeriod > 2) /*month period more than two*/
{
highestSales = (sales > highestSales)? sales : highestSales;
lowestSales = (sales < lowestSales)? sales : lowestSales;
sum += sales;
adjTotal = sum - (highestSales + lowestSales);
month++;
}/*end of else if (monthPeriod>2)*/
else /*month period one or two month*/
{
sum += sales;
average = sum / monthPeriod;
month++;
} /*end of the else for month period of one or two*/
}/*end of the 1st else for correct sales input*/
}/*end of while(month<=monthPeriod) */

if (monthPeriod > 2) /*output for month period > 2*/
{
printf("Total sales: \$%f
", sum);
printf("Average monthly sales: \$%f
", adjTotal / (monthPeriod - 2));
printf("Commision for %d months: \$%f
", monthPeriod, adjTotal * 0.08 * monthPeriod);
} else /*output for month period <=2*/
{
printf("Total sales: \$%f
", sum);
printf("Average monthly sales: \$%f
", average / monthPeriod);
printf("Commision for %d months: \$%f
", monthPeriod, average * 0.08 * monthPeriod);
}/*end of else for month <=2*/

fflush(stdin);
getchar();

}

«1

• : [b][red]This message was edited by tokoG at 2006-1-31 22:34:11[/red][/b][hr]
: Hi! Thanks for the advices on the previous one.
: I need to modify it and make a similar programme to calculate the sales total/average/commision by the input figure. The differences are..
:
: 1) On the previous programme, "monthly period" could not include "1" or "2". If 1 or 2 were entered by scanf, it typed the error msg.
: (Due to the average & commision calculation excluded highest & lowest sales amount.)
:
: 2) Now I have to include "number of month" of "1" & "2". If 1 or 2 were entered, the average & commision calculation will be different from those monthly period over 2.
:
: [Case 1] ([red]Monthly period [b]<=[/b] 2[/red])
: Enter the number of months in the financial period: 2 /*scanf entry*/
: Enter the sales of month 1(\$): 100/*scanf entry*/
: Enter the sales of month 2(\$): 200/*scanf entry*/
: Total sales: /*sum calculation*/
: Adjust average monthly sales: S150 [red]/*(sum / number of month)*/[/red]
: Commision for 2 months: S24 [red]/* average * 0.08 * number of month*/[/red]
:
: [Case 2] ([red]Monthly period [b]>[/b] 2[/red])
: Enter the number of months in the financial period: 3 /*scanf entry*/
: Enter the sales of month 1(\$): 100/*scanf entry*/
: Enter the sales of month 2(\$): 200/*scanf entry*/
: Enter the sales of month 3(\$): 300/*scanf entey*/
: Total sales: \$600/*sum calculation*/
: Adjust average monthly sales: S200 [red]/*sum - (highest+lowest)*/[/red]
: Commision for 2 months: S24 [red]/* average * 0.08 * number of month*/[/red]
: [red](This time, the "sum" & "average" exclude highest and lowest)[/red]
:
:
:
: [b]ANYHOW[/b], I wrote the programme as follows. I separated the calculation in two, one for case1, other for case2. Not sure if this structure is OK... I can't check it. Before checking anything, I think somewhere else with month++; is wrong because all I get is the repeat of the following sentences. It remains [red]month 1[/red] and never goes to the next month!
:
: [red]Enter the sales of month 1(\$):[/red]
:
:
: Would anyone point me the incorrect part? Thank you so much!! xxx
:
:
:
: /*************************************************************************
:
: ********************************************************************
: #include
:
: void main () {
:
: int month;
: int monthPeriod;
:
: float sum, sales, commision, highestSales, lowestSales, average;
: float adjTotal; /*sum - highest - lowest*/
:
: /*valiate monthly period*/
: while (1)
: {
: printf("Enter number of months in the financial period: ");
: scanf("%d", &monthPeriod);
: fflush(stdin);
:
: if (monthPeriod <= 0) /*wong input of month period*/
: {
: printf("You entered a wrong number. The number must be greater than zero.
");
: continue; /*to enter the month period again*/
: }/*end of if*/
: break; /*correct month period*/
: }/*end of while(1) */
:
:
: /*to calculate the highest and lowest sales amount input*/
: sum = 0;
: month = 1;
: while (month <= monthPeriod)
: {
: printf("Enter the sales of month %d (\$): ", month);
: scanf("%f", &sales);
: if (sales < 0) /*wrong sales amount*/
: {
: printf("You entered a wrong sales amount. The amount must be greater than zero.
");
: continue;
: }
: else /*correct sales input*/
: {
: if (month == 1)
: highestSales = lowestSales = sales;
: else if (monthPeriod > 2) /*month period more than two*/
: {
: highestSales = (sales > highestSales)? sales : highestSales;
: lowestSales = (sales < lowestSales)? sales : lowestSales;
: sum += sales;
: adjTotal = sum - (highestSales + lowestSales);
: month++;
: }/*end of else if (monthPeriod>2)*/
: else /*month period one or two month*/
: {
: sum += sales;
: average = sum / monthPeriod;
: month++;
: } /*end of the else for month period of one or two*/
: }/*end of the 1st else for correct sales input*/
: }/*end of while(month<=monthPeriod) */
:
: if (monthPeriod > 2) /*output for month period > 2*/
: {
: printf("Total sales: \$%f
", sum);
: printf("Average monthly sales: \$%f
", adjTotal / (monthPeriod - 2));
: printf("Commision for %d months: \$%f
", monthPeriod, adjTotal * 0.08 * monthPeriod);
: } else /*output for month period <=2*/
: {
: printf("Total sales: \$%f
", sum);
: printf("Average monthly sales: \$%f
", average / monthPeriod);
: printf("Commision for %d months: \$%f
", monthPeriod, average * 0.08 * monthPeriod);
: }/*end of else for month <=2*/
:
: fflush(stdin);
: getchar();
:
: }
:
When the loop
[code]while (month <= monthPeriod)[/code]
is first entered, month has value 1. If the sales typed by the user are correct, then the only part of the if/else structure to be executed is this one:
[code]: if (month == 1)
: highestSales = lowestSales = sales;[/code]
Here month is not incremented so it remains equal to 1 when the loop is reexecuted again and again.

Steph
• [b][red]This message was edited by tokoG at 2006-2-1 19:47:38[/red][/b][hr]
: : ********************************************************************
: : #include
: :
: : void main () {
: :
: : int month;
: : int monthPeriod;
: :
: : float sum, sales, commision, highestSales, lowestSales, average;
: : float adjTotal; /*sum - highest - lowest*/
: : float adjMonthly; /* = adjTotal / (monthlyPeriod - 2) */
: :
: : /*valiate monthly period*/
: : while (1)
: : {
: : printf("Enter number of months in the financial period: ");
: : scanf("%d", &monthPeriod);
: : fflush(stdin);
: :
: : if (monthPeriod <= 0) /*wong input of month period*/
: : {
: : printf("You entered a wrong number. The number must be greater than zero.
");
: : continue; /*to enter the month period again*/
: : }/*end of if*/
: : break; /*correct month period*/
: : }/*end of while(1) */
: :
: :
: : /*to calculate the highest and lowest sales amount input*/
: : sum = 0;
: : month = 1;
: : while (month <= monthPeriod)
: : {
: : printf("Enter the sales of month %d (\$): ", month);
: : scanf("%f", &sales);
: : if (sales < 0) /*wrong sales amount*/
: : {
: : printf("You entered a wrong sales amount. The amount must be greater than zero.
");
: : continue;
: : }
: : else /*correct sales input*/
: : {
: : if (month == 1)
: : highestSales = lowestSales = sales;
: : else if (monthPeriod > 2) /*month period more than two*/
: : {
: : highestSales = (sales > highestSales)? sales : highestSales;
: : lowestSales = (sales < lowestSales)? sales : lowestSales;
: : sum += sales;
: : adjTotal = sum - (highestSales + lowestSales);
: : month++;
: : }/*end of else if (monthPeriod>2)*/
: : else /*month period one or two month*/
: : {
: : sum += sales;
: : average = sum / monthPeriod;
: : month++;
: : } /*end of the else for month period of one or two*/
: : }/*end of the 1st else for correct sales input*/
: : }/*end of while(month<=monthPeriod) */
: :
: : if (monthPeriod > 2) /*output for month period > 2*/
: : {
: : printf("Total sales: \$%f
", sum);
: : printf("Average monthly sales: \$%f
", adjTotal / (monthPeriod - 2));
: : printf("Commision for %d months: \$%f
", monthPeriod, adjTotal * 0.08 * monthPeriod);
: : } else /*output for month period <=2*/
: : {
: : printf("Total sales: \$%f
", sum);
: : printf("Average monthly sales: \$%f
", average / monthPeriod);
: : printf("Commision for %d months: \$%f
", monthPeriod, average * 0.08 * monthPeriod);
: : }/*end of else for month <=2*/
: :
: : fflush(stdin);
: : getchar();
: :
: : }
: :
: When the loop
: [code]while (month <= monthPeriod)[/code]
: is first entered, month has value 1. If the sales typed by the user are correct, then the only part of the if/else structure to be executed is this one:
: [code]: if (month == 1)
: : highestSales = lowestSales = sales;[/code]
: Here month is not incremented so it remains equal to 1 when the loop is reexecuted again and again.
:
: Steph
:
Hi Stephl

Thanks for pointing me that out. True.. it repeats if(month==1) because it's not incremented. But can I simply just add month++; in the if (month==1) structure? It doesn't execute... because the compiler catches the next green sentence and the warning says; Misplaced else.
[code]
if (month == 1)
highestSales = lowestSales = sales;
[red]month++;[/red]
[green]else if (monthPeriod > 2) /*month period more than two*/[/green]
[/code]

Do you know how to fix this? Or is this the structure problem?

• : [b][red]This message was edited by tokoG at 2006-2-1 19:47:38[/red][/b][hr]
: : : ********************************************************************
: : : #include
: : :
: : : void main () {
: : :
: : : int month;
: : : int monthPeriod;
: : :
: : : float sum, sales, commision, highestSales, lowestSales, average;
: : : float adjTotal; /*sum - highest - lowest*/
: : : float adjMonthly; /* = adjTotal / (monthlyPeriod - 2) */
: : :
: : : /*valiate monthly period*/
: : : while (1)
: : : {
: : : printf("Enter number of months in the financial period: ");
: : : scanf("%d", &monthPeriod);
: : : fflush(stdin);
: : :
: : : if (monthPeriod <= 0) /*wong input of month period*/
: : : {
: : : printf("You entered a wrong number. The number must be greater than zero.
");
: : : continue; /*to enter the month period again*/
: : : }/*end of if*/
: : : break; /*correct month period*/
: : : }/*end of while(1) */
: : :
: : :
: : : /*to calculate the highest and lowest sales amount input*/
: : : sum = 0;
: : : month = 1;
: : : while (month <= monthPeriod)
: : : {
: : : printf("Enter the sales of month %d (\$): ", month);
: : : scanf("%f", &sales);
: : : if (sales < 0) /*wrong sales amount*/
: : : {
: : : printf("You entered a wrong sales amount. The amount must be greater than zero.
");
: : : continue;
: : : }
: : : else /*correct sales input*/
: : : {
: : : if (month == 1)
: : : highestSales = lowestSales = sales;
: : : else if (monthPeriod > 2) /*month period more than two*/
: : : {
: : : highestSales = (sales > highestSales)? sales : highestSales;
: : : lowestSales = (sales < lowestSales)? sales : lowestSales;
: : : sum += sales;
: : : adjTotal = sum - (highestSales + lowestSales);
: : : month++;
: : : }/*end of else if (monthPeriod>2)*/
: : : else /*month period one or two month*/
: : : {
: : : sum += sales;
: : : average = sum / monthPeriod;
: : : month++;
: : : } /*end of the else for month period of one or two*/
: : : }/*end of the 1st else for correct sales input*/
: : : }/*end of while(month<=monthPeriod) */
: : :
: : : if (monthPeriod > 2) /*output for month period > 2*/
: : : {
: : : printf("Total sales: \$%f
", sum);
: : : printf("Average monthly sales: \$%f
", adjTotal / (monthPeriod - 2));
: : : printf("Commision for %d months: \$%f
", monthPeriod, adjTotal * 0.08 * monthPeriod);
: : : } else /*output for month period <=2*/
: : : {
: : : printf("Total sales: \$%f
", sum);
: : : printf("Average monthly sales: \$%f
", average / monthPeriod);
: : : printf("Commision for %d months: \$%f
", monthPeriod, average * 0.08 * monthPeriod);
: : : }/*end of else for month <=2*/
: : :
: : : fflush(stdin);
: : : getchar();
: : :
: : : }
: : :
: : When the loop
: : [code]while (month <= monthPeriod)[/code]
: : is first entered, month has value 1. If the sales typed by the user are correct, then the only part of the if/else structure to be executed is this one:
: : [code]: if (month == 1)
: : : highestSales = lowestSales = sales;[/code]
: : Here month is not incremented so it remains equal to 1 when the loop is reexecuted again and again.
: :
: : Steph
: :
: Hi Stephl
:
: Thanks for pointing me that out. True.. it repeats if(month==1) because it's not incremented. But can I simply just add month++; in the if (month==1) structure? It doesn't execute... because the compiler catches the next green sentence and the warning says; Misplaced else.
: [code]
: if (month == 1)
: highestSales = lowestSales = sales;
: [red]month++;[/red]
: [green]else if (monthPeriod > 2) /*month period more than two*/[/green]
: [/code]
:
: Do you know how to fix this? Or is this the structure problem?
:
Include it in braces.
[code]
if (month == 1)
{
highestSales = lowestSales = sales;
[red]month++;[/red]
}
[green]else if (monthPeriod > 2) /*month period more than two*/[/green]
[/code]

Steph
• Hi Steph

Oh silly me.. curly braces...
Thanks for that....

I am having another problem. I changed the programme as following because I noticed the calculation part was wrong. But then... what it doesnt do properly is the sum part. It doesn't sum the sales amount right...

Somethig must be wrong but can't tell...

[code]
if (month == 1)
{
highestSales = lowestSales = sales;
month++;
}
else if (monthPeriod <=2) /*month period one or two month*/
{
sum += sales;
month++;
} /*end of the else for month period of one or two*/
else /*month period more than two*/
{
highestSales = (sales > highestSales)? sales : highestSales;
lowestSales = (sales < lowestSales)? sales : lowestSales;
sum += sales;
month++;
}/*end of else if (monthPeriod>2)*/
}/*end of the 1st else for correct sales input*/
}/*end of while(month<=monthPeriod) */

if (monthPeriod <= 2)/*output for month period <=2*/
{
average = sum / monthPeriod;
printf("Total sales: \$%f
", sum);
printf("Average monthly sales: \$%f
", average);
printf("Commision for %d months: \$%f
", monthPeriod, average * 0.08 * monthPeriod);
} else /*output for month period > 2*/
{
adjTotal = sum - (highestSales + lowestSales);
printf("Total sales: \$%f
printf("Average monthly sales: \$%f
", adjTotal / (monthPeriod - 2));
printf("Commision for %d months: \$%f
", monthPeriod, adjTotal * 0.08 * monthPeriod);
}/*end of else for month <=2*/
[/code]

• : Hi Steph
:
: Oh silly me.. curly braces...
: Thanks for that....
:
: I am having another problem. I changed the programme as following because I noticed the calculation part was wrong. But then... what it doesnt do properly is the sum part. It doesn't sum the sales amount right...
:
: Somethig must be wrong but can't tell...
:
: [code]
: if (month == 1)
: {
: highestSales = lowestSales = sales;
: month++;
: }
: else if (monthPeriod <=2) /*month period one or two month*/
: {
: sum += sales;
: month++;
: } /*end of the else for month period of one or two*/
: else /*month period more than two*/
: {
: highestSales = (sales > highestSales)? sales : highestSales;
: lowestSales = (sales < lowestSales)? sales : lowestSales;
: sum += sales;
: month++;
: }/*end of else if (monthPeriod>2)*/
: }/*end of the 1st else for correct sales input*/
: }/*end of while(month<=monthPeriod) */
:
: if (monthPeriod <= 2)/*output for month period <=2*/
: {
: average = sum / monthPeriod;
: printf("Total sales: \$%f
", sum);
: printf("Average monthly sales: \$%f
", average);
: printf("Commision for %d months: \$%f
", monthPeriod, average * 0.08 * monthPeriod);
: } else /*output for month period > 2*/
: {
: adjTotal = sum - (highestSales + lowestSales);
: printf("Total sales: \$%f
: printf("Average monthly sales: \$%f
", adjTotal / (monthPeriod - 2));
: printf("Commision for %d months: \$%f
", monthPeriod, adjTotal * 0.08 * monthPeriod);
: }/*end of else for month <=2*/
: [/code]
:
:
Hi tokoG!

I had a quick look at your code but cannot figure the problem. Do you still initialize sum to zero before entering the while loop? May you post your whole code please?

Take care, Steph.
• [b][red]This message was edited by tokoG at 2006-2-2 19:20:41[/red][/b][hr]

: Hi tokoG!
:
: I had a quick look at your code but cannot figure the problem. Do you still initialize sum to zero before entering the while loop? May you post your whole code please?
:
: Take care, Steph.

[blue]
Hi Stephl

Here is my entire code for this programme... I didn't find anything wrong with the increment part so it must by a syntax / structure problem??

tG
[/blue]

[code]
#include

void main ()
{

int month;
int monthPeriod;

float sum, sales, commision, highestSales, lowestSales, average;
float adjTotal; /*sum - highest - lowest*/

/*valiate monthly period*/
while (1)
{
printf("Enter number of months in the financial period: ");
scanf("%d", &monthPeriod);
fflush(stdin);

if (monthPeriod <= 0) /*wong input of month period*/
{
printf("You entered a wrong number. The number must be greater than zero.
");
continue; /*to enter the month period again*/
}/*end of if*/
break; /*correct month period*/
}/*end of while(1) */

/*to calculate the highest and lowest sales amount input*/
sum = 0;
month = 1;
while (month <= monthPeriod)
{
printf("Enter the sales of month %d (\$): ", month);
scanf("%f", &sales);
if (sales < 0) /*wrong sales amount*/
{
printf("You entered a wrong sales amount. The amount must be greater than zero.
");
continue;
}
else /*correct sales input*/
{
if (month == 1)
{
highestSales = lowestSales = sales;
month++;
}
else if (monthPeriod <=2) /*month period one or two month*/
{
sum += sales;
month++;
} /*end of the else for month period of one or two*/
else /*month period more than two*/
{
highestSales = (sales > highestSales)? sales : highestSales;
lowestSales = (sales < lowestSales)? sales : lowestSales;
sum += sales;
month++;
}/*end of else if (monthPeriod>2)*/
}/*end of the 1st else for correct sales input*/
}/*end of while(month<=monthPeriod) */

if (monthPeriod <= 2)/*output for month period <=2*/
{
average = sum / monthPeriod;
printf("Total sales: \$%f
", sum);
printf("Average monthly sales: \$%f
", average);
printf("Commision for %d months: \$%f
", monthPeriod, average * 0.08 * monthPeriod);
} else /*output for month period > 2*/
{
adjTotal = sum - (highestSales + lowestSales);
printf("Total sales: \$%f
printf("Average monthly sales: \$%f
", adjTotal / (monthPeriod - 2));
printf("Commision for %d months: \$%f
", monthPeriod, adjTotal * 0.08 * monthPeriod);
}/*end of else for month <=2*/

fflush(stdin);
getchar();

}/*end of main*/

[/code]

• Here are some changes you should take into account (check out the commentaries).
[code]
#include

void main () [red]/*int should be the return type of main, but it's not the problem here*/[/red]
{

int month;
int monthPeriod;

float sum, sales, commision, highestSales, lowestSales, average;
float adjTotal; /*sum - highest - lowest*/

/*valiate monthly period*/
while (1)
{
printf("Enter number of months in the financial period: ");
scanf("%d", &monthPeriod);
fflush(stdin); [red]/*According to K&R, using fflush on input streams has undefined behaviour.
Furthermore it's useless here. But once again it's not the problem!*/[/red]

if (monthPeriod <= 0) /*wong input of month period*/
{
printf("You entered a wrong number. The number must be greater than zero.
");
continue; /*to enter the month period again*/
}/*end of if*/
break; /*correct month period*/
}/*end of while(1) */

/*to calculate the highest and lowest sales amount input*/
sum = 0;
month = 1;
while (month <= monthPeriod)
{
printf("Enter the sales of month %d (\$): ", month);
scanf("%f", &sales);
if (sales < 0) /*wrong sales amount*/
{
printf("You entered a wrong sales amount. The amount must be greater than zero.
");
continue;
}
else /*correct sales input*/
{
if (month == 1)
{
highestSales = lowestSales = sales; [red]/*Perhaps add the line sum+=sales;*/[/red]
month++;
}
else if (monthPeriod <=2) /*month period one or two month*/
{
sum += sales;
month++;
} /*end of the else for month period of one or two*/
else /*month period more than two*/
{
highestSales = (sales > highestSales)? sales : highestSales;
lowestSales = (sales < lowestSales)? sales : lowestSales;
sum += sales;
month++;
}/*end of else if (monthPeriod>2)*/
}/*end of the 1st else for correct sales input*/
}/*end of while(month<=monthPeriod) */

if (monthPeriod <= 2)/*output for month period <=2*/
{
average = sum / monthPeriod;
printf("Total sales: \$%f
", sum);
printf("Average monthly sales: \$%f
", average);
printf("Commision for %d months: \$%f
", monthPeriod, average * 0.08 * monthPeriod);
} else /*output for month period > 2*/
{
adjTotal = sum - (highestSales + lowestSales);
printf("Total sales: \$%f
printf("Average monthly sales: \$%f
", adjTotal / (monthPeriod - 2));
printf("Commision for %d months: \$%f
", monthPeriod, adjTotal * 0.08 * monthPeriod);
}/*end of else for month <=2*/

fflush(stdin);
getchar();

}/*end of main*/
[/code]

Take care, Steph.

PS: If it still does not work, please post the results it gave you and what you expected.
• [b][red]This message was edited by tokoG at 2006-2-5 23:3:31[/red][/b][hr]
Super! It worked fine!! Thanks Stephl! x
I changed according to your advices plus I corrected one of printing part that was wrong.

By the way, I was taught adding 'fflush(stdin)' after the each scanf reading is adequate? But then, why do we need to 'flush' the memory of the reading?

• : [b][red]This message was edited by tokoG at 2006-2-5 23:3:31[/red][/b][hr]
: Super! It worked fine!! Thanks Stephl! x
: I changed according to your advices plus I corrected one of printing part that was wrong.
:
: By the way, I was taught adding 'fflush(stdin)' after the each scanf reading is adequate? But then, why do we need to 'flush' the memory of the reading?
:
Well, I've checked this in my ANSI C book: fflush has undefined behavior for an input stream (standard implementation). However some implementations allow the use of fflush on input streams. It's not necessary to flush the input stream after a call to scanf. In fact it is useless in many cases, eg when you read an integer.

Take care, Steph.
• [b][red]This message was edited by tokoG at 2006-2-7 5:26:7[/red][/b][hr]
: Well, I've checked this in my ANSI C book: fflush has undefined behavior for an input stream (standard implementation). However some implementations allow the use of fflush on input streams. It's not necessary to flush the input stream after a call to scanf. In fact it is useless in many cases, eg when you read an integer.
:
: Take care, Steph.
:
[green]

Thanks Stephl for the info. I dont remember which case it was but when using scanf and it didn't hold the screen if I didn't add flush(stdin). I guess,,, if I want to hold the screen, I need to use fflush(stdin) and getchar(); all the time, ja? Or.. I havent learnt yet but are there any other way to hold the scren?

For example, when I use return(0); for the main function (not need to return the value), even I use getchar(); and it doesnt hold the screen..
[/green]
[red]Edited: Oh no! when in the following, it still holds the screen!

getchar();
return 0;

[red/]

• : [b][red]This message was edited by tokoG at 2006-1-31 22:34:11[/red][/b][hr]
: Hi! Thanks for the advices on the previous one.
: I need to modify it and make a similar programme to calculate the sales total/average/commision by the input figure. The differences are..
:
: 1) On the previous programme, "monthly period" could not include "1" or "2". If 1 or 2 were entered by scanf, it typed the error msg.
: (Due to the average & commision calculation excluded highest & lowest sales amount.)
:
: 2) Now I have to include "number of month" of "1" & "2". If 1 or 2 were entered, the average & commision calculation will be different from those monthly period over 2.
:
: [Case 1] ([red]Monthly period [b]<=[/b] 2[/red])
: Enter the number of months in the financial period: 2 /*scanf entry*/
: Enter the sales of month 1(\$): 100/*scanf entry*/
: Enter the sales of month 2(\$): 200/*scanf entry*/
: Total sales: /*sum calculation*/
: Adjust average monthly sales: S150 [red]/*(sum / number of month)*/[/red]
: Commision for 2 months: S24 [red]/* average * 0.08 * number of month*/[/red]
:
: [Case 2] ([red]Monthly period [b]>[/b] 2[/red])
: Enter the number of months in the financial period: 3 /*scanf entry*/
: Enter the sales of month 1(\$): 100/*scanf entry*/
: Enter the sales of month 2(\$): 200/*scanf entry*/
: Enter the sales of month 3(\$): 300/*scanf entey*/
: Total sales: \$600/*sum calculation*/
: Adjust average monthly sales: S200 [red]/*sum - (highest+lowest)*/[/red]
: Commision for 2 months: S24 [red]/* average * 0.08 * number of month*/[/red]
: [red](This time, the "sum" & "average" exclude highest and lowest)[/red]
:
:
:
: [b]ANYHOW[/b], I wrote the programme as follows. I separated the calculation in two, one for case1, other for case2. Not sure if this structure is OK... I can't check it. Before checking anything, I think somewhere else with month++; is wrong because all I get is the repeat of the following sentences. It remains [red]month 1[/red] and never goes to the next month!
:
: [red]Enter the sales of month 1(\$):[/red]
:
:
: Would anyone point me the incorrect part? Thank you so much!! xxx
:
:
:
: /*************************************************************************
:
: ********************************************************************
: #include
:
: void main () {
:
: int month;
: int monthPeriod;
:
: float sum, sales, commision, highestSales, lowestSales, average;
: float adjTotal; /*sum - highest - lowest*/
:
: /*valiate monthly period*/
: while (1)
: {
: printf("Enter number of months in the financial period: ");
: scanf("%d", &monthPeriod);
: fflush(stdin);
:
: if (monthPeriod <= 0) /*wong input of month period*/
: {
: printf("You entered a wrong number. The number must be greater than zero.
");
: continue; /*to enter the month period again*/
: }/*end of if*/
: break; /*correct month period*/
: }/*end of while(1) */
:
:
: /*to calculate the highest and lowest sales amount input*/
: sum = 0;
: month = 1;
: while (month <= monthPeriod)
: {
: printf("Enter the sales of month %d (\$): ", month);
: scanf("%f", &sales);
: if (sales < 0) /*wrong sales amount*/
: {
: printf("You entered a wrong sales amount. The amount must be greater than zero.
");
: continue;
: }
: else /*correct sales input*/
: {
: if (month == 1)
: highestSales = lowestSales = sales;
: else if (monthPeriod > 2) /*month period more than two*/
: {
: highestSales = (sales > highestSales)? sales : highestSales;
: lowestSales = (sales < lowestSales)? sales : lowestSales;
: sum += sales;
: adjTotal = sum - (highestSales + lowestSales);
: month++;
: }/*end of else if (monthPeriod>2)*/
: else /*month period one or two month*/
: {
: sum += sales;
: average = sum / monthPeriod;
: month++;
: } /*end of the else for month period of one or two*/
: }/*end of the 1st else for correct sales input*/
: }/*end of while(month<=monthPeriod) */
:
: if (monthPeriod > 2) /*output for month period > 2*/
: {
: printf("Total sales: \$%f
", sum);
: printf("Average monthly sales: \$%f
", adjTotal / (monthPeriod - 2));
: printf("Commision for %d months: \$%f
", monthPeriod, adjTotal * 0.08 * monthPeriod);
: } else /*output for month period <=2*/
: {
: printf("Total sales: \$%f
", sum);
: printf("Average monthly sales: \$%f
", average / monthPeriod);
: printf("Commision for %d months: \$%f
", monthPeriod, average * 0.08 * monthPeriod);
: }/*end of else for month <=2*/
:
: fflush(stdin);
: getchar();
:
: }

If you want to hold the screen when executing a console mode program, you have several solutions. You may add the line:
[code]system("pause");[/code]
before the return statement in main. You can also run your program in a command line shell.

Take care, Steph.
• [green]
: If you want to hold the screen when executing a console mode program, you have several solutions. You may add the line:
: [code]system("pause");[/code]
: before the return statement in main. You can also run your program in a command line shell.
:
: Take care, Steph.
[/green]

Very cool! It worked! And even it printed "Press any key to continue..".
cheers!

• : [green]
: : If you want to hold the screen when executing a console mode program, you have several solutions. You may add the line:
: : [code]system("pause");[/code]
: : before the return statement in main. You can also run your program in a command line shell.
: :
: : Take care, Steph.
: [/green]
:
: Very cool! It worked! And even it printed "Press any key to continue..".
: cheers!
:
:

The reason getchar() isn't working is most likely because you have an appending line feed character somewhere in the input buffer which you don't read. It was placed there when the user pressed the enter key.

As mentioned you -cannot- use fflush(stdin). It is not ANSI C and will not work on all compilers. You have to track down which scanf line that isn't dealing with the extra line feed character. Otherwise you will get in trouble when the program executes the next scanf line - it will read the line feed instead of the user input.

• : : [green]
: : : If you want to hold the screen when executing a console mode program, you have several solutions. You may add the line:
: : : [code]system("pause");[/code]
: : : before the return statement in main. You can also run your program in a command line shell.
: : :
: : : Take care, Steph.
: : [/green]
: :
: : Very cool! It worked! And even it printed "Press any key to continue..".
: : cheers!
: :
: :
:
:
: The reason getchar() isn't working is most likely because you have an appending line feed character somewhere in the input buffer which you don't read. It was placed there when the user pressed the enter key.
:
: As mentioned you -cannot- use fflush(stdin). It is not ANSI C and will not work on all compilers. You have to track down which scanf line that isn't dealing with the extra line feed character. Otherwise you will get in trouble when the program executes the next scanf line - it will read the line feed instead of the user input.
:
:

[green]
Does fgets() have this problem as well? I prefer it over scanf()...

{2}rIng

To tokoG:

I'm not sure if you saw the other post I made, but I said I was only a high school student. I program as hobby you could say.

My classes are typical, an English class, a science class (biology...grr...), a math class (geometry, my favorite, I guess),
a History/Geography class ( the latter now ), and a sad excuse for a computer class...jk, its graphics, but no fun stuff...

My Electives are video editing and band...I guess thats pretty much it then

I presume you're a college student, correct?
[/green]

• : [green]
: Does fgets() have this problem as well? I prefer it over scanf()...
:
: {2}rIng
:
: To tokoG:
:
: I'm not sure if you saw the other post I made, but I said I was only a high school student. I program as hobby you could say.
:
: My classes are typical, an English class, a science class (biology...grr...), a math class (geometry, my favorite, I guess),
: a History/Geography class ( the latter now ), and a sad excuse for a computer class...jk, its graphics, but no fun stuff...
:
: My Electives are video editing and band...I guess thats pretty much it then
:
: I presume you're a college student, correct?
: [/green]
:

Hi Gregry

Sorry I missed your other post!
Wow, so what are you thinkig about studying in the collage? Talented in so many ways.

I am studying at the college for part time now and doing a part time tutoring job at the moment to pay my bills. After getting studying in Human Relations,I worked in the OEM product manufactuering industry for some years. Now back in study, for carrier reasons. I had to take programming class to earn credit in the foundation levels. I am not math-scientific-minded person at all but now back in school after what I learned I need in the industry I worked for, I enjoy studying subjects like... my 'was-least-favorite subject in high school' but those you are proberbly very good at.

Never know what you get really.

tokoG xx