A
Andy
Hi
I am writing a small calculator program using switch() for a menu. The
menu is presented at first, selection read in via scanf(), and
calculation is executed. At the end of the operation, the menu is
presented again. When it is presented again however, it is printed
twice. I've been through the code and cannot spot any obvious errors,
but hey ... more eyes are better than one. I don't particularly want to
revamp my code too much nor use sophisticated hacks since I am a student
relatively new to C and programming and a lot of that advanced stuff
would be way over my head
I have copied the code for reference. Does it have something with not
flushing the buffers from the first stdin read by scanf()? Suggestions
please:
----------code begin--------------------
/****************************************************************
* *
* Program: Calk-U-l8r.c *
* *
* Written by: xxxxxxxxxxxx *
* *
* 30/10/03; <version number> *
* *
*****************************************************************/
#include <stdio.h>
/* Menu-driven Calculator program: */
main()
{
float i, z, tmp;
char x;
tmp = 0;
puts("\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n");
puts("\n Calk-U-l8r\n");
puts("\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n");
puts("\nPlease select an operation. Enter 'q' to quit:"); /* menu */
printf("\nAdd: %10c\n", 'a');
printf("Divide: %7c\n", 'd');
printf("Multiply: %5c\n", 'm');
printf("Subtract: %5c\n", 's');
scanf("%c", &x);
while ( x != 'q'){
switch(x){
case 'a': /* add function */
puts("\nAddition\n");
printf("\nEnter digit:>");
scanf("%f", &i);
tmp += i;
printf("\nEnter next digit:>");
scanf("%f", &i);
tmp += i;
printf("\nThe answer is: %.2f", tmp);
break;
case 'd': /* divide function */
puts("\nDivision\n");
puts("\nEnter a digit:");
scanf("%f", &i);
puts("\nEnter the dividend:");
scanf("%f", &z);
printf("\nThe answer is: %.2f", i / z);
break;
case 'm': /* multiplication */
puts("\nMultiplication\n");
printf("\nEnter a digit:>");
scanf("%f", &i);
printf("\nEnter a second digit:>");
scanf("%f", &z);
printf("\nThe answer is: %.2f", i * z);
break;
case 's': /* subtraction */
puts("\nSubtraction\n");
puts("\nEnter a digit:");
scanf("%f", &i);
puts("\nEnter second digit:");
scanf("%f", &z);
printf("\nThe answer is: %.2f", i - z);
break;
}
puts("\nPlease select an operation. Enter 'q' to quit:");
printf("\nAdd: %10c\n", 'a');
printf("Divide: %7c\n", 'd');
printf("Multiply: %5c\n", 'm');
printf("Subtract: %5c\n", 's');
scanf("%c", &x);
}
puts("\n\nThank-you for using Calk-U-l8r!\n");
return 0;
}
----------code end------------------------
Thanks
Andy
I am writing a small calculator program using switch() for a menu. The
menu is presented at first, selection read in via scanf(), and
calculation is executed. At the end of the operation, the menu is
presented again. When it is presented again however, it is printed
twice. I've been through the code and cannot spot any obvious errors,
but hey ... more eyes are better than one. I don't particularly want to
revamp my code too much nor use sophisticated hacks since I am a student
relatively new to C and programming and a lot of that advanced stuff
would be way over my head
I have copied the code for reference. Does it have something with not
flushing the buffers from the first stdin read by scanf()? Suggestions
please:
----------code begin--------------------
/****************************************************************
* *
* Program: Calk-U-l8r.c *
* *
* Written by: xxxxxxxxxxxx *
* *
* 30/10/03; <version number> *
* *
*****************************************************************/
#include <stdio.h>
/* Menu-driven Calculator program: */
main()
{
float i, z, tmp;
char x;
tmp = 0;
puts("\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n");
puts("\n Calk-U-l8r\n");
puts("\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n");
puts("\nPlease select an operation. Enter 'q' to quit:"); /* menu */
printf("\nAdd: %10c\n", 'a');
printf("Divide: %7c\n", 'd');
printf("Multiply: %5c\n", 'm');
printf("Subtract: %5c\n", 's');
scanf("%c", &x);
while ( x != 'q'){
switch(x){
case 'a': /* add function */
puts("\nAddition\n");
printf("\nEnter digit:>");
scanf("%f", &i);
tmp += i;
printf("\nEnter next digit:>");
scanf("%f", &i);
tmp += i;
printf("\nThe answer is: %.2f", tmp);
break;
case 'd': /* divide function */
puts("\nDivision\n");
puts("\nEnter a digit:");
scanf("%f", &i);
puts("\nEnter the dividend:");
scanf("%f", &z);
printf("\nThe answer is: %.2f", i / z);
break;
case 'm': /* multiplication */
puts("\nMultiplication\n");
printf("\nEnter a digit:>");
scanf("%f", &i);
printf("\nEnter a second digit:>");
scanf("%f", &z);
printf("\nThe answer is: %.2f", i * z);
break;
case 's': /* subtraction */
puts("\nSubtraction\n");
puts("\nEnter a digit:");
scanf("%f", &i);
puts("\nEnter second digit:");
scanf("%f", &z);
printf("\nThe answer is: %.2f", i - z);
break;
}
puts("\nPlease select an operation. Enter 'q' to quit:");
printf("\nAdd: %10c\n", 'a');
printf("Divide: %7c\n", 'd');
printf("Multiply: %5c\n", 'm');
printf("Subtract: %5c\n", 's');
scanf("%c", &x);
}
puts("\n\nThank-you for using Calk-U-l8r!\n");
return 0;
}
----------code end------------------------
Thanks
Andy