Loops & variables

This is a discussion on Loops & variables within the C++ Programming forums, part of the General Programming Boards category; I'm writing a program which requires me to use a do while loop. The specifications of the program include 1. ...

  1. #1
    SLM
    SLM is offline
    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
    Katy, Texas
    Posts
    2,309
    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
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,630
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    SLM
    SLM is offline
    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
    21,630
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    Katy, Texas
    Posts
    2,309
    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
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,630
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    Katy, Texas
    Posts
    2,309
    Well, an array of chars would indeed be limiting. An array of strings would not.

    Todd
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

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, 03:46 PM
  4. simple question about variables and loops
    By InvariantLoop in forum C Programming
    Replies: 2
    Last Post: 01-26-2005, 08:47 AM
  5. hwnd and variables in them
    By underthesun in forum Windows Programming
    Replies: 6
    Last Post: 01-16-2005, 05:39 PM

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