0

I am trying to do a simple program where i will first give the operation name and 2nd will give two number. Based on the operation name it will show me result. It will be done inside a infinite for loop.

But Here my problem is that the code is working for first iteration, from 2nd it is not working properly. gets() not taking input.

I have given the source here please where the mistake i have done.

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
    char command[80];
    float i,j;

    for( ; ; )
    {
        printf("what operation you want???\n");
        gets(command);

        if( !strcmp(command,"quit") )
        {
            break;
        }

        printf("enter first number :\n");
        scanf("%f",&i);
        printf("enter second number :\n");
        scanf("%f",&j);

        if(!strcmp(command,"add"))
            printf("%f\n",i+j);
        else if(!strcmp(command,"divide"))
            printf("%f\n",i/j);
        else if(!strcmp(command,"multiply"))
            printf("%f\n",i*j);
        else if(!strcmp(command,"subtract"))
            printf("%f\n",i-j);
        else printf("unknown command\n");
    }
    return 0;
} 
coder
  • 1
  • 1
  • Put this: `printf("command: \"%s\"", command);` directly behind the `gets()` call and think about the result. – Daniel Jour Aug 31 '16 at 07:25
  • 1
    it is not taking input for second time – coder Aug 31 '16 at 07:30
  • @BLUEPIXY Thanks. I wonder why they placed the "object identity" argument last for regular file operations when most other C api:s has it first. I've lost track of how many times the compiler has had to remind me. Replacing my comment with a corrected version. – Klas Lindbäck Aug 31 '16 at 08:53
  • Side note: gets() has been removed from the C language because it is unsafe. You should use fgets(command, sizeof command, stdin) instead. – Klas Lindbäck Aug 31 '16 at 08:55

0 Answers0