Like Tree1Likes
  • 1 Post By grumpy

Asks to input number nad if outside number asks to put in number within bounds

This is a discussion on Asks to input number nad if outside number asks to put in number within bounds within the C Programming forums, part of the General Programming Boards category; Hi all, I have to write a program that will ask you to put in a number between 0 and ...

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    5

    Asks to input number nad if outside number asks to put in number within bounds

    Hi all,
    I have to write a program that will ask you to put in a number between 0 and 9 and multiply it by pi. If the number put in is between 0 and 9 then pi is multiplied but if it isnt between 0 or 9, it will say the number is not between 0 and 9 and asks you to put it in again and will repeat until a number between 0 and 9 is put in.
    I have got the program working to the extent that it the number is between 0 and 9 it will multiply it by pi but if its not between 0 and 9 it will say the number is not between 0 and 9 and ask to put in a new number.
    I can't work out how to get the program to repeat itself if the number entered isnt between 0 and 9.
    I have enclosed the code below, can anyone help me with this?
    Attached Files Attached Files

  2. #2
    Registered User
    Join Date
    Feb 2013
    Location
    Buea Cameroon
    Posts
    64
    I think this will work.
    keep up on the indentation gook work!
    Code:
    //S_Howlin 20/3/13 
    // Revision  
    // number between 0 and 9 by pi and check if value entered is between 0 and 9 and asks for new value if outside of 0 and 9 
     
     
    #include <stdio.h> 
    #include <math.h> 
    #include <conio.h> 
    
     
    int main () 
     
    { 
        int x; 
        double y; 
        for(;;){             //This loop ensures that the program keeps asking for a number    
        printf ("Please enter a value between 0 and 9 (Enter a number less than 0 to terminate):  " ); // Asks to put in a value between 0 and 9 and a value less thatn 0 to terminate 
        scanf ( " %d", &x);
        if(x<0){break;} // checks if the value entered is less than zero 
        if ( x > 0.0 && x < 9.0 ) // if the value is between 0 and 9 the following will be said: 
        { 
             printf( "The value entered is between 0 and 9 \n"); 
             y = x * M_PI;  // works out value for y by multiplying x with pi 
             printf ( "The value for y is: %0.2f\n", y); // Works out value for y to 2 decimel places     
        } 
        else //If the value is not between 0 and 9 the follwoing will be siad: 
        { 
             printf( "The value you entered is NOT between 0 and 9. \n"); 
        } 
        } 
         
         
         
        getch (); 
        return 0; 
    }

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    333
    You need to look up do ... whike loops. You ask for the number, then while it is bad, go back and ask again.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    http://www.malcolmmclean.site11.com/www

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,433
    acho.arnold, it is a good idea to avoid doing people's homework for them as this site has a homework policy here.

    In this case, it makes no difference as your solution does not meet requirements of the exercise anyway. And your code uses a few things that are not standard C (<conio.h>, getch(), M_PI).

    As Malcolm suggested, a do/while loop is a more suitable solution.
    stahta01 likes this.
    Right 98% of the time, and don't care about the other 3%.

  5. #5
    Registered User
    Join Date
    Mar 2012
    Posts
    5
    I am not looking for someone to do it just for help, a guide on what way to go and do it.
    I use the getch() as my lecturer uses bloodshed on lynix and the system ("pause") doesn't work on it.

    Is using a do/while loop more suitable than an if/else loop?

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,433
    Quote Originally Posted by howlin View Post
    Is using a do/while loop more suitable than an if/else loop?
    A do/while loop is a valid code construct in C. There is no such thing as an "if/else loop".

    Practically, a valid code construct is usually considered more suitable than a non-existent one.
    Right 98% of the time, and don't care about the other 3%.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Random Number Bounds
    By deeisenberg in forum C Programming
    Replies: 1
    Last Post: 03-05-2012, 11:11 AM
  2. Replies: 8
    Last Post: 12-30-2010, 09:08 PM
  3. Replies: 5
    Last Post: 10-05-2009, 10:21 AM
  4. Input a Hex number and output hex number to a text field
    By zoobaby in forum C++ Programming
    Replies: 4
    Last Post: 05-12-2009, 11:26 AM
  5. Allocation of major number and minor number linux
    By vinayharitsakp@ in forum Linux Programming
    Replies: 4
    Last Post: 12-08-2007, 01:01 PM

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