Thread: scanf problem. with very simple calculator.

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    18

    Question scanf problem. with very simple calculator.

    The program works fine up until after the first calculation. The program prompts the user to enter Y or N to calculate again. But scanf wont take the the input.
    Also I know it would be better to use a loop instead of a goto statement but my book asks me to use the goto for now because it hasn't gotten to explaining loops yet.

    Code:
    /*Example 3.11 A calculator*/
    #include<stdio.h>
    int main()
    {
    double number1 = 0.0; /*first operand value a decimal number */
    double number2 = 0.0; /*second operand value a decimal number */
    char operation = 0; /*operation - must be +, -, *, /, or % */
    char again = 0;
    there: printf("\nEnter the calculation\n");
    scanf("%lf %c %lf", &number1, &operation, &number2);
    /*code to check the input goes here*/
    switch(operation)
    {
    case'+':
    printf("=%lf\n", number1 + number2);
    break;
    case'-':
    printf("=%lf\n", number1 - number2);
    break;
    case'*':
    printf("=%lf\n", number1 * number2);
    break;
    case'/':
    if(number2 == 0)
    printf("\n\n\aDivision by zero error!\n");
    else
    printf("=%lf\n", number1 / number2);
    break;
    case'%':
    if((long)number2 == 0)
    printf("\n\n\aDivision by zero error!\n");
    else
    printf("=%ld\n", (long)number1 % (long)number2);
    break;
    default:
    printf("\n\n\aIllegal operation!\n");
    break;
    }
    printf("\nWould you like to carry out another calculation Y or N? ");
    scanf("%c", &again);
    if(again == 'y' || again == 'Y')
    goto there;
    }
    

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Also I know it would be better to use a loop instead of a goto statement but my book asks me to use the goto for now because it hasn't gotten to explaining loops yet.
    Then I suggest you find a better book!!

    The program prompts the user to enter Y or N to calculate again. But scanf wont take the the input.
    This is happening because your previous scanf() left the end of line character in the input buffer. To fix this issue you can add a line before you %c in your scanf().
    Code:
    scanf(" %c", &again);
    This tells scanf() to skip the leading white space characters.

    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-15-2012, 11:25 PM
  2. Simple Calculator (input problem)
    By grache28 in forum C++ Programming
    Replies: 15
    Last Post: 12-02-2011, 07:10 PM
  3. simple calculator problem...
    By yeohwl91 in forum C Programming
    Replies: 14
    Last Post: 09-27-2011, 08:35 AM
  4. simple scanf() problem
    By qurilly007 in forum C Programming
    Replies: 3
    Last Post: 05-20-2011, 02:36 AM
  5. Having some problem with my simple calculator
    By Aven in forum C Programming
    Replies: 3
    Last Post: 07-21-2002, 09:03 AM