Thread: Multiple if statements

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    2

    Multiple if statements

    I cannot seem to get any of the if statements other than the first one to work. I have searched high and low for the cause and have come up with nothing.

    Code:
    #include <stdio.h>
    #include <conio.h>
    
    int main(void)
    {
        int add;
        int subt;
        int mult;
        int divi;
        int num1;
        int num2;
        int sum;
        
        printf("Add(1), Subtract(2), Multiply(3), Divide(4) \nEnter the corrisponding number for the action you want to take: ");
        scanf("%d", &add, &subt, &mult, &divi);
        
        if(add == 1){
           printf("Enter first digit: ");
           scanf("%d", &num1);
           printf("Enter second digit: ");
           scanf("%d", &num2);
           
           sum = num1 + num2;
           printf("\nAnswer: %d", sum);
        }
        if(subt == 2){
           printf("Enter first digit:");
           scanf("%d", &num1);
           printf("Enter second digit:");
           scanf("%d", &num2);
           
           sum = num1 - num2;
           printf("\nAnswer: %d", sum);
        }         
        if(mult == 3){
           printf("Enter first digit:");
           scanf("%d", &num1);
           printf("Enter second digit:");
           scanf("%d", &num2);
           
           sum = num1 * num2;
           printf("\nAnswer: %d", sum);
        }
        if(divi == 4){
           printf("Enter first digit:");
           scanf("%d", &num1);
           printf("Enter second digit:");
           scanf("%d", &num2);
           
           sum = num1 / num2;
           printf("\nAnswer: %d", sum);
        }
        
        
        getch();
        
        }

  2. #2
    Registered User javaeyes's Avatar
    Join Date
    Feb 2012
    Posts
    153
    You don't want to have 4 variables add,subt,mult and divi. Just create one called 'operation' or something similar. There are some other things you could do to make the code cleaner but that's your big problem.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Code:
    scanf("%d", &add, &subt, &mult, &divi);
    You're only telling the "scanf()" to read in one integer, but are giving it four arguments. Perhaps you mean to read the user input into a single variable, and use that single variable in all four of you "if()" statements.

    Also, you're correctly declaring main as returning an integer, so don't forget the "return 0" statement at the end of "main()".

  4. #4
    Registered User
    Join Date
    Sep 2012
    Posts
    2
    Thank you for the help guys that got it working. This is actually my first piece of code to write by myself without a tutorial. Still a lot to learn!

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Indeed - and you seem to be going about your learning in the right way. Keep it up!

  6. #6
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Also, keep in mind for the statement sum = num1 / num2;
    if num1 = 9 and num2 = 10 the answer is 0.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. if statement with multiple switch statements.
    By blu in forum C Programming
    Replies: 27
    Last Post: 02-16-2012, 11:05 PM
  2. help with multiple switch statements and arrays
    By dangalong801 in forum C Programming
    Replies: 7
    Last Post: 05-23-2011, 01:58 AM
  3. Multiple if statements and conditions
    By tomeatworld in forum C Programming
    Replies: 19
    Last Post: 11-07-2010, 10:43 AM
  4. Multiple If Statements Question.
    By thekautz in forum C++ Programming
    Replies: 3
    Last Post: 11-07-2008, 03:06 PM
  5. Replies: 1
    Last Post: 05-01-2003, 02:52 PM

Tags for this Thread