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

  • Context: C/C++ 
  • Thread starter Thread starter googled123
  • Start date Start date
  • Tags Tags
    Array C++ Data
Click For Summary

Discussion Overview

The discussion revolves around how to create and manage arrays in C++, particularly focusing on user input for the size of the array and the subsequent values to be stored. The conversation includes considerations for using arrays versus vectors, as well as practical coding examples.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Homework-related

Main Points Raised

  • One participant seeks guidance on prompting the user for the number of elements in an array before declaring it.
  • Another participant inquires about the use of the new operator in relation to arrays.
  • A suggestion is made to use C++ vectors instead of arrays, highlighting the ease of resizing and managing elements dynamically.
  • Further elaboration on vectors includes a code example demonstrating how to declare a vector with zero size and allow it to expand as values are added.

Areas of Agreement / Disagreement

There is no explicit consensus on whether to use arrays or vectors, as some participants advocate for the use of arrays due to potential assignment constraints, while others promote vectors for their flexibility.

Contextual Notes

Participants do not address the limitations or assumptions regarding the use of arrays versus vectors, nor do they clarify the context of the assignment or the specific requirements that might influence the choice of data structure.

googled123
Messages
1
Reaction score
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
Do you know what the new operator is, and how to use it with an array?
 
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).
 
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](control-D entered here)[/I]
You entered:
12
23
34
45
jtbell$
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 25 ·
Replies
25
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 31 ·
2
Replies
31
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
20
Views
2K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 17 ·
Replies
17
Views
4K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K