Question

In: Computer Science

C++ PLEASE Write a program to prompt the user to display the following menu: Guess-Number                       ...

C++ PLEASE

Write a program to prompt the user to display the following menu:

Guess-Number                        Concat-names             Quit

  • If the user selects Guess-number, your program needs to call a user-defined function called int guess-number ( ). Use random number generator to generate a number between 1 – 100. Prompt the user to guess the generated number and print the following messages. Print the guessed number in main ():

Guess a number: 76

96:

Too large

10

Too small

70

Close

Do you want to quit? Q

  • If the user selects Concat-name, prompt the user how many names to concatenate and then enter all the first and las names and store then into a two one-dimensional array. Your program needs to call a user-defined function called void sort-name ( char [] , char[]) to concatenate the first name and the last name and print the names in this function.

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

Solutions

Expert Solution

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 :


Related Solutions

Write a program to prompt the user to display the following menu: Guess-Number                        Concat-names     &
Write a program to prompt the user to display the following menu: Guess-Number                        Concat-names             Quit If the user selects Guess-number, your program needs to call a user-defined function called int guess-number ( ). Use random number generator to generate a number between 1 – 100. Prompt the user to guess the generated number and print the following messages. Print the guessed number in main (): Guess a number: 76 96: Too large 10 Too small 70 Close Do you...
Write a program to prompt the user to display the following menu: Sort             Matrix                   Q
Write a program to prompt the user to display the following menu: Sort             Matrix                   Quit If the user selects ‘S’ or ‘s’, then prompt the user to ask how many numbers you wish to read. Then based on that fill out the elements of one dimensional array with integer numbers. Then sort the numbers and print the original and sorted numbers in ascending order side by side. How many numbers: 6 Original numbers:                     Sorted numbers 34                                                                         2          55                                                      ...
Write a program to prompt the user to display the following menu: Sort             Matrix                   Q
Write a program to prompt the user to display the following menu: Sort             Matrix                   Quit If the user selects ‘S’ or ‘s’, then prompt the user to ask how many numbers you wish to read. Then based on that fill out the elements of one dimensional array with integer numbers. Then sort the numbers and print the original and sorted numbers in ascending order side by side. How many numbers: 6 Original numbers:                     Sorted numbers 34                                                                         2          55                                                      ...
1. Write a program in C++ to find the factorial of a number. Prompt the user...
1. Write a program in C++ to find the factorial of a number. Prompt the user for a number and compute the factorial by using the following expression. Use for loops to write your solution code. Factorial of n = n! = 1×2×3×...×n; where n is the user input. Sample Output: Find the factorial of a number: ------------------------------------ Input a number to find the factorial: 5 The factorial of the given number is: 120 2. Code problem 1 using While...
Write a C program that asks the user to guess a number between 1 and 15(1...
Write a C program that asks the user to guess a number between 1 and 15(1 and 15 are included). The user is given three trials. This is what I have so far. /* Nick Chioma COP 3223 - HW_2 */ #include <iostream> #include <time.h> // needed for time function #include <stdlib.h> // needed for srand, rand functions int main () {       int numbertoguess, num, correct, attempts;       srand(time(NULL)); //this initializes the random seed, making a new...
write a program to perform the following in C Your program should prompt the user to...
write a program to perform the following in C Your program should prompt the user to enter ten words, one at a time, which are to be stored in an array of strings. After all of the words have been entered, the list is to be reordered as necessary to place the words into alphabetical order, regardless of case. Once the list is in alphabetical order, the list should be output to the console in order. The program should execute...
IN C This assignment is to write a program that will prompt the user to enter...
IN C This assignment is to write a program that will prompt the user to enter a character, e.g., a percent sign (%), and then the number of percent signs (%) they want on a line. Your program should first read a character from the keyboard, excluding whitespaces; and then print a message indicating that the number must be in the range 1 to 79 (including both ends) if the user enters a number outside of that range. Your program...
In Java: Write a program that generates a random number and asks the user to guess...
In Java: Write a program that generates a random number and asks the user to guess the number and keeps track of how many guesses it took If the user input is negative or zero then the loop must stop reading further inputs and display how many guesses they used If they guess the correct number display a message telling them they got it and exit the program If they guess the wrong number (but still a legal guess) you...
Write a C program Your program will prompt the user to enter a value for the...
Write a C program Your program will prompt the user to enter a value for the amount of expenses on the credit card. Retrieve the user input using fgets()/sscanf() and save the input value in a variable. The value should be read as type double. The user may or may not enter cents as part of the input. In other words, expect the user to enter values such as 500, 500.00 and 500.10. The program will then prompt the user...
17. Write a program that generates a random number and asks the user to guess what...
17. Write a program that generates a random number and asks the user to guess what the number is. If the user’s guess is higher than the random number, the program should display “Too high, try again.” If the user’s guess is lower than the random number, the program should display “Too low, try again.” The program should use a loop that repeats until the user correctly guesses the random number. 18. Enhance the program that you wrote for Programming...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT