Get string and save in array of pointers in C++

Hello,

I would like to get 10 string from the input and save in array of pointers.

Like this:

https://i.ibb.co/hMySjRZ/Untitled.jpg
Sounds like a plan.
What's stopping you?
I wrote this but I have an error:

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
#include<iostream>
#include<string>
using namespace std;

int main()
{
	char* name[5];

	string myString;

	for (int i = 0; i < 5; i++)
	{
		getline(cin, myString);
		int n = myString.length();

		strcpy_s(name[i], n, myString.c_str());

		for (int i = 0; i < n; i++)
		{
			cout << name[i];

		}
		cout << endl;
	}

	return 0;
}
Last edited on
You have an array of 5 pointers, but they don't point to anything.
You need to dynamically allocate the memory for the c-style string before you copy the characters.
You should also wait to print out the strings until after you've read them all in.

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
#include <iostream>
#include <string>
#include <cstring>

using namespace std;

int main()
{
    const int Size = 5;
    
    char* name[Size];
    string str;

    for (int i = 0; i < Size; i++)
    {
        getline(cin, str);
        name[i] = new char[str.size() + 1]; // allocate memory (+1 for '\0')
        strcpy(name[i], str.c_str()); // you can use strcpy with c_str() as it is guaranteed to be '\0'-terminated
    }

    // Print the strings.
    for (int i = 0; i < Size; i++)
        cout << name[i] << '\n';

    // You should delete the allocated memory afterwards.
    for (int i = 0; i < Size; i++)
        delete[] name[i];

    return 0;
}

Thank you,

Could you please check the below code?
Why are all five strings the same?

For example:
Book
Hello
World
USA
Earth

In output I have
Earth
Earth
Earth
Earth
Earth

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

#include<iostream>
#include<string>
using namespace std;

int main()
{
	string* name[5];

	string myString;

	for (int i = 0; i < 5; i++)
	{
		getline(cin,myString);
		name[i] = &myString;
	}

	for (int i = 0; i < 5; i++)
	{
		cout << *name[i] << endl;
	}


	return 0;
}

It's printing the last string over and over since you stored the address of myString in every location of name, and that's what myString contains after the loop. If you want to store strings, then store strings, not pointers to a single string.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <string>
using namespace std;

int main()
{
    string name[5];
    string myString;

    for (int i = 0; i < 5; i++)
    {
        getline(cin, myString);
        name[i] = myString;
    }

    for (int i = 0; i < 5; i++)
        cout << name[i] << endl;
}

If you really want to store pointers to strings, then you need to dynamically allocate them so the pointers have something to point at. And you should also remember to delete them.

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

int main()
{
    string* name[5];
    string myString;

    for (int i = 0; i < 5; i++)
    {
        getline(cin, myString);
        name[i] = new string(myString);
    }

    for (int i = 0; i < 5; i++)
        cout << *name[i] << endl;

    for (int i = 0; i < 5; i++)
        delete name[i];
}

Last edited on
Hello Shervan360,

To go along with what dutch said.

Line 10 defines the variable "myString" which has 1 address pointing to the beginning of the string. This address is stored on the stack even if part or all the string is stored on the heap.

Line 15 stores the same address in the array because there is only 1 address to work with.

Line 14 will allow you to enter a new string, but it will over wright "myString" each time through the loop leaving "myString" with the value of the last entry.

So your last for loop has 5 elements all with the same address and your last value for "myString" is "Earth", so it is printed 5 times.

Consider this with out using pointers:
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
#include<iostream>
#include<string>

using namespace std;

int main()
{
    constexpr int MAXSIZE{ 5 };

    std::string name[MAXSIZE];
    string myString;
        
    for (int i = 0; i < MAXSIZE; i++)
    {
        // <--- Needs a prompt or written directions of what to do. Or source code for those who know C++.
        getline(cin, myString);

        name[i] = myString;

        //int n = myString.length();

        //strcpy_s(name[i], n, myString.c_str());
    }

    std::cout << '\n';

    for (int i = 0; i < MAXSIZE; i++)
    {
        cout << name[i] << '\n';

    }

    cout << '\n';

    return 0;
}


Andy
If there must be an array of pointers, we can do this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>

int main()
{
    const int N = 5 ;

    std::string strings[N] ; // an array of strings
    for( std::string& str : strings ) std::getline( std::cin, str ) ;

    std::string* pointers[N] {} ; // array of pointers
    // make these pointers point to corresponding strings in the array of strings
    for( int i = 0 ; i < N ; ++i ) pointers[i] = strings + i ;

    for( const std::string* p : pointers ) std::cout << *p << '\n' ;
}

Topic archived. No new replies allowed.