-2

So here is my code of a simple calculator I'm trying to build in C (I'm learning C):

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>

int main()
{
    char Operation;
    printf("Welcome to Double-Figure Calculator!\n\n");
    Sleep(1000);
    printf("What operation would you like to do? (+, -, *, /,) \n");
    Sleep(1000);
    scanf("%c\n\n", Operation);
    Sleep(1000);

    if (Operation == '+'){

        int FirstNumber;
        int SecondNumber;

            printf("Please enter your first number\n");
            scanf("%s\n\n", &FirstNumber);

            printf("Please enter your second number\n");
            scanf("%s\n\n", &SecondNumber);
            Sleep(1000);

            printf("Calculating");

            Sleep(1000);
            printf(".");

            Sleep(1000);
            printf(".");

            Sleep(1000);
            printf(".\n\n");

            printf("Your calculation is %d\n\n", FirstNumber + SecondNumber);

            system("Pause");

    }

    if (Operation == '-'){

        int OneFirstNumber;
        int OneSecondNumber;


        printf("Please enter your first number\n");
        scanf("%s\n\n", &OneFirstNumber);

        printf("Please enter your second number\n");
        scanf("%s\n\n", &OneSecondNumber);
        Sleep(1000);

        printf("Calculating");

        Sleep(1000);
        printf(".");

        Sleep(1000);
        printf(".");

        Sleep(1000);
        printf(".\n\n");

        printf("Your calculation is %d\n\n", OneFirstNumber - OneSecondNumber);

        system("Pause");

    }

    if (Operation == '*'){

        int TwoFirstNumber;
        int TwoSecondNumber;


        printf("Please enter your first number\n");
        scanf("%s\n\n", &TwoFirstNumber);

        printf("Please enter your second number\n");
        scanf("%s\n\n", &TwoSecondNumber);
        Sleep(1000);

        printf("Calculating");

        Sleep(1000);
        printf(".");

        Sleep(1000);
        printf(".");

        Sleep(1000);
        printf(".\n\n");

        printf("Your calculation is %d\n\n", TwoFirstNumber * TwoSecondNumber);

        system("Pause");

    }

    if (Operation == '/'){

        int ThreeFirstNumber;
        int ThreeSecondNumber;


        printf("Please enter your first number\n");
        scanf("%s\n\n", &ThreeFirstNumber);

        printf("Please enter your second number\n");
        scanf("%s\n\n", &ThreeSecondNumber);
        Sleep(1000);

        printf("Calculating");

        Sleep(1000);
        printf(".");

        Sleep(1000);
        printf(".");

        Sleep(1000);
        printf(".\n\n");

        printf("Your calculation is %d\n\n", ThreeFirstNumber / ThreeSecondNumber);

        system("Pause");

    }

    system("pause");
    return 0;

}

The problem is whenever I run the code, and enter an operation, so +,-,*, or /, and then press enter, nothing happens, even though I have if statements there to carry out the rest of the program. I can't figure out what I'm doing wrong here. Any help? (Sorry, I'm new to programming, and am in the early stages of learning C).

2 Answers2

1

You have some mistakes in your code that you could improve.

The first one, when you use scanf, you must pass the variable address to store the data, for example:

scanf("%c", &Operation);

Avoid using "\n\n" in scanf, it tells that you expect an char and two returns from user input. If the user don't do that, your code will not work properly.

I could notice you are using scanf wrong in the most cases regarding data type. If you want to get an integer value from user with scanf, you must use the corresponding conversion %d to get an integer value, for example:

scanf("%d\n", &secondNumber);

When you use %s, you are telling that you expect an string from user's input, it can crash your code if you store an string into an integer variable.

Another point is related to use if statement to switch the user's input, it would be better to use switch-case statements, for example:

switch(operation) {
case '+':
    // do add operation
    break;
case '-': 
    // do sub operation
    break;
default:
    // return some error message.
}

In switch-case statement, the "default" is a routine that you want to perform if none of the cases don't match with what you expect.

I thing this is all you need to do to get your code working.

campescassiano
  • 809
  • 5
  • 18
  • Thanks for replying, I'm going to make the changes and see if it works. – Hamza Qayyum Sep 07 '15 at 22:01
  • Just fixed it/ You were right, even one \n in scanf("%d\n", &FirstNumber); made the computer expect two user inputs, so enter something twice, and hit enter each time. All I did was take away the \n's, and problem solved! Thanks a lot man, and thanks to everyone else for your input. Learned something new today! – Hamza Qayyum Sep 07 '15 at 22:35
0
scanf("%c\n\n", &Operation);

To scan into the value, you need to give scanf an address.

mksteve
  • 12,614
  • 3
  • 28
  • 50