Arrays (Unlimited Input)

What I am trying to do, is allow the user to input any number of variables (for ex: 1 6 945 fhds)and my program will check for any strings. I have heard of arrays, but I feel like I can only limit how many inputs the user can have. Foo seems to need pre-input coded into the program? Can anybody clarify how to do this?

I tried lol:

#include <iostream>
using namespace std;

int main() {
int x;
int array[x];
cout << "Enter 5 numbers: ";
for (int x = 0; x = <char>; ++x) {
cin >> x;
}
cout << x;
return 0;
}
Arrays cannot be resized - once they are created they are stuck with that size forever.

You want to use a dynamically resizing container like std::vector - it will resize itself for you:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <vector>

int main()
{
    std::vector<int> numbers;
    int temp;
    while((std::cout << "Enter a number: ") && (std::cin >> temp))
    {
        numbers.push_back(temp); //numbers will resize itself automatically
    }
    //...
}
http://www.cplusplus.com/reference/vector/vector/
http://en.cppreference.com/w/cpp/container/vector

For more complex user input (e.g. disallowing strings), check out http://www.LB-Stuff.com/user-input
Last edited on
Topic archived. No new replies allowed.