Write a three function program (including main). The main function must create a
ID: 3547670 • Letter: W
Question
Write a three function program (including main).
The main function must create a string array with room for 10 elements.
The main function should call the second function passing it the array as the first argument and the number of elements in the array as the second argument.
The second function is passed in an array and the number of elements in the array. The function should get 8 names from the user and return the number of names read back to main. Use the return statement to do this.
After the array is filled by the second function, main should then call a third function passing it the array as the first argument and the value returned by the second function as the second argument.
The third function should display the names from the array on separate lines on the computer screen. The third function is passed in an array as the first parameter. The second parameter is the number of elements in the array to be displayed.
The main function has an array of 10 elements. The second function is passed that array of 10 elements but only reads in 8 elements. The number read in by the second function is returned back to main. Main then passes the array and the value returned back from the second function to the third function.
Sample output:
Enter in name 1: Mary[Enter]
Enter in name 2: Margaret[Enter]
Enter in name 3: Robert[Enter]
Enter in name 4: Fred[Enter]
Enter in name 5: Sally[Enter]
Enter in name 6: Sue[Enter]
Enter in name 7: Vern[Enter]
Enter in name 8: Fern[Enter]
Name 8 is Fern
You must not use global variables.
Explanation / Answer
#include<iostream>
#include<string>
using namespace std;
int ReadString(string strNames[],int num)
{
string ReplaceName;
for (int i=1; i<9;i++)
{
cout<<"Enter in Name "<<i<<" : ";
getline(cin,ReplaceName);
strNames[i-1]=ReplaceName;
}
return 8;
}
void DisplayString(string strNames[], int num)
{
for (int i=1; i<9;i++)
{
cout<<"Name "<<i<<" is "<<strNames[i-1]<<endl;
}
}
int main()
{
string Names[10];
int num;
num = ReadString(Names,10);
DisplayString(Names,num);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.