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.
#include <iostream>
#include <string>
#include <cstring>
usingnamespace std;
int main()
{
constint Size = 5;
char* name[Size];
string str;
for (int i = 0; i < Size; i++)
{
getline(cin, str);
name[i] = newchar[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;
}
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>
usingnamespace 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.
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.
#include<iostream>
#include<string>
usingnamespace std;
int main()
{
constexprint 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;
}
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()
{
constint 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' ;
}