Click here to Skip to main content
15,918,330 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
CODE 1

#include <stdio.h>
int main()
{

    int a;

    printf("Enter the value of a\n");
    scanf("%d", &a);

    int c;

    printf("Enter the value of c\n");
    scanf("%d", &c);

    char command;

    printf("Enter the  command \n");
    scanf("%c", &command);
 printf("Enter the  command \n");
    scanf("%c", &command);
    if (command == '+')
    {
        printf("%d ", a + c);
    }

    if (command == '-')
    {
        printf("%d ", a - c);
    }
    return 0;
}




// VS code 2



#include <stdio.h>
int main()
{

    int a;

    printf("Enter the value of a\n");
    scanf("%d", &a);

    int c;

    printf("Enter the value of c\n");
    scanf("%d", &c);

    char command;

 printf("Enter the  command \n");
    scanf("%c", &command);
    if (command == '+')
    {
        printf("%d ", a + c);
    }

    if (command == '-')
    {
        printf("%d ", a - c);
    }
    return 0;
}


What I have tried:

I have faced this problem with scanf many times
Posted
Updated 24-Jul-22 6:42am
Comments
Greg Utas 24-Jul-22 9:24am    
What problem have you faced with scanf? You haven't told us.
Richard MacCutchan 24-Jul-22 12:13pm    
See my answer below.

I will guess that you have a whitespace problem. See this write-up on scanf[^]. That's my go-to site when I have detailed questions about C++. It's also great for C, which I don't use. Note the example part way down, which looks very much like yours:
C
scanf("%d", &a);
scanf(" %c", &c); // consume all consecutive whitespace after %d, then read a char
Note the space at the beginning of their " %c". This suggests that there was still whitespace in the buffer after you read the second integer, and that your first call to scanf returned it (probably a '\n') before the second one read the character that you entered.

You would have figured this out if you had checked if c was a '+', '-', or something else. scanf also returns an int, which indicates whether it succeeded, and this is also something that you should check. Simply assuming that your code works leads to this kind of bewilderment. When you work on large programs that need to be resilient, as much as 90% of the code involves checking for, and handling, error situations.
 
Share this answer
 
v2
As Richard said, we had already discussed the basic problem.
C
int c;

printf("Enter the value of c\n");
scanf("%d", &c);

char command;
    
printf("Enter the  command \n");
scanf("%c", &command);
printf("Enter the  command \n");
scanf("%c", &command);


The first entry is skipped directly because the end-of-line character from the previous entry was still in the buffer. So it would make sense to read until the end of line character was read.

You need something like this:
C
char c
do { scanf("%c", &c);} while (c != '\n');
 
Share this answer
 
v2
You were given a suggested solution in your original post at Why when subtracting I am not getting value of C[^]. Please do not repost.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900