Question

In: Computer Science

Part B) Write a C++ program which prompts user to choose from the two alternative funtions...

Part B)

Write a C++ program which prompts user to choose from the two alternative funtions specified below, each of which passes a random number between 1 and 4 and returns a random message.

The two functions are:
Function messageByValue that passes a random value between 1 and 4 and returns the message by value and
Function messageByReference that passes a random value between 1 and 4 and returns the message by reference.

Each function returns one of four random messages:
- “Sorry. You’re busted!”
- “Oh, you’re going for broke, huh?”
- “Aw cmon, take a chance!”
- You’re up big. Now’ the time to cash in your chips”

NOTE:
Use a while loop to continually run program until a -1 is entered to Terminate program.

Solutions

Expert Solution

Solution for the given question are as follows -

Code :

#include<iostream>
#include <stdlib.h>
using namespace std;
// messageByReference - return message by reference
string &messageByReference (string s[], int x)
{
   return s[x];
}
// messageByValue - return message by value
string messageByValue(string s[],int x)
{
   return s[x];
}
int main()
{
// variable declaration
int n;
string s, str[4] = {"Sorry. You’re busted!", "Oh, you’re going for broke, huh?", "Aw cmon, take a chance!", "You’re up big. Now’ the time to cash in your chips"};
// infinite loop
do {
cout << "Enter your choice: \n";
cout << "1. MessageByValue: \n";
cout << "2. MessageByReference : \n";
cout << "Exit -1 \n";
cin >> n;
// get random number
int x = rand() % 3;
switch (n) {
case 1 :
// call to function and pass array of string and random number
s = messageByValue(str,x);
cout<<s<<"\n";
break;
case 2 :
s = messageByReference(str,x);
cout<<s <<"\n";
break;
case -1 :
exit(0);
break;
default:
cout<<"Invalid choice\n";
break;
}
} while (n != -1);
   return 0;
}

Code Screen Shot :

Output :


Related Solutions

C++ Program: Write a program that prompts the user for two numbers and stores them in...
C++ Program: Write a program that prompts the user for two numbers and stores them in signed integers. The program should then add those two numbers together and store the result in a signed integer and display the result. Your program should then multiply them by each other and store the result in another integer and display the result. Then do the same but with dividing the first number by the second. Display an error message to the screen if...
write this program in C++ Write a program that prompts a user for three characters. The...
write this program in C++ Write a program that prompts a user for three characters. The program must make sure that the input is a number 10 - 100 inclusive. The program must re prompt the user until a correct input is entered. Finally output the largest and the lowest value. Example 1: Input : 10 Input : 20 Input : 30 The largest is 30. The lowest is 10. Example 2: Input : 100 Input : 50 Input :...
Please Write C++ PROGRAM : That will write a program that initially prompts the user for...
Please Write C++ PROGRAM : That will write a program that initially prompts the user for a file name. If the file is not found, an error message is output, and the program terminates. Otherwise, the program prints each token in the file, and the number of times it appeared, in a well formatted manner. To accomplish all this, do the following: - Open the file - the user must be prompted and a file name input. DO NOT hardcode...
Write a C++ Program Write a program that prompts the user to input a string. The...
Write a C++ Program Write a program that prompts the user to input a string. The program then uses the function substr to remove all the vowels from the string. For example, if str=”There”, then after removing all the vowels, str=”Thr”. After removing all the vowels, output the string. Your program must contain a function to remove all the vowels and a function to determine whether a character is a vowel. You must insert the following comments at the beginning...
Write a C++ program that prompts the user for the radius of a circle and then...
Write a C++ program that prompts the user for the radius of a circle and then calls inline function circleArea to calculate the area of that circle. It should do it repeatedly until the user enters -1. Use the constant value 3.14159 for π Sample: Enter the radius of your circle (-1 to end): 1 Area of circle with radius 1 is 3.14159 Enter the radius of your circle (-1 to end): 2 Area of circle with radius 2 is...
Write a program in C that prompts the user for a number of seconds and then...
Write a program in C that prompts the user for a number of seconds and then converts it to h:m:s format. Example: 5000 seconds should display as 1:23:20 (1 hour, 23 minutes, 20 seconds.) Test with several values between about 100 seconds and 10,000 seconds. use unint and remainders for this and keep it as simple as possible.
Write a C++ program which prompts the user to enter an integer value, stores it into...
Write a C++ program which prompts the user to enter an integer value, stores it into a variable called ‘num’, evaluates the following expressions and displays results on screen. num+5, num-3, (num+3) – 2, ((num+5)*2 / (num+3)) For performing addition and subtraction, you are allowed to use ONLY the increment and decrement operators (both prefixing and postfixing are allowed). You must remember that using increment/decrement operators changes the original value of a number. Indent your code and include comments for...
Part A In PyCharm, write a program that prompts the user for their name and age....
Part A In PyCharm, write a program that prompts the user for their name and age. Your program should then tell the user the year they were born. Here is a sample execution of the program with the user input in bold: What is your name? Amanda How old are you? 15 Hello Amanda! You were born in 2005. Write the program. Format your code using best practices. Refer to the zyBooks style guide, if needed, to use proper naming...
Write a program which prompts the user for the year of their birth, and which then...
Write a program which prompts the user for the year of their birth, and which then calculates their age (ignoring the month/day). Split the program into `main`, a "prompt for year" function which tests to make sure the user's input is a valid year, and a "calculate age" function which performs the actual calculation. (You can assume the current year is 2017.) for my intro to c++ class
C++ Question: write a program that prompts the user for the length and width of a...
C++ Question: write a program that prompts the user for the length and width of a rectangle in inches.  The program then uses functions to compute the perimeter and area of the rectangle and to convert those to meters and square meters respectively. Sample output from one instance of the program is shown below: ```html Welcome to the Foot-To-Meter Rectangle Calculator ================================================= Enter the rectangle length in feet: 2 Enter the rectangle width in feet: 3 The rectangle dimensions are: 0.61...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT