0

i think my code skips the line scanf("%c", &operation); when i run the program, but when i move the scanf"%c" above the scanf"%d" it works... also when i change "%c" to "%s" it works too... here's my code...

#include <stdio.h>
main()
{
    char operation;
    int num1, num2, ans = 0;
    printf("Enter number 1: ");
    scanf("%d", &num1);
    printf("Enter number 2: ");
    scanf("%d", &num2);
    printf("A - Addition \n");
    printf("S - Subtraction \n");
    printf("M - Multiplication \n");
    printf("D - Division \n");
    printf("Enter a character for the operation: ");
    scanf("%c", &operation); // this line
    switch(operation) {
    case 'A':
        ans = num1 + num2;
        break;
    case 'S':
        ans = num1 - num2;
        break;
    case 'M':
        ans = num1 * num2;
        break;
    case 'D':
        ans = num1 / num2;
        break;
    default:
        printf("%c is an invalid character \n", &operation);
    }
    printf("asnwer is %d", ans);
}

moving up scanf"%c"

#include <stdio.h>
main()
{
    char operation;
    int num1, num2, ans = 0;
    printf("A - Addition \n");
    printf("S - Subtraction \n");
    printf("M - Multiplication \n");
    printf("D - Division \n");
    printf("Enter a character for the operation: ");
    scanf("%c", &operation); // this line
    printf("Enter number 1: ");
    scanf("%d", &num1);
    printf("Enter number 2: ");
    scanf("%d", &num2);
    switch(operation) {
    case 'A':
        ans = num1 + num2;
        break;
    case 'S':
        ans = num1 - num2;
        break;
    case 'M':
        ans = num1 * num2;
        break;
    case 'D':
        ans = num1 / num2;
        break;
    default:
        printf("%c is an invalid character \n", &operation);
    }
    printf("asnwer is %d", ans);
}

changing %c to %s...

#include <stdio.h>
main()
{
    char operation;
    int num1, num2, ans = 0;
    printf("Enter number 1: ");
    scanf("%d", &num1);
    printf("Enter number 2: ");
    scanf("%d", &num2);
    printf("A - Addition \n");
    printf("S - Subtraction \n");
    printf("M - Multiplication \n");
    printf("D - Division \n");
    printf("Enter a character for the operation: ");
    scanf("%s", &operation); // this line
    switch(operation) {
    case 'A':
        ans = num1 + num2;
        break;
    case 'S':
        ans = num1 - num2;
        break;
    case 'M':
        ans = num1 * num2;
        break;
    case 'D':
        ans = num1 / num2;
        break;
    default:
        printf("%c is an invalid character \n", &operation);
    }
    printf("asnwer is %d", ans);
}

why my first code don't work? and why the other 2 works fine? any reason behind it?

Vitas
  • 149
  • 13
  • Mixing character, string, and decimal input with `scanf` is the No. 1 pitfall new C programmers fall into. Why? Failing to account for characters left in the input buffer (`stdin`) and failing to understand which *format specifiers* consume leading whitespace and which don't. The No. 2 pitfall is failing to validate the return of `scanf` to determine whether a successful conversion took place or whether a *matching* or *input* failure occurred. These questions have been answered 100s of times here. Search around. – David C. Rankin Oct 21 '17 at 05:47
  • hmmmp. it looks like a homework. – winnie damayo Oct 21 '17 at 05:48
  • Examples: [c - scanf won't ask for input the second time](https://stackoverflow.com/questions/13372923/scanf-wont-ask-for-input-the-second-time), [c - My program skip getting input data?](https://stackoverflow.com/questions/18721375/my-program-skip-getting-input-data), [c - scanf Getting Skipped - Stack Overflow](https://stackoverflow.com/questions/14484431/scanf-getting-skipped) – David C. Rankin Oct 21 '17 at 05:51
  • ohhh... so that's how it works.... thank you :) no... it's not a homework... actually i copied it in a book... the exact syntax i copied is not working... – Vitas Oct 21 '17 at 05:57
  • [A beginners' guide *away from* `scanf()`](http://sekrit.de/webdocs/c/beginners-guide-away-from-scanf.html) –  Oct 21 '17 at 06:15

0 Answers0