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.