Loop outputs the menu of my C program twice -


so on code review made calculator normal operations addition etc (here question). anyway followed of advice , updated program. menu of original program works fine, outputs menu once when user runs program , when finish doing calculation program loop start , out menu once again. other infinite loop does. decided make program scratch again time program outputs menu twice after user first calculation. i've tried doing for(;;) instead of do-while loop same issue. have no idea what's gone wrong, appreciate if fix me , don't come across issue again.

here's code:

#include <stdio.h> #include <stdlib.h>  void getnumbers(float *, float *); float addition(float, float); float subtraction(float, float); float multiplication(float, float); float division(float, float);  int main(void) {         {         float num1, num2;         char choice;          puts("\nenter number or letter below\n\n"              "\t1. addition\n"              "\t2. subtraction\n"              "\t3. multiplication\n"              "\t4. division\n"              "\tq. quit program");          printf("\nenter choice: ");         scanf("%c", &choice);          if(choice == 'q' || choice == 'q')         {             puts("\nquitting program...");             exit(0);         }         else if(choice < '1' || choice > '4')         {             puts("\ninvalid input");         }         else         {             getnumbers(&num1, &num2);              if(choice == '1')             {                 printf("\n%.2f + %.2f = %.2f\n", num1, num2, addition(num1, num2));             }             else if(choice == '2')             {                 printf("\n%.2f - %.2f = %.2f\n", num1, num2, subtraction(num1, num2));             }             else if(choice == '3')             {                 printf("\n%.2f * %.2f = %.2f\n", num1, num2, multiplication(num1, num2));             }             else if(choice == '4')             {                 if(num2 == 0)                 {                     puts("\ndivision zero");                 }                 else if(num2 != 0)                 {                     printf("\n%.2f / %.2f = %.2f\n", num1, num2, division(num1, num2));                 }             }             else             {                 puts("\ninvalid input");             }         }     }while(1);     return 0; }  void getnumbers(float *num1_ptr, float *num2_ptr) {     printf("\nenter first number: ");     scanf("%f", num1_ptr);      printf("\nenter second number: ");     scanf("%f", num2_ptr); } float addition(float num1, float num2) {     return num1 + num2; } float subtraction(float num1, float num2) {     return num1 - num2; } float multiplication(float num1, float num2) {     return num1 * num2; } float division(float num1, float num2) {     return num1 / num2; } 

it can scanf problem? try this:

scanf(" %c", &choice); 

the way had scanf leave newline consumed in next iteration.


Comments

Popular posts from this blog

node.js - Using Node without global install -

How to access a php class file from PHPFox framework into javascript code written in simple HTML file? -

java - Null response to php query in android, even though php works properly -