Thread: What am I doing wrong

  1. #1
    Registered User
    Join Date
    Aug 2022
    Posts
    3

    What am I doing wrong

    I am a beginner and attempted to make an calculator which uses i else to add or subtract. Whenever i choose add it still subtracts.Here is the code:
    Code:
    #include <stdio.h>
    main()
    {
        int x, y, z, sum, sub;   /* make variables for storing numbers to be added or subtracted and the variables to store the sum and difference */
        char type; /* variable to determine addition or subtraction */ 
        printf("This calculator only knows how to add and subtract, so enter you want to add or subtract by A for add and anything else for subtract.\n ");
        scanf(" %c", &type);
        if (type == "A") /* if the user's input is A then there will be addition */
        {
            printf("Type the first number.\n "); /* inputs first number */ 
            scanf(" %d", &x);
            printf("Type the second one.\n ");
            scanf(" %d", &y);
            printf("Type the last one.\n ");
            scanf(" %d", &z);
            sum = x + y + z; /* determines the value of the sum*/
            printf("The sum is: %d", sum); /* prints the sum*/
        }
        else /*otherwise there will be subtraction*/
        {
    
    
            printf("Type the first number.\n ");
            scanf(" %d", &x);
            printf("Type the second one.\n ");
            scanf(" %d", &y);
            printf("Type the last one.\n ");
            scanf(" %d", &z);
            sub = x - y - z;
            printf("The difference is: %d", sub);
        }
    return 0;
    }
    Even if I type A the calculator would subtract the numbers

  2. #2
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    scanf scans the stdin stream and tries to convert the input string to the object pointed using the format specified...
    scanf returns the number of successful conversions made... if you don't check this, no data is writen to the target object. You could do:
    Since the conversion fails (and scanf will return 0 [or -1 in case of error]), stdin sill has the previous string to be rescanned. There is no portable way to flush the input stream (and using fflush( stdin ) is an undefined behavior or a plain ERROR).

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Not paying attention to your compiler error messages would be something you're doing wrong.
    Code:
    main.c:2:1: warning: return type defaults to ‘int’ [-Wimplicit-int]
     main()
     ^~~~
    main.c: In function ‘main’:
    main.c:8:14: warning: comparison between pointer and integer
         if (type == "A") /* if the user's input is A then there will be addition */
                  ^~
    If you're not seeing this, or something like it, then you need to either increase the warning level or get a better compiler.

    For the comparison, use
    Code:
     if (type == 'A' )
    Single quotes are for lone characters.
    Double quotes are for strings.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Aug 2022
    Posts
    3
    Thanks, I didn't notice the warning but it still says:
    Code:
    warning: return type defaults to 'int' [-Wimplicit-int]|
    ||=== Build finished: 0 error(s), 1 warning(s) (0 minute(s), 0 second(s)) ===|

  5. #5
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,110
    Quote Originally Posted by Silver2000 View Post
    Thanks, I didn't notice the warning but it still says:
    Code:
    warning: return type defaults to 'int' [-Wimplicit-int]|
    ||=== Build finished: 0 error(s), 1 warning(s) (0 minute(s), 0 second(s)) ===|
    main() should be explicitly defined to return an int.
    Code:
    int main(void)
    {
       // ...
      return 0;
    }

  6. #6
    Registered User
    Join Date
    Aug 2022
    Posts
    3
    Thanks, I should have done more research. This board really is helpful.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why so wrong numbers? As I write so wrong?
    By Dmy in forum C++ Programming
    Replies: 2
    Last Post: 07-31-2017, 02:10 PM
  2. Replies: 3
    Last Post: 11-14-2011, 06:35 PM
  3. wrong wrong with my xor function?
    By Anddos in forum C++ Programming
    Replies: 5
    Last Post: 04-26-2009, 01:38 PM
  4. whats wrong with this? no errors but wrong result
    By InvariantLoop in forum C Programming
    Replies: 6
    Last Post: 01-28-2005, 12:48 AM
  5. Replies: 9
    Last Post: 07-15-2004, 03:30 PM

Tags for this Thread