Writing a Three Function Program with String Array -- Crashing

This is my third attempt rewriting it in hoping to avoid whatever's making the code crash. My book doesn't have any similar problems to base this off of, but I've read over arrays. I have gotten farther--last time the code wouldn't even run--but there's still a problem before it makes it to the third function. My task is:

"Write a three function program. Main function must create a string array with room for 10 elements. Main function should call the second function passing it the array as the first argument and the size of the array as the second argument. The second function should get 8 names from the user and return the number of names to main. After the array is filled, main should call a third function passing it the array as the first argument and the value returned by function 2 as the second argument. The third function should display the names from the array."

Asking for ten elements and taking 8 names doesn't add up to me. If 10 stood for how many characters could be entered in the name I'd understand, but I don't think it works like that (at least right now). Would it help initializing it to something? At this point I'm calling arraySize to secondFunction but not using it as I need to take in 8 names, not 10. Could this be causing a problem, by chance? Maybe too many characters being entered in the names? I put in arraySize instead of 8 in the for loop's count with no difference except taking in ten names.

Compiler says nothing's wrong, yet I doubt that with the crashing.

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
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
using namespace std;

//Function prototypes
string secondFunction(string[], int);
void thirdFunction(string[], int);

int main()
{
	int arraySize(10);		// Holds 10 elements
	int names = 8;			// Passes value to third function
	string array[arraySize];	// Array with a size of 10 elements

// I know I'm cheating with the names thing, I'll look at it later: 
// compiler was giving me an error about const char!
	
	// Call second function
	secondFunction(array, arraySize);
		
	// Call third function
	thirdFunction(array, 8);
	
	return 0;
}

string secondFunction (string array[], int arraySize)
{
	int names = 0;
	
	// Get names.
	for (int count = 0; count < 8; count++)
	{
		cout << "Enter name " << (count + 1) << " of 8: ";
		cin >> array[count];
		
		if (count < 8)
		{
			names++;
		}
	}
	// Display amount of names entered.
	cout << names << " received.";
}

void thirdFunction (string array[], int names)
{
	// Display back names in array.
	for (int count = 0; count < names; count++)
	{
		cout << array[count] << endl;
	}
}


My sample output went:
Enter name 1 of 8: kyle
Enter name 2 of 8: liam
Enter name 3 of 8: coco
Enter name 4 of 8: wilbert
Enter name 5 of 8: sam
Enter name 6 of 8: brittanykittylongname
Enter name 7 of 8: ben
Enter name 8 of 8: ron
8 received.

And crashed immediately after.

Any hints you can give me as to why this isn't working would be greatly appreciated. I've got a final in this class coming up and I'd like to understand the material. I feel like I'm on the right track, I just can't quite get it.
Last edited on
Change the return value of secondFunction from string to void - it is crashing because you say it returns string but you have no return statement. I am almost sure you want this function to not return anything, so its return type should be void.
Last edited on
I did not look through all your code but in the very beginning of your code I see an inconsistence. In your assignment there is written

"The second function should get 8 names from the user and return the number of names to main"

but your functions has return type string

string secondFunction(string[], int);

Also if you are using class std::string you should include header <string>
Thank you both for the prompt replies.

I tried changing secondFunction to void as well as changing it to int (as I do need to return an int value, names, to main) and the compiler gave me:

error: cannot convert 'std::string {aka std::basic_string<char>}' to 'std::string* {aka std::basic_string<char>*}' for argument '1' to 'int secondFunction(std::string*, int)'

Based on the asterisks I'm thinking it's not happy about my string now that it's an int. I'm not sure how to fix that.

I also added "#include <string>" at the top under #include <iostream>.
vlad, is that what you meant? I'm sorry if it's not, my class has never talked about the string header.
Can you re-post your current code?

Your arraySize int in main needs to be const.
Sure thing. This includes changing string to int (so I still get the error above), I added what is possibly a string header, and I just changed arraySize to const (thanks, by the way).

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
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <string>
using namespace std;

//Function prototypes
int secondFunction(string[], int);
void thirdFunction(string[], int);

int main()
{
	int const arraySize(10);	// Holds 10 elements
	int names = 8;			// Passes value to third function
	string array;			// Array with a size of 10 elements
	
	// Call second function
	secondFunction(array, arraySize);
		
	// Call third function
	thirdFunction(array, 8);
	
	return 0;
}

int secondFunction (string array[], int arraySize)
{
	int names = 0;
	
	// Get names.
	for (int count = 0; count < 8; count++)
	{
		cout << "Enter name " << (count + 1) << " of 8: ";
		cin >> array[count];
		
		if (count < 8)
		{
			names++;
		}
	}
	// Display amount of names entered.
	cout << names << " received.";
}

void thirdFunction (string array[], int names)
{
	// Display back names in array.
	for (int count = 0; count < names; count++)
	{
		cout << array[count] << endl;
	}
}
Look at line 12 of your original program and then look at line 13 of your latest program. Why did you change this? Change it back ;p
I have no idea why I changed that... how weird/silly of me. Thank you so much for pointing that out, I changed it back and the code runs without crashing! :)
Topic archived. No new replies allowed.