-
infinite loop
my program works fine but as soon as i try to enter E or e it just stays in a infinite loop help please see if you can spot my problem.
Code:
#include <stdio.h>
int main (void)
{
float value1;
float accumulator = 0;
char operator;
printf ("\t\tCalulator\n");
printf ("\t\t---------\n");
do
{
printf("Type in your test\n");
scanf("%f %c", &value1, &operator);
switch (operator)
{
case 'S':
accumulator = value1;
printf("%.3f\n", value1);
break;
case 's':
accumulator = value1;
printf("%.3f\n", value1);
break;
case '+':
accumulator += value1;
printf("%.3f\n", accumulator);
break;
case '-':
accumulator = accumulator - value1;
printf("%.3f\n", accumulator);
break;
case '/':
accumulator = accumulator / value1;
printf("%.3f\n", accumulator);
break;
case '*':
accumulator = accumulator * value1;
printf("%.3f\n", accumulator);
break;
case 'E':
printf("End of Tests\n");
break;
case 'e':
printf("End of Tests\n");
break;
default:
printf("UNKNOWN OPERATOR\n");
break;
}
while (operator != 'e' && operator != 'E');
}
printf("\n%.3f\n", accumulator);
return 0;
}
-
You are trying to scan for %f first. So if you are just entering an E, it gets stuck in the %f, which messes up your scanf call.
You need to post code with less pointless blank lines, and actual indentation. You don't need a blank line between every line of code, and three between every block. I don't want to have to scroll two pages down just to read five lines.
Quzah.
-
The code you posted will not compile for me, check the position of your while and the do's closing brace.
After fixing that the program works for me.
Jim
-
thanks for the help guys tryed to move around the brackets still couldnt get it to compile :-(
-
Start by fixing up your formatting:
Code:
#include <stdio.h>
int main (void)
{
float value1;
float accumulator = 0;
char operator;
printf ("\t\tCalulator\n");
printf ("\t\t---------\n");
do
{
printf("Type in your test\n");
scanf("%f %c", &value1, &operator);
switch (operator)
{
case 'S':
accumulator = value1;
printf("%.3f\n", value1);
break;
case 's':
accumulator = value1;
printf("%.3f\n", value1);
break;
case '+':
accumulator += value1;
printf("%.3f\n", accumulator);
break;
case '-':
accumulator = accumulator - value1;
printf("%.3f\n", accumulator);
break;
case '/':
accumulator = accumulator / value1;
printf("%.3f\n", accumulator);
break;
case '*':
accumulator = accumulator * value1;
printf("%.3f\n", accumulator);
break;
case 'E':
printf("End of Tests\n");
break;
case 'e':
printf("End of Tests\n");
break;
default:
printf("UNKNOWN OPERATOR\n");
break;
}
while (operator != 'e' && operator != 'E');
}
printf("\n%.3f\n", accumulator);
return 0;
}
Quzah.