What am I doing wrong?

This is a discussion on What am I doing wrong? within the C Programming forums, part of the General Programming Boards category; I am trying to get this program to display an error message when the user inputs a zero for the ...

  1. #1
    Registered User skaldicpoet9's Avatar
    Join Date
    Dec 2007
    Posts
    15

    What am I doing wrong?

    I am trying to get this program to display an error message when the user inputs a zero for the base of an exponent. However, whenever I compile the program the program does not display an error message when I type in zero. I have looked over this code backwards and forwards but still can't find out what is wrong. The line in question is line sixty-four with the expression:

    if((num1 == 0)&&(num2 <=0)); {
    printf("That is not an acceptable value!\n");
    break; }

    Can someone please help me? Thanks

    Code:
    #include <stdio.h>
    #include <math.h>
    
    int menu(void);
    
    int main(void) {
       int selection,num1,num2,ans,fans;
       selection = menu();
       while(selection != 8)  {
          switch(selection) {
              case 1: printf("Enter 1st number: ");
                      scanf("&#37;d", &num1);
                      printf("Enter 2nd number: ");
                      scanf("%d", &num2);
                      ans = num1 + num2;
                      printf("%d + %d = %d\n",num1,num2,ans);
                      break;
              case 2: printf("Enter 1st number: ");
                      scanf("%d", &num1);
                      printf("Enter 2nd number: ");
                      scanf("%d", &num2);
                      ans = num1 - num2;
                      printf("%d - %d = %d\n",num1,num2,ans);
                      break;
              case 3: printf("Enter 1st number: ");
                      scanf("%d", &num1);
                      printf("Enter 2nd number: ");
                      scanf("%d", &num2);
                      ans = num1 * num2;
                      printf("%d * %d = %d\n",num1,num2,ans);
                      break;
              case 4: printf("Enter 1st number: ");
                      scanf("%d", &num1);
                      printf("Enter 2nd number: ");
                      scanf("%d", &num2);
                      if(num2 == 0) {
                      printf("Zero is not an acceptable value!\n");
                      break; }
                      fans =(float)num1/num2;
                      printf("%d / %d = %2d\n",num1,num2,fans);
                      break;
              case 5: printf("Enter 1st number: ");
                      scanf("%d", &num1);
                      ans = abs(num1);
                      printf("|%d| = |%d|\n",num1,ans);
                      break;
              case 6: printf("Enter 1st number: ");
                      scanf("%d",&num1);
                      if(num1 < 0){
                      printf("You cannot enter in a negative value!\n");
                      break; }
                      fans = sqrt(num1);
                      printf("sqrt(%d) = %d\n",num1,fans);
                      break;
              case 7: printf("Enter base: ");
                      scanf("%d", &num1);
                      printf("Enter exponent: ");
                      scanf("%d", &num2);
                      if((num1 == 0)&&(num2 <= 0)) {
                      printf("That is not an acceptable value!\n");
                      break; }
                      fans = pow(num1, num2);
                      printf("%d ^ %d = %d\n",num1,num2,fans);
                      break;
             default: printf("%d is not a valid selection\n", selection);
           }
           selection = menu();
        }
        printf("Bye!\n");
      }
    
    
      int menu(void) {
          int choice;
          printf("1 Add\n");
          printf("2 Subtract\n");
          printf("3 Multiply\n");
          printf("4 Divide\n");
          printf("5 Absolute Value\n");
          printf("6 Square Root\n");
          printf("7 Powers\n");
          printf("8 Quit\n");
          printf("Enter a Selection: ");
          scanf("%d", &choice);
          return choice;
      }
    Last edited by skaldicpoet9; 02-06-2008 at 08:01 PM.

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,165
    Perhaps you meant
    if (num1 == 0 || num2 <= 0)
    If num2 is equal to 0 OR num2 is less or equal to 0.
    Also suggest you try indenting a little bit better.

    Like this;
    Code:
                      if(num2 == 0) {
              printf("Zero is not an acceptable value!\n");
                      break; }
    The printf have a lesser indentation level than the preceding if, which makes it even more confusing. For every block, you should indent another level, so:

    Code:
    if (num2 == 0) {
        printf("Zero is not an acceptable value!\n");
        break;
    }
    So it should look something like that.
    Placing an ending bracket } at the same line as some other code is also not a good idea. It hurts readability.

    I could also suggest you try to learn some more from here:
    http://cpwiki.sf.net/User:Elysia/Indentation
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    "And" means "both". So your if-statement only triggers if both the base is zero and the exponent is nonpositive.

  4. #4
    Aia
    Aia is offline
    Beautiful to C Aia's Avatar
    Join Date
    Jun 2007
    Posts
    124
    if((num1 == 0)&&(num2 <=0)); /* will execute only when both expressions are true */

    if((num1 == 0) || (num2 <=0)); /* will execute when either expression is true */
    When the eagles are silent, the parrots begin to jabber. ~Winston Churchill

  5. #5
    Registered User skaldicpoet9's Avatar
    Join Date
    Dec 2007
    Posts
    15
    Hmm, when I try to compile it with the code if((num1 ==0) || (num <=0));
    it still doesn't display the error message when I type zero though, it only displays it after I type in the second number as a negative or zero.

  6. #6
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Get rid of the semicolon at the end of that if line!

  7. #7
    Registered User skaldicpoet9's Avatar
    Join Date
    Dec 2007
    Posts
    15
    Oops.

    That actually isn't like that in the code though...I just checked. I just typed that on the forum like that.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    C is a procedural language: that is, it does statements in the order in which you have written them. It will not jump ahead just because. So since the if-statement doesn't happen (in the code) until after you've input both numbers....

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,165
    Quote Originally Posted by skaldicpoet9 View Post
    Hmm, when I try to compile it with the code if((num1 ==0) || (num <=0));
    it still doesn't display the error message when I type zero though, it only displays it after I type in the second number as a negative or zero.
    I believe you want it to immediately print an error when you enter 0 for the first number?
    If yes, then recheck your code because that's not what it's doing. It's checking for valid input after you've entered both numbers.

    I'd also like to see you fix the indentation a bit.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  10. #10
    Registered User skaldicpoet9's Avatar
    Join Date
    Dec 2007
    Posts
    15
    Right on, that works. I knew it was something simple I was overlooking.

    Quote Originally Posted by Elysia View Post
    I'd also like to see you fix the indentation a bit.
    Oh, sorry about that, it must have got messed up when I pasted it.

  11. #11
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    Quote Originally Posted by skaldicpoet9 View Post
    it must have got messed up when I pasted it.
    To not mess up - use only spaces or only tabs for indentation. do not mix them.
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  12. #12
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,165
    I don't see any tabs in the original code, though, only spaces.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 09:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 11:01 AM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21