Question

In: Computer Science

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 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++

Solutions

Expert Solution

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:


Related Solutions

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...
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                                                      ...
Write a program names EncryptDecrypt.java that has the following menu choices: Print menu and allow user...
Write a program names EncryptDecrypt.java that has the following menu choices: Print menu and allow user to choose options. The program must have a file dialogue box for text file. Output should be based on user choices. Read in a file Print the file to the console Encrypt the file and write it to the console Write out the encrypted file to a text file Clear the data in memory Read in an encrypted file Decrypt the file Write out...
Write a java code snippet to prompt the user for the number of names they’d like...
Write a java code snippet to prompt the user for the number of names they’d like to enter. Create a new array of the size chosen by the user and prompt the user for each of the names. Output the list of names in reverse order.
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...
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...
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 JAVA program that prompts the user for the number of names they’d like to...
Write a JAVA program that prompts the user for the number of names they’d like to enter. Create a new array of the size chosen by the user and prompt the user for each of the names. Output the list of names in reverse order.
Write a java program that will first display the following menu: Choose one of the following...
Write a java program that will first display the following menu: Choose one of the following 1- To add 2 double integers 2- To add 2 integer numbers 3- To add 3 double numbers 4- To add 3 integer numbers After reading user’s choice, use a switch case statement to call the corresponding method Void add 1 (double n1, double n2) Void add2() Double add3 (double n1, double n2, double n3) Double add4 ()
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT