if i were to create a program which required the user to enter multiple values, but i do not no how many. How would i go about anticipating the input. For instance if the user were permitted to input any amount of numbers and i wanted to add all the values, how on earth would i do this :(...Its easy when you restrict the amount as you say something like enter x amount of numbers, and then you can manipulate that x amount of numbers without any hassle.

any help much appreciated :)

Store them in a std::vector and use a while loop to get the input:

int temp;
while(cin >> temp)
{
   vec.push_back(temp);
}

and have the user push ctrl-z (or ctrl-d on *nix) when they are done.

ok thanks, but lets say the user entered ten numbers which would work out nicely using that algorithm you outlined, however how would i maniplaute the input, lets say i want to add all the ten numbers. This would be easy if i new the exact amount of inputs as i could store each input within a variable and total it(Var = input one + Input two), so how would this be accomplished if i were to use the method you showed me, not forgetting i don't know how many input they have done?

Use vec.size() to get the number of entries. Your vec is a vector<int> so your storage is already taken care of (if you're doing doubles or something else just change the type of vector).

ok i understand now, perhaps you could show me an example of how i would use the vec.size(), as i am unfamiliar with it. :)

Sure. Here's an example in a for loop (vec.size() returns an element of size_t, which is usually defined as an unsigned int, so we'll make the loop variable size_t as well).

So you've done your while loop and vec has N elements. vectors store this value within the data structure.

int sum = 0; //again change for double or whatever your values are
for (size_t i = 0;i<vec.size();i++) //vec.size() accesses that property = N
{
   sum+=vec[i];
}

You could use an iterator with the vector also but this is less involved.

Ok how would i add the value of each input to produce a total, vec.size just checks for how many elements there are but has nothing to do with the values of each does it. How would i obtain the value of each and sum all?

All there in lines 2-5 above. The ith element of the vector is accessed via vec[i] assuming your vector is named vec (so if you have declared vector vec<int>; earlier.

Ha, amazing thanks for the excellent help...Really aprrectiate it;)

Anyway this is what i got in the end:

#include <iostream>
#include <vector>

using namespace std;

 int main(){
	 vector<int>vec;
         int temp;
cout << "Press Ctrl Z to stop. "<< endl;
while(cin >> temp)
{
   vec.push_back(temp);
}
int sum = 0; //again change for double or whatever your values are
for (size_t i = 0;i<vec.size();i++) //vec.size() accesses that property = N
{
   sum+=vec[i];
}

cout <<sum; 

return 0; 
 }

Although not really sure what is going on within the for loop :(

Well, we could have calculated size before hand (if you knew it wasn't going to change and especially if you had an immense vector that would be advisable). If you had an array that had elements 10,20,30,40 and you had the same exact loop you could just say i<4, but the vector has some machinery behind the scenes that keeps track of each item that you add (not unlike a std::string having a length member and a way to access it). Tell me which part is troubling you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.