Thread: Loops & variables

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    2

    Loops & variables

    I'm writing a program which requires me to use a do while loop.

    The specifications of the program include

    1. Allow the user to enter any numbers and when the user presses zero to follow the following information..

    The numbers that the user has entered (not including 0)
    The average of the numbers (not including 0)
    The smallest number entered (not including 0)
    The largest number entered (not including 0)
    The difference between the largest and the smallest number.

    The problem that I am having is how do I get the variable to store more than one number???

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    At a maximum,

    You'll need an array of variables for the values the user entered.
    You'll use another variable for the accumulator
    You'll use another for the number of variables you process
    You'll use another for the min
    You'll use another for the max
    You'll need another for the delta between the min and max.

    Todd
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The problem that I am having is how do I get the variable to store more than one number???
    Use an array, or rather, a std::vector since you need a dynamic array.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Mar 2008
    Posts
    2
    We haven't learned std or vectors

    we have learned character arrays would I be able to use that?

    I do not have my written program at hand but I use a for loop as a counter so when I got the average I was able to divide it
    Last edited by SLM; 03-20-2008 at 02:21 PM.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    we have learned character arrays would I be able to use that?
    No, you need an array of int, not char. Also, since you only know about arrays, your loop must check that the user does not enter so many numbers before entering 0 that it goes past the bounds of your array.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Yes, of course you can use an array of chars. You'll just have to convert them to ints each time you need a value. More work, but certainly a valid approach.

    Todd
    Mainframe assembler programmer by trade. C coder when I can.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yes, of course you can use an array of chars. You'll just have to convert them to ints each time you need a value. More work, but certainly a valid approach.
    True, but that would limit the range of each input entered to the range of a char, and I suspect that that is an unnecessary limitation here.

    EDIT:
    Besides, by "array of characters" SLM is probably thinking of null terminated C-style strings.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Well, an array of chars would indeed be limiting. An array of strings would not.

    Todd
    Mainframe assembler programmer by trade. C coder when I can.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. declaring variables inside loops
    By samus250 in forum C Programming
    Replies: 21
    Last Post: 04-30-2008, 01:51 PM
  2. Remotely Creating Variables
    By Rajin in forum C++ Programming
    Replies: 1
    Last Post: 04-26-2005, 11:20 PM
  3. Declaring an variable number of variables
    By Decrypt in forum C++ Programming
    Replies: 8
    Last Post: 02-27-2005, 04:46 PM
  4. simple question about variables and loops
    By InvariantLoop in forum C Programming
    Replies: 2
    Last Post: 01-26-2005, 09:47 AM
  5. hwnd and variables in them
    By underthesun in forum Windows Programming
    Replies: 6
    Last Post: 01-16-2005, 06:39 PM