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!
#include <iostream>
#include <iomanip>
#include <string>
usingnamespace 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
constint 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;
}
}
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.
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++)