I'm supposed to add a loop to a previous program written in Chapter 4. It sounds easy enough, I gave it a go, tried a few different things, and I can't get it to work either.
By adding the loop, I basically want the program to run exactly as before, only rather than terminate after output, it should again prompt the user for another trade, etc, etc, loop, loop.. until the user enters 0 to terminate.
I added a printf line to instruct the user to enter 0 to terminate, then I added a do loop which engulfs the printf, scanf, and the if, else if, else statments. I did this because I want everything to be done just as before, and continue looping until the user enters 0. Instead only the printf and scanf keep looping, never calculating a commission. But all the if, else if calculation code is included in the loop with brackets as part of the do/statement/while/expression. My while is simple while (value != 0 ).
Why doesn't this work? It seems easy.
Also tried this, still doesn't calculate commissions.
By adding the loop, I basically want the program to run exactly as before, only rather than terminate after output, it should again prompt the user for another trade, etc, etc, loop, loop.. until the user enters 0 to terminate.
I added a printf line to instruct the user to enter 0 to terminate, then I added a do loop which engulfs the printf, scanf, and the if, else if, else statments. I did this because I want everything to be done just as before, and continue looping until the user enters 0. Instead only the printf and scanf keep looping, never calculating a commission. But all the if, else if calculation code is included in the loop with brackets as part of the do/statement/while/expression. My while is simple while (value != 0 ).
Why doesn't this work? It seems easy.
Code:
/*calculates a brokers commission*/
main ()
{
float commission, value ;
printf ("Calculates the broker's commission, enter 0 to end program.\n") ;
do {
printf ("Enter value of trade: ") ;
scanf ("%f", &value) ;
if (value < 2500.00)
commission = 30.00 + .017 * value ;
else if (value < 6250.00)
commission = 56.00 + .0066 * value ;
else if (value < 20000.00)
commission = 76.00 + .0034 * value ;
else if (value < 50000.00)
commission = 155.00 + .0011 * value ;
else
commission = 255.00 + .0009 * value ;
if (commission < 39.00)
commission = 39.00;
}
while (value != 0 ) ;
printf ("Commission: $%.2f\n", commission) ;
return 0 ;
}
Also tried this, still doesn't calculate commissions.
Code:
main ()
{
float commission, value ;
printf ("Calculates the broker's commission, enter 0 to end program.\n") ;
do {
printf ("Enter value of trade: ") ;
scanf ("%f", &value) ;
if (value == 0) //added this line, not in first version
printf ("Thank You, this session has been terminated\n") ;
if (value < 2500.00)
commission = 30.00 + .017 * value ;
else if (value < 6250.00)
commission = 56.00 + .0066 * value ;
else if (value < 20000.00)
commission = 76.00 + .0034 * value ;
else if (value < 50000.00)
commission = 155.00 + .0011 * value ;
else
commission = 255.00 + .0009 * value ;
if (commission < 39.00)
commission = 39.00;
}
while (value > 0 ) ; //changed this from != to >
printf ("Commission: $%.2f\n", commission) ;
return 0 ;
}
Last edited by a moderator: