Click here to Skip to main content
15,884,537 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is the code in C :

C#
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main()
{
    char opera;
    int num1, num2, x;
    printf("please enter an operation (+,-,*,/,%) \n");
    scanf_s("%c", &opera);
    if ((opera != '+') || (opera != '-') || (opera != '*') || (opera != '/') || (opera != '%'))
    {
        printf("your input is incorrect!\n");
    }
    else
    {
        printf("enter two numbers!\n");
        scanf_s("%d%d", &num1, &num2);
        if (opera == '+')
            printf("I am clever calculator %d+%d=%d\n", num1, num2, num1 + num2);
        if (opera == '-')
            printf("I am clever calculator %d-%d=%d\n", num1, num2, num1 - num2);
        if (opera == '*')
            printf("I am clever calculator %d*%d=%d\n", num1, num2, num1 * num2);
        if (opera == '/')
            printf("I am clever calculator %d/%d=%d\n", num1, num2, num1 / num2);
        if (opera == '%')
            printf("I am clever calculator %d%%d=%d\n", num1, num2, num1 % num2);
    }
    getchar();
    getchar();
    return 0;


}


No matter what character i type i always get a '\0' in the locals menu.
Posted
Comments
PIEBALDconsult 20-Nov-14 15:25pm    
What "locals menu" ?
Daniel Mashukov 20-Nov-14 15:30pm    
if i put a break point then down you have 3 tabs : autos locals and watch

1) This
C
scanf_s("%d%d", &num1, &num2);

will hardly work since it searches for two consecutive integers, like "32345". It sees one integer, how can it know if it was "3" "2345", "32" "345" or other?

2) Are you compiling in Debug or in Release? In Release most of the variables are optimized in registers, there is even a good article here on CP about debugging in Release. Please check.

3) You didn't tell us WHICH variable you see filled with "\0" so I assumed the problem is in the second scanf_s because it is the one with the obvious [possible] flaw.

Hope that helps
Denis

ADDENDUM:
You may be using Unicode charset or wide characters. In that case the first byte of opera will be always 00H because the ASCII set is mapped in Unicode leaving the first byte to 0. Try disallowing Unicode adding

C++
#undef _UNICODE
#undef UNICODE


at the beginning of the source file.
 
Share this answer
 
v2
Comments
den2k88 20-Nov-14 15:48pm    
More on ASCII vs UNICODE
https://social.msdn.microsoft.com/Forums/en-US/8d16d37d-a956-4932-9662-b73c4cf4d0e6/turn-off-unicode-on-platform-sdk-using-visual-c-express?forum=Vsexpressvc
my guess is you mean here :-

scanf_s("%d%d", &num1, &num2);


so num2 is null ? you have to read the numbers individually ie:-

scanf_s("%d", &num1);

scanf_s("%d", &num2);


iirc the 1st %d reads all the input into num1 so there's nothing left for num2 - from the spec even a space inbetween num1 and num2 in the string will be absorbed
 
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