input value as a function parameter ?

if you want the user to input many values to be put in char array, but some of these values maybe short and some maybe long so it won't be great idea to declare array of a big size like: char arr[100]; it would be better to make array like this arr[] and make the user input the values directly to it in a loop so that arr[] size will be changeable according to length of input, this is to be done using a function with a prototype like : void input(char arr[]); the question: is there a way to make the user input the value directly as a function a parameter ?
EDIT : each time the user inputs a value the char array will consist only of this value
Last edited on
Array of variable size in C++ is called vector. If you need a string, there is a string in C++ as well.

Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <vector>

std::vector<int> input()
{
    std::cout << "Enter numbers.\nEnter negative number to terminate.\n";
    std::vector<int> results;
    int temp;
    while(std::cin >> temp) {
        if(temp < 0) break;
        results.push_back(temp);
    }
    return results;
}

int main()
{
    std::vector<int> numbers = input();
    for(int i: numbers)
        std::cout << i << '\n';
}
i didn't use string at first because i was dealing with numbers but i found suitable conversion now, thanks.
(with char array i was using atof, so i said to myself why wouldn't be there stof :D)
for array, there is one line
int * q = new int [n];

whatever type you want, This allocates the array for us dynamically, at run-time. just remember to delete it by using
delete [] q;
Topic archived. No new replies allowed.