read strings from array --help

1
2
3
4
5
6
7
8
9
	char* arr[5];
	int i = 0;
	while (i++ < 5) {
		cout << "type " << i << ". string: ";
		cin >> input;
		arr[i] = input;
	}
	for(int i = 0; i < 5; ++i)
		cout << arr[i];


this piece of code makes me crazy, it does not alow me to read iputs from arr on last line?
what is wrong?
char pointers are not strings. They're pointers.

arr[i] = input;

Here, arr[i] (that is, all 5 elements of your arr array) will point to the same string that input points to.

Therefore when you print them:

1
2
for(int i = 0; i < 5; ++i)
    cout << arr[i];


This is like printing 'input' 5 times in a row. Each time it will print the same thing.

EDIT:

To clarify, you only have 1 string here: 'input'. Your arr array basically just acts as an alias to access input via different pointers. Each time you get a new string from the user, you're overwriting your previous string.

Of course... this code is very bad if input is also just a pointer and not an actual buffer. In which case you are corrupting the heap. How is input declared?


EDIT 2:

The easy solution is to forget about char arrays and just use strings:

1
2
3
4
5
6
7
8
9
10
	// make sure 'input' is also a string
	string arr[5]; // make these strings, not pointers...
	int i = 0;
	while (i++ < 5) {
		cout << "type " << i << ". string: ";
		cin >> input;
		arr[i] = input;  // now this will work the way you want
	}
	for(int i = 0; i < 5; ++i)
		cout << arr[i];
Last edited on
I've forgot to include input declaration in my post .. it was so
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;

int main() {
    char input[40];
    char* arr[5];
	int i = 0;
	while (i++ < 5) {
		cout << "type " << i << ". string: ";
		cin >> input;
		arr[i] = input;
	}
	for(int i = 0; i < 5; ++i)
		cout << arr[i];
	return 0;
}


however I'm still geting error and nothing is printed :/

I've tryed with strcpy function but error as well...

1
2
3
4
5
6
7
8
9
10
    char input[40];
    char* arr[5];
	int i = 0;
	while (i++ < 5) {
		cout << "type " << i << ". string: ";
		cin >> input;
		strcpy(arr[i], input);
	}
	for(int i = 0; i < 5; ++i)
		cout << arr[i];


I know it's easy to use strings instead but that will not teach me a lession lol
how do I acctualy release this code?

EDIT:
thank you for clarification...
I've tryed like so:
1
2
3
4
5
6
7
8
9
10
11
12
13
    char input[40];
    char* arr[5];
	int i = 0;
	while (i < 5) {
		cout << "type " << i << ". string: ";
		cin >> input;
		arr[i] = new char[strlen(input) + 1];
		strcpy(arr[i++], input);
	}
	for(int i = 0; i < 5; ++i)
		cout << arr[i] << endl;
	for(int i = 0; i < 5; ++i)
		delete arr[i];

now is OK
TNX again!!
Last edited on
You are leaking memory there.

You are creating arr[i] with new[], but you are using singular delete. You need to use delete[]:

1
2
	for(int i = 0; i < 5; ++i)
		delete[] arr[i];  // <-  delete[], not delete 


After that you're good.

Although I'm wondering why you are using a while loop instead of a for loop. You're using a for loop everywhere else, why not for the main loop?
delete[] arr[i];

Oh thanks! I've forgot that lol

. You're using a for loop everywhere else, why not for the main loop?

I've beign with while loop cos so was better is prevous version of "program" after that it left so...

tnx cheers!
Topic archived. No new replies allowed.