Thread: Need help with floating point values

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    6

    Need help with floating point values

    I am fairly new at C++, taking a 10 wk college course on it. I am having to make a program that asks the user for a number between 5 and 10. then when the number is accepted, you are to take the floating value of the number entered, and ask for that many more numbers. The problem is, I am not sure how you would go about accepting that floating point value. This is what I have so far, but I am stuck from here. Anyone have any guidance? Sorry if I did something incorrectly, im a noob to this stuff. I am not asking anyone to do this for me, or code it up. Just point me in the righht direction and I can figure it out. Im just stumped at this point.

    Code:
    int main() {
       int num;
    
      do {
        cout << "Enter a value in the range of 5 to 10: ";
         cin >> num ;
          if (num < 5 || num > 10)
           cout << "The value entered is not in the proper range; please try again"
                << endl;
           cout << " " << endl;
       } while (num < 5 || num > 10);
      
      cout << "You entered " << num << endl;
    
    
      return 0;
     }
    Last edited by 69gto96z; 07-17-2008 at 07:48 PM.

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Are you sure you understand the assignment? How can you enter a floating point number of values? Unless they just want you to practice casting of sorts, this seems rather pointless.

  3. #3
    Registered User
    Join Date
    Jun 2008
    Posts
    6
    Here is the instructions.

    Write a C program that will prompt the user to enter a integer value in the range of 5 to 10 (inclusive). If the user doesn't enter a value in that range, you are to display an error message and prompt him again to enter a proper value. This sequence is to continue until he finally enters a value in the proper range. You can assume the user will enter a properly-formatted numeric value.


    This is where im at in the program


    After the user has entered a value in the range of 5 to 10, you are to accept from the user that number of floating point values (if the user specified seven values, for example, you are to then get seven values from the user, prompting him for each value).

    The program's output should begin (as always) with the course number, problem number, your name, and your email address.

    After getting the specified number of values, you are to display the following information about the values entered:

    1. The sum of the values
    2. The average of these values
    3. The largest value entered
    4. The smallest value entered

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    So, define a float variable, and cin >> into it, inside a loop that loops "num" times.
    Mainframe assembler programmer by trade. C coder when I can.

  5. #5
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Hmm, a few things to note:

    • This asks you to write a C program, however, you are writing your code in C++. Which language are you really working in?
    • The assignment is really asking you to read in an integer and then read as many floating point numbers as the integer represents. Just like how the assignment says, "if the user specified seven values, for example, you are to then get seven values from the user, prompting him for each value".
      • This means you need a loop to read the floating point data.
      • Since you know that there can only be a maximum of 10 floating point values, you are safe to use an array of size 10, however, if you wanted to be slightly more advanced, you could dynamically allocate a block of memory to hold just the right amount.

  6. #6
    Registered User
    Join Date
    Jun 2008
    Posts
    6
    Thanks for the replies guys, this program is probably not too difficult, bit its racking my brain. The class is on C++, im not sure why it says C in the instructions.

  7. #7
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Attack the problem in small areas. Ask specific questions when you get stuck. Consider that all you have to do now is write a loop to read in num number of floating point values. Then afterwards, you have to perform the calculations, but focus on reading them into an array for now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How accurate is the following...
    By emeyer in forum C Programming
    Replies: 22
    Last Post: 12-07-2005, 12:07 PM
  2. checking for floating point number
    By ssharish2005 in forum C Programming
    Replies: 6
    Last Post: 10-18-2005, 08:14 PM
  3. floating point question
    By Eric Cheong in forum C Programming
    Replies: 8
    Last Post: 09-10-2004, 10:48 PM
  4. Floating point numbers in a binary file
    By frenchfry164 in forum C++ Programming
    Replies: 6
    Last Post: 07-31-2003, 10:04 AM
  5. Replies: 2
    Last Post: 09-10-2001, 12:00 PM