In: Computer Science
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++
Code:
#include<iostream>
#include<random>
#include<ctime>
#include<iomanip>
using namespace std;
void guess_number()
{
srand(time(0));
char choice;
do
{
int guess=0;
int num=rand()%(100)+1;/*Generating a random number*/
cout<<"Guess the number:";
while(guess!=num)
{
cin>>guess;/*Reading user input*/
if(guess>num)
{/*if guess is greater than random number*/
cout<<"Too Large"<<endl;
}
else if(guess<num)
{/*if guess is less than random number*/
cout<<"Too Small"<<endl;
}
else
{/*if boath are equal*/
cout<<"Close"<<endl;
}
}
cout<<"Do you want to quit?";
cin>>choice;/*users choice*/
}while(choice!='Q');
}
void sort_name(char first[],char last[])
{/*Here we print the concatinated name*/
int i=0;
while(first[i]!='\0')
{/*Here we print the first name*/
cout<<first[i];
i++;
}
i=0;
cout<<" ";
while(last[i]!='\0')
{/*Here we print the last name*/
cout<<last[i];
i++;
}
cout<<""<<endl;
}
int main()
{
while(true)
{
cout<<"1.
Guess_Number"<<endl;
cout<<"2.Concat-names"<<endl;
cout<<"3.Quit"<<endl;/*Priting the menu*/
int choose;
cout<<"Enter your
choice:";
cin>>choose;/*Reading
users choice*/
switch(choose)
{
case
1:/*if 1*/
guess_number();
break;
case 2:/*if
2*/
char
choice;
do
{
char first[50];
char last[50];
int n,i;/*Decalring
variables*/
cout<<"How many names
:";
cin>>n;/*Reading no of
names*/
cout<<"First
name";
cout<<setw(20)<<"Last Name";
cout<<setw(20)<<"combined"<<endl;
for(i=0;i<n;i++)
{/*This loop reads n
names*/
cin>>first;
cin>>last;
cout<<setw(43);
sort_name(first,last);
}
cout<<"Do you want to
quit?";
cin>>choice;/*users choice*/
}while(choice!='Q');
break;
case 3:/*if
3*/
exit(0);
break;
default:
cout<<"invlaid input"<<endl;
}
}
}
Output:
Indentation: