0

I have been using windows all this time and recently installed Ubuntu on Virtual Box. To give Ubuntu a try,i wrote a simple calculator program.

Here's how it goes:

#include<stdio.h>
float add(float ,float ),sub(float , float ),mul(float ,float ),div(float ,float );
int main()
{
char ch;
float a,b;
printf("Enter an operator: ");
scanf("%c",&ch);
printf("Enter two values: ");
scanf("%f%f",&a,&b);
switch(ch)
{
    case '+':
        printf("The sum of %f and %f is %f\n",a,b,add(a,b));
        break;
    case '-':
        printf("The substraction of %f from %f is %f\n",a,b,sub(a,b));
        break;
    case '*':
        printf("The multiplication of %f and %f is %f\n",a,b,mul(a,b));
        break;
    case '/':
        printf("The division of %f and %f is %f\n",a,b,div(a,b));
        break;
    default:
        printf("\nEnter a valid operator: \n");
        main();
}
return 1;
}
float add(float x,float y)
{
    return (float)x + y;
}
float sub(float x,float y)
{
    return (float)x-y;
}
float mul(float x,float y)
{
    return (float) x*y;
}
float div(float x,float y)
{
    return (float) x/y;
}

when i enter an invalid operator,it should actually read the operator and values again. But, it's asking for values directly without reading the operator. Here's a picture:

Running the code

So what am i doing wrong? please explain. Thanks in advance!

Pruthvi Raj
  • 3,016
  • 2
  • 22
  • 36
  • 1
    You **never** call `main()` yourself directly. You're wanting to loop, not restart your entire application. Find a C tutorial. – Ken White Mar 30 '14 at 02:30
  • The lines of a C program execute in the order that you write them, unless there's a jump of some sort. So if you look at your own code, what happens right after you read the operator? That's not the only thing wrong with your program; there are several other problems. – Robert Harvey Mar 30 '14 at 02:30
  • @Robert but when i enter an invalid operator, it doesnt read operator again? – Pruthvi Raj Mar 30 '14 at 02:39
  • `float main()` is totally non-standard. See [What should `main()` return in C and C++?](http://stackoverflow.com/questions/204476/what-should-main-return-in-c-and-c/) for the gory details. – Jonathan Leffler Mar 30 '14 at 02:44
  • 1
    Calling `main()` is legal in C, it's just another function. He can do this and it will work until his stack overflows (or work forever if the compiler knows about tail recursion). – M.M Mar 30 '14 at 03:18

1 Answers1

3

You did not ignore the newlines in your inputs.

Change

scanf("%c", &ch);

to

scanf(" %c", &ch);

and try again.

When you input 3<enter>, that 3 will be consumed by the second %f, but that <enter> (i.e. newline) will still in the input buffer, and the %c in the first scanf() will consume this newline. The space in %c will ignore that newline in the input buffer.


$ ./a.out
Enter an operator: h
Enter two values: 2
3

Enter a valid operator: 
Enter an operator: +
Enter two values: 2
3
The sum of 2.000000 and 3.000000 is 5.000000
Lee Duhem
  • 14,695
  • 3
  • 29
  • 47
  • tried the edit too. The cursor keeps blinking and didnt read the operator second time :/ – Pruthvi Raj Mar 30 '14 at 02:37
  • @PruthviRaj It works for me. Are you sure you include all those spaces in the format strings? – Lee Duhem Mar 30 '14 at 02:39
  • Yeah! my bad sorry, why didnt it work earlier(i mean before making those changes) , can you please explain? – Pruthvi Raj Mar 30 '14 at 02:40
  • 3
    The only conversion specifiers that do not ignore leading white space are `%c`, `%n` and `%[…]` (scan sets). All the others, including `%f` specifically, automatically ignore leading white space, so the change to `"%f%f"` is not necessary. – Jonathan Leffler Mar 30 '14 at 02:42