Input question

This is a discussion on Input question within the C++ Programming forums, part of the General Programming Boards category; How can I input an unknown number of numbers and have eachone declared as some sore of int ? Is ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    27

    Input question

    How can I input an unknown number of numbers and have eachone declared as some sore of int ? Is this even possible?

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    use a loop, type a number, then store the number in either an array or std::vector. The vector would be best because you don't have to know how may items it will hold, it will manage all the memory requirements for you. If you use an int array, such as int*, you have to learn how to do that yourself.

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    27
    Quote Originally Posted by Ancient Dragon
    use a loop, type a number, then store the number in either an array or std::vector. The vector would be best because you don't have to know how may items it will hold, it will manage all the memory requirements for you. If you use an int array, such as int*, you have to learn how to do that yourself.
    I'm trying to write a program that will add in an unknown series of numbers then divide it by the number of numbers to get an average. Can I use an array for that?

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,062
    It would be a pain because arrays are fixed size. You could dynamically allocate an array and reallocate everytime you get a new number, but that would just be a waste of time and CPU. As Ancient said, you're easiest choice would be a std::vector.
    Sent from my iPadŽ

  5. #5
    Registered User
    Join Date
    Jan 2003
    Posts
    118
    If you use an array you would have to either have a max amount of numbers, or learn to use dynamic memory. You could also use an std::vector<int> and not have to worry about memory, as the other poster said.
    Why drink and drive when you can smoke and fly?

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    You don't have to store the numbers that have been read in. Just keep a variable that stores the sum and one that stores the count. Each time through your loop you can read in a variable, add it to the sum, and increment the count. When you exit the loop your sum and count will be correct.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie Question: File Input and Relative Paths
    By Ashes999 in forum C++ Programming
    Replies: 11
    Last Post: 05-23-2003, 04:21 AM
  2. Replies: 2
    Last Post: 05-12-2003, 04:40 PM
  3. quick question: File Input
    By meltingdude in forum C Programming
    Replies: 1
    Last Post: 04-08-2003, 02:02 AM
  4. Replies: 2
    Last Post: 03-15-2002, 12:45 PM
  5. Handling input errors, general question
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 11-08-2001, 05:21 PM

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