Arrays

Good Day!!!

I am really wondering if we can reinitialize the array size during run time.

and also,

Please help about this test code. Why I am getting an error message.

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
#include <cstdlib>
#include <iostream>
#include <string>
#include <sstream>
#include <new>

using namespace std;


int main(int argc, char *argv[])
{
	int numName;
	int ctr;
	string * str;

	cout << "How many names you want to enter: ";
	cin >> numName;

	str = new (nothrow) int[numName];


	for(ctr = 0; ctr < numName; ctr++)
	{
		cout << "Name number " << ctr << ": ";
		cin >> str[ctr];
	
	}

	system("pause");
	return 0;
}


Thanks in advance.. :)
You have declared str as string and using int for allocation.

You should use string[numName]

I dont think that you can change array size during run-time .

But you can check out realloc .
http://www.cplusplus.com/reference/clibrary/cstdlib/realloc/
Last edited on
Thank you for everything adesh....and also the link..it will help me a lot in using C++. I am actually doing some test programs like Phone Book. Thanks a lot once again.... :)
Topic archived. No new replies allowed.