Array program Crashing

Hi there! I have neared finishing my program but every time it outputs all the names, I get an error 'a.exe has stopped working'. Can someone tell me what exactly I am doing wrong? I have looked at all the tutorials but I still can't figure it out. Thanks!

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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67

#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

//function 2 prototype

string secondFunction(string array[], int SIZE);

//function 3 prototype

string thirdFunction(string array[], int NAMES);

int main()
{
	
	//create a string array with room for 10 elements
	
	const int size(10);
	string array[size];
	int names;

	//call the second function passing it the array as
	//the first argument and the size of the array
	//as the second argument 
	
	secondFunction(array, size);
	
	
	//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 
	
	thirdFunction(array, names);

	
	return 0;
}

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

string secondFunction(string array[], int size)
{
	
	for ( size = 0; size < 8; size++)
	{
		cout << "Please enter a name: ";
		cin >> array[size];
	}
	return array[size];
	
}

//third function should display the names from the
//array on separate lines on the computer screen. 

string thirdFunction(string array[], int names)
{
	for (names = 0; names < 8; names++)
	{
		cout << array[names] << endl;
	}
}



My output is as follows:

Please enter a name: adam
Please enter a name: bart
Please enter a name: celia
Please enter a name: dave
Please enter a name: elvis
Please enter a name: frodo
Please enter a name: gimli
Please enter a name: harry
adam
bart
celia
dave
elvis
frodo
gimli
harry


However right after everything outputs, I get a windows crash error Window right over my screen.

Thanks for the help!
The problem is you are passing a value that isn't defined yet into a function which isn't a good idea. Set names equal to 0 in main and it should solve the main crashing problem.

Your 'thirdFunction' should be void as it is not returning anything, it is just printing out a list of names (it has a string return type right now). Don't forget to change the prototype too.

Also how come you are only doing 0 - 7 when your array has 10 values? I would expect
for ( size = 0; size < 10; size++)
g++ -Wall
66: warning: no return statement in function returning non-void [-Wreturn-type]


Also, your function parameter makes no sense.
secondFunction(array, 42); will be the same
Thanks for your help! Got everything working correctly!
Topic archived. No new replies allowed.