In: Computer Science
C++ PLEASE
Write a program to prompt the user to display the following menu:
Guess-Number Concat-names Quit
Guess a number: 76
96:
Too large
10
Too small
70
Close
Do you want to quit? Q
How many names: 3
First name last name Combined
Sue Smith Sue Smith
Alan Davidson Alan Davidson
David Lee David Lee
Do you want to quit? Q
C++ please
Please find the program below.
#include <iostream>
#include <stdlib.h> /* srand, rand */
#include <time.h>
using namespace std;
void sort_name ( string firstname[] , string lastname[],int
count){
cout<<"First name"<<"\t\t"<<"Last
name"<<"\t\t"<<"Combined name"<<endl;
cout<<"------------------------------------------------------------"<<endl;
for (int i=0;i<count;i++){
cout<<firstname[i]<<"\t\t\t"<<lastname[i]<<"\t\t\t"<<firstname[i]<<"
"<<lastname[i]<<endl;
}
}
int main()
{
int number=0;
cout<<"1. Guess-Number"<<endl ;
cout<<"2. Concat-names"<<endl;
cout<<"3. Quit"<<endl;
cin>>number;
switch (number)
{
case 1:
{
int random_number=0,guess_number;
char ans;
/* initialize random seed: */
srand (time(NULL));
/* generate secret number between 1 and 10: */
random_number = rand() % 100 + 1;
while(1){
cout<<"Guess the number "<<endl;
cin>>guess_number;
if(guess_number<random_number){
cout<<"Too small"<<endl;
}
else if (guess_number>random_number){
cout<<"Too large"<<endl;
}
else{
cout<<"Close"<<endl;
break;
}
}
cout<<"Do you want to quit? Press Q to quit
..."<<endl;
cin>>ans;
if(ans=='Q'){
exit(0);
break;}
}
case 2:{
while(1){
int name;
char ans;
cout<<"How many names ?" <<endl;
cin>>name;
string firstname[name];
string lastname[name];
for (int i=0;i<name;i++){
cin>>firstname[i];
cin>>lastname[i];
}
sort_name (firstname,lastname,name);
cout<<"Do you want to quit? Press Q to quit
..."<<endl;
cin>>ans;
if(ans=='Q'){
exit(0);
break;
}
}
}
case 3:{ exit(0); }
}
return 0;
}
OUTPUT :