Thread: using if statement

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    41

    using if statement

    Not sure where I got lost

    Code:
    :
    #include <stdio.h>
    #include <math.h>
    
    int main()
    {
      int opselect;
      float A, S;
     
      printf("Please type in one of the following letters:");
      printf("\nA to add three numbers:");
      printf("\nS to subtract two number:");
      scanf("%d" &opselect);
      printf("Please type in three numbers:\n");
      scanf("%f", &A);
      scanf("%f", &A);
      scanf("%f", &A);
      printf("Please type in two numbers");
      scanf("%f", &S);
      scanf("%f", &S);
        
      switch (opselect);
    }
     Add:
      if (A != a);
      printf("The total of two numbers is %.2f\n", (A+A+A));
      else
      printf("Please type in capital A");
      break;
    
     Subtract:
      if (S != s);
      printf("Three numbers subtracted is %.2f\n", (S-S));
      else
      printf("Please type in capital S);
      break;    
    {
       return 0;
    }
    Last edited by wonderpoop; 10-08-2006 at 06:46 PM.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Your code doesn't seem to be very coherent, and you have a number of misplaced semicolons. Regarding the first, could you explain what you want the code to do. For the second, back up in your reference.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    41
    Quote Originally Posted by Dave_Sinkula
    Your code doesn't seem to be very coherent, and you have a number of misplaced semicolons. Regarding the first, could you explain what you want the code to do. For the second, back up in your reference.
    I need to have a user first select a letter that will

    A add 3 numbers
    S subtract 2 numbers

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    swich syntax is like this:
    Code:
    switch(expression)
    {
         case val1:
              //do stuff if expression was equal to val1
         case val2:
              //do stuff if expression was equal to val2 or, after doing stuff for val1
             break;
         case val3:
              //do stuff is expression is equal to val3;
              break;
         default:
              //if expression is none of the above do this.
    }
    if else syntax is like this, for 1 line conditions. Note the indent. It is important to make your code easy to read.
    Code:
    if(epression)
         puts("The expression is true");
    else
        puts("The expression is false");
    So your code should be like this:
    Code:
       ...
    
      switch (opselect)
      {
        case 'A':
          if (A != a);
            printf("The total of two numbers is %.2f\n", (A+A+A));
          else
             printf("Please type in capital A");
          break;
    
      case S:
        if (S != s);
          printf("Three numbers subtracted is %.2f\n", (S-S-S));
        else
          printf("Please type in capital S);
        break;    
      }
    }
    But that still won't work because your other code is wrong. And I have no clue what if(S != s) is supposed to do.
    You should read some toutorals on reading in variables.
    Last edited by King Mir; 10-08-2006 at 06:56 PM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    25
    Edit: Lets go through your code first... then ill post a fixed program...

    Code:
    :  //Useless... 
    #include <stdio.h>
    #include <math.h>
    
    int main()
    {
      int opselect;
      float A, S;  // make them arrays because your taking multiple inputs... 
     
      printf("Please type in one of the following letters:");
      printf("\nA to add three numbers:");
      printf("\nS to subtract two number:");
      scanf("%d" &opselect);  // reading in a decimal when asking for a char... 
      printf("Please type in three numbers:\n");
      scanf("%f", &A);  // Putting each input into the same variable... overwriting  
      scanf("%f", &A);   // the previous each time...    
      scanf("%f", &A);
      printf("Please type in two numbers");
      scanf("%f", &S);  // same as the above... 
      scanf("%f", &S);
        
      switch (opselect);  // have no idea why your using a switch here at all...
    }  // Backwards... 
     Add:
      if (A != a);  // what, why? 
      printf("The total of two numbers is %.2f\n", (A+A+A));  //This will just add the same number 3 times... 
      else
      printf("Please type in capital A");
      break;
    
     Subtract:
      if (S != s);  // again, what, why? 
      printf("Three numbers subtracted is %.2f\n", (S-S));  // again, subtracting the same number... 
      else
      printf("Please type in capital S);
      break;    
    {
       return 0;
    }

    Heres a working program.

    Code:
    int main()
    {
      char opselect;
      float A[3], S[2];
     
      printf("Please type in one of the following letters:");
      printf("\nA to add three numbers");
      printf("\nS to subtract two number");
      printf("> ");
      scanf("%c" &opselect);
    
      if (opselect == "A" || opselect == "a")
      {
         printf("\n\nPlease type in the first number: ");
         scanf("%f", &A[0]);
         printf("\nPlease type in the second number: ");
         scanf("%f", &A[1]);
         printf("\nPlease type in the third number: ");
         scanf("%f", &A[2]);
    
         printf("The total of these numbers is: %f", (A[0]+A[1]+A[2]));
       }
       elseif (opselect == "B" || opselect == "b")
       {
         printf("\n\nPlease type in the first number: ");
         scanf("%f", &B[0]);
         printf("\nPlease type in the second number: ");
         scanf("%f", &B[1]);
    
         printf("The difference of these numbers is: %f", (B[0]-B[1]));
       }
       else
       {
         printf("\n\nInvalid input.");
       }
         
       return 0;
    }
    Last edited by JDD; 10-08-2006 at 07:07 PM.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > if (opselect == "A" || opselect == "a")
    This "works" ???
    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.

  8. #8
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Shouldn't it be like this?
    Code:
    if (opselect == 'A' || opselect == 'a')
    Different quotes mean different things in C++.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    41

    rewritten

    this is now rewritten. My problem though is that it wants to modulate everything after I press a key. So for example, I will press A to add, it will do the add function, then it will ask me for the numbers to modulate.

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <math.h>
    
      int main()
    {
      float A[3], S[2], M[3], D[2], O[2];
      float add, subtract, multiply, divide, modulate;
      char E;
      char choice;
      
      printf("Please type in one of the following letters:");
      printf("\nA to Add three numbers: ");
      printf("\nS to Subtract two numbers: ");
      printf("\nM to Multiply three number: ");
      printf("\nD to Divide two number: ");
      printf("\nO to Modulate two number: ");
      printf("\nE to Exit program: ");
      printf("\n>");
      scanf("%c", &choice);
        
    if (choice == 'A' || choice == 'a')
      {
      printf("\nPlease type in the first number: ");
      scanf("%f", &A[0]);
      printf("\nPlease type in the second number: ");
      scanf("%f", &A[1]);
      printf("\nPlease type in the third number: ");
      scanf("%f", &A[2]);
      add=A[0]+A[1]+A[2];  
      printf("\n%.2f plus %.2f plus %.2f is: %.2f", A[0], A[1], A[2], add);
      }
    if (opselect == 'S' || opselect == 's')
      {
      printf("\nPlease type in the first number: ");
      scanf("%f", &S[0]);
      printf("\nPlease type in the second number: ");
      scanf("%f", &S[1]);
      subtract=S[0]-S[1];
      printf("\n%.2f minus %.2f minus %.2f is: %.2f", S[0], S[1], subtract);
      }
    if (choice == 'M' || choice == 'm')
      {
      printf("\nPlease type in the first number: ");
      scanf("%f", &M[0]);
      printf("\nPlease type in the second number: ");
      scanf("%f", &M[1]);
      printf("\nPlease type in the third number: ");
      scanf("%f", &M[2]);
      multiply=M[0]*M[1]*M[2];
      printf("\n%.2f times %.2f times %.2f is: %.2f", M[0],M[1], M[2], multiply);
      }
    if (choice == 'D' || choice == 'd')
      {
          printf("\nPlease type in the first number: ");
          scanf("%f", &D[0]);
          printf("\nPlease type in the second number: ");
          scanf("%f", &D[1]);
          divide=D[0]/D[1];
          printf("\n%.2f divided by %.2f is: %.2f", D[0], D[1], divide);
      }
     if (choice == 'O' || choice == 'o');
     {
       printf("\nPlease type in the first number: ");
       scanf("%f", &O[0]);
       printf("\nPlease type in the second number: ");
       scanf("%f", &O[1]);
       modulate = fmod(O[0],O[1]);
       printf("\nThe modulus of %.2f and %.2f is remainder: %.2f", O[0], O[1], modulate);
       
     }
    if (choice == 'E' || choice == 'e')
       {
         printf("\nThank You and Goodbye!: ");
         scanf("%c", &E);
       }
    
    return 0;
    }
    <<continued here>>
    Last edited by wonderpoop; 10-09-2006 at 03:24 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Usefulness of the "else if" statement
    By gn17 in forum C Programming
    Replies: 7
    Last Post: 08-12-2007, 05:19 AM
  2. Meaning of this statement?
    By @nthony in forum C Programming
    Replies: 7
    Last Post: 07-16-2006, 02:57 AM
  3. If Else statement problem
    By doofusboy in forum C Programming
    Replies: 2
    Last Post: 11-09-2005, 07:18 AM
  4. if/break statement
    By Apropos in forum C++ Programming
    Replies: 7
    Last Post: 02-22-2005, 02:33 PM
  5. Uh-oh! I am having a major switch problem!
    By goodn in forum C Programming
    Replies: 4
    Last Post: 11-01-2001, 04:49 PM