String arrays in different functinos.

I'm just trying to solicit input from the user and save it in the array in a separate function. I cant seem to get the syntax right, and dont quite know how to continue. could anyone point (haha) me in the right direction?

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
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
void getstring(string * array[3]);
int main()
{
	string * array[3] = {NULL};
    getstring(array);
    findmax();
    
   
}

void getstring(string * array[])
{
	cout<<"enter 3 strings"<<endl;
	
	
	for(int i= 0; i < 3; i++)
	{
	getline (cin, *array[i]);
	getline (cin, *array[i]);
	getline (cin, *array[i]);
	
    }

}


I'm getting a segmentation fault, core dumped, extied with code 139.
Last edited on
Do you want an array of 3 strings or an array of 3 pointers to string?

Also use code tags an post compiler errors
an array of three strings. I just thought i needed a pointer to point to the original value?
Yeah, arrays and pointers are confusing when you first approach them (and second approach, and third)

When you create an array you reserve a certain quantity of memory. This memory starts at the location pointed by array. So the original value (meaning the first value of the sequence) is pointed by array itself. To access the elements then you use the square brackets
sweet. I just cant seem to get the syntax correct. do you have any insight.
In the prototype for getstring() you don't need to write the size of the array
In main() you are defining an array of 3 elements which holds pointers to strings (string*). If you do
string array[3] you define an array of 3 elements which holds strings. Also, a std::string initialized to NULL has no meaning
The code for getstring() would have been correct if you had an array of pointers. Since you want strings you need to do just getline (cin, array[i]);
Last edited on
are you meaning something like this?
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>
#include <cstdlib>
using namespace std;
void getstring(string * array[]);
void findmax( );
int main()
{
	string array[3];
    getstring(*&array);
    findmax();
    
   
}

void getstring(string * array[])
{
	cout<<"enter 3 strings"<<endl;
	
	
	for(int i= 0; i < 3; i++)
	{
	getline (cin, array[i]);
	getline (cin, array[i]);
	getline (cin, array[i]);
	
    }


because then i get all kinds of errors....
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>
#include <cstdlib>
using namespace std;
void getstring(string array[]);
void findmax( );
int main()
{
	string array[3];
    getstring(array);
    findmax();
    
   
}

void getstring(string array[])
{
	cout<<"enter 3 strings"<<endl;
	
	
	for(int i= 0; i < 3; i++)
	{
	getline (cin, array[i]);
	getline (cin, array[i]);
	getline (cin, array[i]);
	
    }


Highlighted the parts you missed
Last edited on
Thank you, but while this works for my current program, I have more functions to implement, and I think i need a pointer to the arrays. Eventually, ill be comparing the length of them, but for now ive just used 'cout' to see them to make sure im writing the code right. in my 'getmax' function, i can only output the last string ive entered.


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
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
void getstring(string array[]);
void findmax(string array[] );
int main()
{
	string array[3];
    getstring(array);
    findmax(array);
    
   
}

void getstring(string  array[])
{
	cout<<"enter 3 strings"<<endl;
	

	for(int i= 0; i < 1; i++)
	{
	getline (cin, array[i]);
	getline (cin, array[i]);
	getline (cin, array[i]);
	
    }
    

}

void findmax(string array[])
{
	for(int i =0; i < 3; i++)
	{
	cout<<array[i]<<endl;
}
	
}

//Pass the array as a string *&. You do not need local strings. Dereference by *arr[0]. Specifically, getline(*arr[i]), for for i = 0 to 2... 
in my 'getmax' function, i can only output the last string ive entered.

Right, I overlooked that one. In getstring() your loop executes only once. This means that what happens is
1
2
3
4
5
6
for(int i= 0; i < 1; i++)
{
	getline (cin, array[0]);
	getline (cin, array[0]);
	getline (cin, array[0]);
}

To fix you can either call a getline with a different array element each time withouth the loop
1
2
3
	getline (cin, array[0]);
	getline (cin, array[1]);
	getline (cin, array[2]);

or you can leave the loop and do
1
2
for(int i= 0; i < 3; i++)
	getline (cin, array[i]);
Last edited on
Ha. I should Have seen that...I'm still stuck on the pointer thing...but this is farther then ive gotten for a week.
Topic archived. No new replies allowed.