c++ - Function with arrays not working -
i need make program 3 functions.
the main function should call second function passing array first argument , number of elements in array second argument.
the second function passed in array , number of elements in array. function should 8 names user , return number of names read main. use return statement this.
after array filled second function, main should call third function passing array first argument , value returned second function second argument.
the third function should display names array on separate lines on computer screen. third function passed in array first parameter. second parameter number of elements in array displayed.
the main function has array of 10 elements. second function passed array of 10 elements reads in 8 elements. number read in second function returned main. main passes array , value returned second function third function.
my code point is:
#include <iostream> #include <string> using namespace std; int main() { // names , store them in array int const arraysize(10); int names = 8; string array[arraysize]; // send second function recievenames(array, arraysize); // send third function displaynames(array, 8); return 0; } int recievenames(string array[], int arraysize) { int names = 0; // names. (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 displaynames(string array[], int names) { // display names entered in array. (int count = 0; count < names; count++) { cout << array[count] << endl; } }
for reason isn't working can tell me why?
you have foward declartion of function prototypes recievenames()
, displaynames()
, place them before main()
. can define , embody functions after main.
#include <iostream> #include <string> using namespace std; // declare function prototypes int recievenames(string array[], int arraysize); void displaynames(string array[], int names); int main() { // names , store them in array int const arraysize(10); int names = 8; string array[arraysize]; // send second function recievenames(array, arraysize); // send third function displaynames(array, 8); return 0; } //define functions int recievenames(string array[], int arraysize) { int names = 0; // names. (int count = 0; count < 8; count++) { cout << "enter name " << (count + 1) << " of 8: "; cin >> array[count]; if (count < 8) { names++; } return 0; } // display amount of names entered. cout << names << " received."; } void displaynames(string array[], int names) { // display names entered in array. (int count = 0; count < names; count++) { cout << array[count] << endl; } }
Comments
Post a Comment