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...
Part 3 Write a C++ program that prompts the user for an Account Number(a whole number)....
Part 3 Write a C++ program that prompts the user for an Account Number(a whole number). It will then prompt the user for the initial account balance (a double). The user will then enter either w, d, or z to indicate the desire to withdraw an amount from the bank, deposit an amount or end the transactions for that account((accept either uppercase or lowercase). You must use a nested if statements instead of a Switch construct. Test Run: Enter the...
Part 2 Write a C++ program that prompts the user for an Account Number(a whole number)....
Part 2 Write a C++ program that prompts the user for an Account Number(a whole number). It will then prompt the user for the initial account balance (a double). The user will then enter either w, d, or z to indicate the desire to withdraw an amount from the bank, deposit an amount or end the transactions for that account((accept either uppercase or lowercase). You must use a switch construct to determine the account transaction and a do…while loop to...
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...
Write a C program that uses a loop which prompts a user for an integer 10...
Write a C program that uses a loop which prompts a user for an integer 10 times and stores those integers in an array. The program will print out the numbers entered by the user with a comma and a space separating each number so the numbers can be read easily. The program will then calculate and print out the sum, integer average and floating point average (to 4 decimal points) of the numbers in the array. The information being...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT