Click here to Skip to main content
15,881,687 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
#include <stdio.h>
int main()
{
    
    int a;

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

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

    int c;

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

    printf("%d", c);
    char command;


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

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


What I have tried:

I a not able to get value of C
Posted
Updated 24-Jul-22 3:27am

There are several problems with the code. The scanf function should NEVER be used with a character string because of an unavoidable overflow.
C
scanf("%s", &command);

In this case, however, only a single character should be read anyway. However, since at least the end-of-line character is still in the buffer from the previous reading of the variable c, it is read here. It is therefore advisable to empty the buffer completely before reading in. I've added a function for this - there are several ways.
C
scanf("%d", &c);
printf("%d\n", c);

char command;
printf("Enter the  command \n");
clearstdin();
scanf("%c", &command);
 
Share this answer
 
You are getting the command character incorrectly. You declare command as a char type but try to scan it as a string. So change the scanf call thus:
C++
printf("Enter the  command \n");
scanf("%c", &command); // use %c for a single character.


See Solution 2 by OriginalGriff.
 
Share this answer
 
v2
Comments
OriginalGriff 24-Jul-22 8:57am    
That won't work either, the '\n' from the previous entry is still in the input buffer!
Richard MacCutchan 24-Jul-22 9:03am    
Damn, I thought I had tested that part.
Richard MacCutchan 24-Jul-22 9:09am    
scanf, how do I hate thee, let me count the ways ...
OriginalGriff 24-Jul-22 9:19am    
Yep.
Always have, always will. :D
Try this instead:
C
printf("Enter the  command \n");
do
{
    scanf("%c", &command);

    if (command == '+')
    {
        printf("%d ", a + c);
        break;
    }

    if (command == '-')
    {
        printf("%d ", a - c);
        break;
    }
} while (1 == 1);
 
Share this answer
 
Quote:
Why when subtracting I am not getting value of C

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
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