dynamically allocate array

someone please help me so I want the user to be able to enter as many numbers they wish. so this is currently giving me error. how can I get the user to enter the numbers in the array?


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

using namespace std;

int main() 
{
     
	 int size = 0;
	 
	 int* arr;
	 do{
	 	int* newArray = new int [++size];
	 	for (int j=0; j < size-1; ++j)
                newArray[j]=arr[j];
        delete[] arr;
        arr = newArray;
        cin >> arr[size-1];
	 } while (arr[size-1] >= 0);
	 
}
Last edited on
and I want it to print back the array the user has entered.
Still new to coding myself but I can help you with your issue.
My advice to you is to read your book that was given by your instructor. It really helps to read the book after a lecture. One night you should just focus mainly on the chapter explaining memory allocation to give you a better understanding of it.


Here is the code you need but I am not sure how you wanted it done. I made it into functions because it made it easier for me instead of writing it all inside in main(). You can easily convert the code into in main() if you need to.

Code is working for me but if it doesn't work for you. please post the issue.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
//read the comments
#include <iostream>
using namespace std;


void displayValue(int[], int);
void setValue(int[], int);



int main()
{
	int size; // I just created two simple variable's because that is all I will be using for my functions. 
	int* value; // This one will be the variable creating the memory allocation.


	cout << "How big do you want the array to be? ";
	cin >> size; //I gave this variable a value. Which will give my array its size and how many times I will be looping inside my function.


	value = new int [size]; // Creating a pointer here. (ie. memory allocation).

	setValue(value, size); // These two are my functions, again not sure how you wanted your code but you can easily convert everything into int main().
	displayValue(value, size);

	return 0;
}

void setValue(int v[], int s)
{
	//Now like I said this can be easy to move everything into int main(). Since I know you're a bit knowledgable in C++.
	//but this function is what gives the values inside the array.
	cout << "Enter the values inside the array: " << endl;
	for (int count = 0; count < s; count++)
	{
	cout << "Value " << (count +1) << ":"; 
	cin >> v[count];
	}
}

void displayValue(int v[], int s)
{
	//This simply just displays the values that are inside the array.
	for (int count = 0; count < s; count++)
	{
	cout << (count + 1 ) << " value entered was: " << v[count] << endl;
	}
}
Last edited on
thanks so much , I had another question:
so output looks like:
Enter the values inside the array:
Value 1: 1
Value 2: 2
1 values entered were: 1
2 values entered were: 2

How can I make it look like:
Enter the values inside the array:
Values : 1 2
values entered were: 1 2
Last edited on
When entering the values I think its impossible to do because its a cin >>. You always need to push "enter" on the keyboard. Not sure though, I might be super wrong but I was able to do what you requested when its displaying the values.


Just replace these two things


1
2
3
4
5
6
7
// belongs in setvalue function
cout << "Enter the values inside the array: " << endl;
	for (int count = 0; count < s; count++)
	{
	cout << "Value " << (count +1) << ":"; 
	cin >> v[count];
	}



1
2
3
4
5
//this belongs inside in main(). where the functions are at.
        setValue(value, size);
	cout << "\nValue's entered were: ";
	displayValue(value, size); 
        cout << "\n";




Last edited on
1
2
3
4
5
6
void setValue(int v[], int s)
{
	cout << "Enter the values inside the array:\nValues:" << endl;
	for (int count = 0; count < s; count++)
	        cin >> v[count];
}

You can type all the values and then hit enter, and the program will read all the input it can before stopping again (if you didn't enter enough).

1
2
3
4
5
6
7
8
9
void displayValue(int v[], int s)
{
        cout << "values entered were: ";
	for (int count = 0; count < s; count++)
	{
	        cout << v[count] << ' ';
	}
        cout << endl;
}


When you use dynamic allocation always remeber to delete the memory before going out of scope.
Last edited on
Whoops, maeriden is correct always delete when you are using memory allocation. I was sure I had wrote it but I guess I missed it.
Thanks


Copy and paste maeridan's code. It will do what you requested
Last edited on
so I would put:
delete [] value;
is this correct and where would I put this?
That is correct, and always delete any memory allocation where the code finishes.
In this case, right after the functions are called
Last edited on
You have to free memory before the pointers go out of scope. If you're not using classes this means before the function where you defined them ends.
I also had another question like if I don't want to ask the user for the size but rather let the user enter it is that possible? For example they will enter:5 3 8 2 6 8
Last edited on
nvm. Misunderstood the problem.
Last edited on
If you mean creating an array based on how many values the user enters it is possible, but not really simple with plain arrays.
std::vector would be a better choice for that, but you'd have to find a way to signal the program for the end of the sequence, like "enter a negative number to stop" or "enter a non-numeric character to stop".
I mean how do I let the user enter the size of the array followed by the array like: 5 2 3 4 1 8 or 4 2 1 3 6.
Topic archived. No new replies allowed.