Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    dBx
    dBx is offline
    New to the CF scene
    Join Date
    Apr 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation [C] Calculator help.

    Code:
     
    //Calculator written in C
    //Coded by dBx @ h4ckinab0x
    #include <stdio.h>
    #include <conio.h>
    
    
    int main()
    {
    
          printf("On Your Marks. Get Set. Calculate!\n\n");
          printf("Press [1] for Addition\n");
          printf("Press [2] for Subtraction\n");
          printf("Press [3] for Multiplication\n");
          printf("Press [4] for Division\n");
          getch();
          
         
          
          if("choice==1") 
          
          {
                          
           int num1;
           int num2;
           int sum;
           
          printf("Enter Your Number:\n");
        
        scanf("%d", &num1);
        scanf("%d", &num2);
        
        sum = num1 + num2;
        
        printf("The Answer is %d!", sum); 
        
    }
    
         else if("choice==2")
        
        {
         
           int num1;
           int num2;
           int sub;
    
         printf("Enter Your Number:\n");
        
        scanf("%d", &num1);
        scanf("%d", &num2); 
        
        sub = num1 - num2;
        
        printf("The Answer is %d!", sub); 
        
    }
       
          else if("choice==3")
        
        {
          
           int num1;
           int num2;
           int multi;
    
       printf("Enter Your Number:\n");
        
        scanf("%d", &num1);
        scanf("%d", &num2); 
        
        multi = num1 * num2;
        
        printf("The Answer is %d!", multi); 
        
    }
        
      else if("choice==4")
        
        {
        
           int num1;
           int num2;
           int dvs;
    
       printf("Enter Your Number:\n");
        
        scanf("%d", &num1);
        scanf("%d", &num2); 
        
        dvs = num1 / num2;
        
        printf("The Answer is %d!", dvs); }
        getch();
    }
    What am i doing wrong...when i compile it,
    the exe shows up. but the functions dont work.
    i think i have to set them as for example;
    sub = subtraction;
    etc.
    also i think i should use switch/case statements.
    or do while loops..
    but im having trouble with it ]:
    Last edited by dBx; 04-26-2009 at 08:28 PM.

  • #2
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    I don't see how this code could have compiled at all. You are manipulating variables you never declared.

    And if statements like this:
    Code:
    if("choice==1")
    are always going to return true because it will evaluate the address of that string literal as a boolean. And since no string literal will have an address of zero, it will always be true.
    OracleGuy


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •