Thread: Stop if less than 0

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    28

    Stop if less than 0

    Hi everybody! I am new to C and I have a quick question. I am writing a program that calculates the area of a circle.The program is asking to enter diameter and then it calculates the area radius and etc.
    How could I make stop the program if less than 0 is entered for the diameter?
    This is not a homework, just learning on my own....
    Thank you!

  2. #2
    Registered User
    Join Date
    Jun 2006
    Posts
    22
    if(diameter==0)
    goto again;
    again: printf("input an invalid number\n");
    printf("diameter: ");
    scanf("%d",&diameter);

  3. #3
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Quote Originally Posted by mycount
    if(diameter==0)
    goto again;
    again: printf("input an invalid number\n");
    printf("diameter: ");
    scanf("%d",&diameter);
    Very funny...

    How could I make stop the program if less than 0 is entered for the diameter?
    If, as you said you want to stop the program, then a simple condition with a return is fine as long as you're in main.
    Code:
    if (inputValue < 0) {
       fprintf(stderr, "Your input should be greater than or equal to 0.");
       return 1;
    }
    This is probably the best option if you're running your program from a command line interface. If you're in a Windowed Interface and you're opening your program in the standard command window that your OS uses, then you're better off looping until you get an appropriate value.
    Code:
    while (inputValue < 0) {
       fprintf(stderr, "Your input should be greater than or equal to 0.\nInput again:");
       scanf("%d", &inputValue);
    }
    If you're in a function, not main, then you could return the negative value and do a comparison with the returned value in the same manner I showed above.
    Sent from my iPadŽ

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    28
    Thank you for your help...
    I am just not very familiar with "while". I am getting an error when inserting while statement....
    This is what I have so far:
    Code:
    #include<stdio.h>
    #include<math.h>
    #define PI 3.1416 
    int main(void)
    {        
           float radius, diamater, circumference;
                    
           printf("Please enter the radius of your circle: ");
           scanf("%f",&radius);
                                            
           diamater = radius*2;
           circumference =PI*diamater; 
           area=(PI*radius*radius);
           
           printf("The diameter is: %0.2f\n",diamater);
           printf("the circumference is: %0.2f\n",circumference);
           printf("the area is: %0.2f\n",area);
           
        
           return 0;
    
    }
    My question is how where can I place while statement?
    Thank you for your help

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    The loop would go after your first scanf, the only difference being inputValue would change to radius and "%d" should be "%f". The basic idea is, you take your initial value and then you loop while that value is invalid. When it is valid, it breaks out of the loop and continues the program.
    Last edited by SlyMaelstrom; 10-19-2006 at 01:14 AM.
    Sent from my iPadŽ

  6. #6
    Registered User
    Join Date
    Jun 2006
    Posts
    22
    But,whereve are you from?
    Quote Originally Posted by Newbie999
    Thank you for your help...
    I am just not very familiar with "while". I am getting an error when inserting while statement....
    This is what I have so far:
    Code:
    #include<stdio.h>
    #include<math.h>
    #define PI 3.1416 
    int main(void)
    {        
           float radius, diamater, circumference;
                    
           printf("Please enter the radius of your circle: ");
           scanf("%f",&radius);
           while(radius==0)
           {
                  printf("An invalid radius.\n");
                  printf("radius: ");
                  scanf("%f",radius);
           }                                  
           diamater = radius*2;
           circumference =PI*diamater; 
           area=(PI*radius*radius);
           
           printf("The diameter is: %0.2f\n",diamater);
           printf("the circumference is: %0.2f\n",circumference);
           printf("the area is: %0.2f\n",area);
           
        
           return 0;
    
    }
    My question is how where can I place while statement?
    Thank you for your help

  7. #7
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    mycount, what is the less-than operator? I have to ask you...
    Sent from my iPadŽ

  8. #8
    Registered User
    Join Date
    Oct 2006
    Posts
    28
    Thank you so much! You're awesome!
    I got it working.

  9. #9
    Registered User
    Join Date
    Oct 2006
    Posts
    28
    SlyMaelstrom...I have one more questions related to a different problem. How could I write a program that converts decimal ASCII codes 4 from 100 (just an example) to their correspondent character constant by using a loop. I would like to use a loop in case the wrong number is entered.
    Also how can I end a program by entering the number 1?
    I started something,but is giving me some errors....
    Thank you in advance!

  10. #10
    Registered User
    Join Date
    Jun 2006
    Posts
    22
    Quote Originally Posted by SlyMaelstrom
    mycount, what is the less-than operator? I have to ask you...
    replace with "<"

    So funny............

  11. #11
    Registered User
    Join Date
    Jun 2006
    Posts
    22
    Quote Originally Posted by Newbie999
    SlyMaelstrom...I have one more questions related to a different problem. How could I write a program that converts decimal ASCII codes 4 from 100 (just an example) to their correspondent character constant by using a loop. I would like to use a loop in case the wrong number is entered.
    Also how can I end a program by entering the number 1?
    I started something,but is giving me some errors....
    Thank you in advance!
    As the word shown,A poke.

  12. #12
    Registered User
    Join Date
    Oct 2006
    Posts
    28
    mycount, if you can't give me an advice please don't make fun!
    I would appreciate it.

  13. #13
    Registered User
    Join Date
    Jun 2006
    Posts
    22
    Yellow big.

  14. #14
    Cat Lover
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    109
    Quote Originally Posted by Newbie999
    SlyMaelstrom...I have one more questions related to a different problem. How could I write a program that converts decimal ASCII codes 4 from 100 (just an example) to their correspondent character constant by using a loop. I would like to use a loop in case the wrong number is entered.
    Also how can I end a program by entering the number 1?
    I started something,but is giving me some errors....
    Thank you in advance!
    Ending if someone enters 1:
    You can end a program by calling exit(0), or return 0 if you're in main.
    You can test if someone's input is 1 by something like if(input == 1)

    To convert a decimal value into its corresponding ascii code char c = n works. If n == 57 c == '9'.
    Last edited by Dweia; 10-19-2006 at 01:44 AM.

  15. #15
    Registered User
    Join Date
    Sep 2006
    Posts
    15
    In order to convert decimal ASCII code to their correspondent character, you can use special character:

    \ddd
    ddd is the ASCII code in octal number format (base-8 number)

    To print it, simply use printf statement. For instance:
    Code:
    printf("\43 \n");
    This statement will show you the correspondent character of the ASCII code whose number is in base-8 number format. The result is a '#' symbol for ASCII code number 43 (octal)
    Note that: 43 in octal equals to 35 in decimal number.
    For reference, you can refer to this link: http://www.iu.hio.no/~mark/CTutorial/CTutorial.html

    To show a sequence of ASCII character from certain range, you can use for loop. Then, to show characters from ASCII code, you can actually use %c specifier. So, just mention the range of integer value in the for loop. And then you show the value of that variable using printf. BUT: instead of using %d, use %c to show your integer variable. The integer value is a decimal number.

    To end a program by entering 1, you can use do-while loop. Put do-while loop with the condition that it will loop if the entered value is not equal to 1. Before that ask the user to enter a value using scanf (tell the user that the program will be terminated if he/she enter 1). This scanf should be inside the do-while loop. Hence, the condition should evaluate the value of the variable that is entered with the previous scanf.

    TRY IT!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fscanf %s or %d integer input space char stop question...
    By transgalactic2 in forum C Programming
    Replies: 5
    Last Post: 04-14-2009, 10:44 AM
  2. Error stop Http Listener
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 06-04-2008, 02:14 AM
  3. how do I stop text from being erased
    By rakan in forum Windows Programming
    Replies: 2
    Last Post: 02-20-2008, 12:00 AM
  4. when a while loop will stop ?
    By blue_gene in forum C Programming
    Replies: 13
    Last Post: 04-20-2004, 03:45 PM
  5. Telling other applications to stop
    By nickname_changed in forum Windows Programming
    Replies: 11
    Last Post: 09-25-2003, 12:47 AM