Need help in putting data into an array in c++

In summary, Timo is asking for help with how to ask for input from the user in order to create an array. He is using a while loop to input values into the vector. The size() member function tells him how big the vector is.
  • #1
googled123
1
0
Hi guys,
I need to create an array in c++. But before I declare how many elements are in the array I want to prompt the user for input. I want to ask the user how many elements do they want in the array.
What ever the user said, 5 or 20. I then want to enter these 5 values or 20 values myself.
hope you can help.
 
Technology news on Phys.org
  • #2
Do you know what the new operator is, and how to use it with an array?
 
  • #3
You may want to consider using the c++ structure "vector" rather than an array. A vector v (of double values in this example) of size N is created by
Code:
  // ... determine the value of N here ...

  // create a vector of N double values.
  std::vector<double> v(N);
The usage is the same as for an array (except for extra options that you may find out some day).
 
  • #4
I'm guessing this is a class assignment, so you need to use an array instead of a vector. But for future reference...

Going further from Timo's example, you can declare the vector to have zero size initially, and then let it automatically expand as you add data to it. That way, you don't have to ask the user how many numbers he's going to enter.

Code:
#include <iostream>
#include <vector>

using namespace std;

int main ()
{
    vector<int> numbers;  // has length zero, initially

    cout << "Enter some numbers, terminating with control-D:" << endl;

    int n;
    while (cin >> n)
    {
        numbers.push_back(n);  // append n to the vector and expand
                               // the vector as necessary
    }

    // the size() member function tells you how big the vector is

    cout << "You entered:" << endl;
    for (int k = 0; k < numbers.size(); ++k)
    {
        cout << numbers[k] << endl;
    }

    return 0;
}

Input/output:

Code:
jtbell$ g++ vector_demo.cpp
jtbell$ ./a.out
Enter some numbers, terminating with control-D:
12
23
34
45
[I][COLOR="Blue"](control-D entered here)[/COLOR][/I]
You entered:
12
23
34
45
jtbell$
 
  • #5


Hi there,

Creating an array in C++ is a great way to store and manipulate data. In order to put data into an array, you will first need to declare the array and specify its size. This can be done using the syntax: "data_type array_name [array_size];"

In your case, since you want to prompt the user for the array size, you can use the "cin" function to get user input and store it in a variable. Then, you can use this variable as the size for your array.

Next, you can use a loop to ask the user for input for each element in the array. This can be done using the "cin" function again, and storing the values in the array using the index notation.

For example, if your array is called "myArray", the syntax for storing values in the array would be: "myArray[index] = value;"

I hope this helps you get started with creating an array and putting data into it in C++. If you need further assistance, don't hesitate to reach out. Best of luck with your project!
 

Related to Need help in putting data into an array in c++

1. How do I declare an array in C++?

To declare an array in C++, you can use the following syntax:
data_type array_name [array_size];
For example, to declare an array of integers with 10 elements, you would use:
int myArray[10];

2. How do I initialize an array in C++?

To initialize an array in C++, you can use the following syntax:
data_type array_name [array_size] = {value1, value2, ...};
For example, to initialize an array of integers with 10 elements, you would use:
int myArray[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

3. How do I access elements in an array in C++?

To access elements in an array in C++, you can use the following syntax:
array_name[index];
The index refers to the position of the element in the array, starting from 0. For example, to access the third element in an array named myArray, you would use:
myArray[2]; //since the third element is at index 2

4. How do I store user input in an array in C++?

To store user input in an array in C++, you can use a loop to iterate through the array and use the cin function to get input from the user. For example:
for (int i = 0; i < array_size; i++) {
   cin >> myArray[i];
}

This will store the user's input into each element of the array.

5. How do I manipulate data in an array in C++?

To manipulate data in an array in C++, you can use various functions and algorithms that are available in the C++ standard library. These include functions for sorting, searching, and modifying elements in an array. You can also use loops and conditional statements to perform specific actions on the elements of an array. Familiarizing yourself with the various functions and algorithms in the standard library can greatly help in manipulating data in an array.

Similar threads

  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
1
Views
953
  • Programming and Computer Science
Replies
25
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
31
Views
2K
  • Programming and Computer Science
Replies
20
Views
1K
  • Programming and Computer Science
Replies
17
Views
2K
  • Programming and Computer Science
Replies
11
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
11
Views
1K
Back
Top