In: Computer Science
Instructions
Write a program in C++ that create a LookupNames project.
In the main function:
Ask the user to enter a number of names, X to quit input.
Store the names in an array.
Also use a counter variable to count the number of names
entered.
Write a function displayNames to display the
names.
The function must receive the array and the counter as
parameters.
Write a function called lookupNames.
The function must receive the array and the counter as
parameters.
Ask the user to enter a letter.
Display all the names with the letter that was entered as the first
letter of the name.
Call the displayNames and lookupNames functions from the main function.
Tip: declare your functions above the main() function:
void displayNames(char array[][60], int count)
{
// function code here
}
int main()
{
// main code here
displayNames(names, number);
// possible more code here
}
Tip2: make sure your function parameter matches the data type
you send as an argument to that function.
In the above example, names and array should have
the same data type, and number and count should
have the same data type.
Enter name (X to quit input): John Peterson
Enter name (X to quit input): Diane Lee
Enter name (X to quit input): James Smith
Enter name (X to quit input): Frank Xaba
Enter name (X to quit input): Jacky Mokabe
Enter name (X to quit input): x
List of Names
John Peterson
Diane Lee
James Smith
Frank Xaba
Jacky Mokabe
Enter a letter: J
Names starting with the letter J
John Peterson
James Smith
Jacky Mokabe
Thanks for the question. Below is the code you will be needing. Let me know if you have any doubts or if you need anything to change.
If you are satisfied with the solution, please leave a +ve feedback : ) Let me know for any help with any other questions.
Thank You!
===========================================================================
#include<iostream>
#include<cstring>
using namespace std;
void displayNames(char array[][60], int count){
cout<<"List of Names\n";
for(int i=0; i<count;i++){
cout<<array[i]<<endl;
}
cout<<endl;
}
void lookupNames(char array[][60], int count){
char letter;
cout<<"Enter a letter: "; cin >>
letter;
for(int i=0; i<count;i++){
if(array[i][0]==letter)
cout<<array[i]<<endl;
}
}
int main(){
const int SIZE = 100;
char names[SIZE][60];
char name[60];
int count = 0;
while(count<SIZE) {
cout<<"Enter name (X to quit
input): ";
cin.getline(name,59,'\n');
if(strcmp(name,"X")==0 ||
strcmp(name,"x")==0) break;
strcpy(names[count],name);
count++;
}
displayNames(names, count);
lookupNames(names, count);
return 0;
}
=================================================================