Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

1. Assignment 9 Function Pass by Reference Functions that pass by Reference & are an essential skill to have. Additionally, completing this assignment will help

1. Assignment 9 Function Pass by Reference

Functions that pass by Reference & are an essential skill to have. Additionally, completing this assignment will help tremendously as a way to start the restaurant Final Project.

File for submission: Ref_Menu_YourLastName.zip (App/CPP/Folder should share the same name)

1. Type line by line the following code from below

2. Solve each of the challenges within (as presented in the comments, adding the menu items, and solving the patterns in displayPatterns)

3. Validate the input of the menu option. For a cleaner way to validate input consider the Example: Input_Validation_Ref_Functions

4. Add Color to the file to enhance the user experience.

(Ref_Menu_.pdf ) Below:

/*

2: APP/CPP/FOLDER: Ref_Menu_YourLastName

3: This program uses a reference variable as a function

4: parameter.

5: INSTRUCTIONS: Type this code line by line first (with comments), then seek to solve 6: the various programming challenges herein.

7: Base code from Gaddis (2012).

8: Expanded by: Dr. Tyson McMillan, 10182014

9: Student:

10:

11: A menu using the Power of functions that pass values by reference. 12: When a function uses pass by refrence, it can manipulate variable values outside 13: of its own scope. Reference refers to the memory location of the variable.

14: */

15: #include

16: #include //to be able to use system("cls"); in multiple platforms

17: using namespace std;

18:

19: // Function prototype. The parameter is a reference variable.

20: void doubleNum(int &);

21: void increaseNum(int &);

22: void decreaseNum(int &);

23: void displayMenu();

24: void displayPatterns();

25:

26: int main()

27: {

28: int value = 4;

29: cout << "In main, value is " << value << endl;

30: cout << "Now calling doubleNum..." << endl;

31: doubleNum(value);

32: cout << "Now back in main. value is " << value << endl;

33:

34: //using the pass by reference premise on a C++ menu 35: cout << " Let's practice this premise on a C++ Menu" << endl; 36: displayMenu(); //function call 37: 38: /*Programming Challenges 39: 1) Add 4 more menu items to this program.

40: 2) Validate input of decision 41: 3) Display a custom Exit message when 9 inputted; 42: 4) Add color to enhance messages to the user.

43: hint: much of this is done in the displayMenu() function definition

44: */

45:

46: //EXTRA Challenge: displayPatterns()

47: displayPatterns(); //see function definition below 48:

49: return 0;

50: }

51:

52: //**********************************************************

53: // Definition of doubleNum. *

54: // The parameter refVar is a reference variable. The value *

55: // in refVar is doubled. *

56: //**********************************************************

57:

58: void doubleNum(int &refVar)

59: {

60: refVar *= 2;

61: }

62:

63: void increaseNum(int &counter)

64: {

65: counter++;

66: }

67:

68: void decreaseNum(int &counter)

69: {

70: counter;

71: }

72:

73: void displayMenu()

74: {

75: int mainDecision = 0;

76: int greenTeaCounter = 0;

77:

78: do

79: {

80: if(mainDecision == 0) //first run of the menu

81: {

82: cout << " WELCOME TO DR. T'S RESTAURANT!!!" << endl;

83: }

84:

85: //increment menu item

86: if(mainDecision == 1)

87: {

88: increaseNum(greenTeaCounter);

89: }

90:

91: //decrement menu item

92: if(mainDecision == 1)

93: {

94: if(greenTeaCounter != 0) //subtract only if value greather than 0

95: {

96: decreaseNum(greenTeaCounter);

97: }

98: else

99: {

100: cout << "Negative: Green Tea Order must be > 0 to "; 101: cout << "subtract (please try again)" << endl;

102: }

103: }

104:

105: cout << " MENU" << endl;

106: cout << "Item *********** [ Count ]" << endl;

107: cout << "1) Green Tea *** [ " << greenTeaCounter << " ] 1 to remove" << endl; 108: cout << "Please enter an item number above (9 to exit): ";

109: cin >> mainDecision;

110:

111: //clear the screen

112: system("cls");

113:

114: }while(mainDecision != 9); //9 is my exit condition

115: }

116:

117: void displayPatterns()

118: {

119: //nested = for loop with another for loop in its scope.

120: //this was the case with bubble sort

121:

122: /*EXTRA programming challenge1

123: use nested for loops OR one loop to display this pattern:

124:

125: *******

126: *******

127: *******

128:

129: */

130: 131: /*EXTRA programming challenge2

132: use nested for loops to display this pattern:

133:

134: *

135: **

136: ***

137: ****

138: *****

139: ******

140: *******

141:

142: */

143:

144: /*EXTRA programming challenge3

145: use nested for loops to display this pattern:

146:

147: *******

148: ******

149: *****

150: ****

151: ***

152: **

153: *

154:

155: */

156: }

157:

158:

Example: Input_Validation_Ref_Functions

//From: http://stackoverflow.com/questions/514420/how-to-validate-numeric-input-c

#include // Provides ios_base::failure

#include // Provides cin

#include //for string manipulation

#include //for string manipulation and comparison

using namespace std;

//Function Prototypes

int validateInt(int &); //use the validation method to vaildate and return a data type integer pass by reference &

double validateDouble(double &); //use the validation method to vaildate and return a data type double pass by reference &

char validateChar(char &); //use the validation method to vaildate and return a data type char pass by reference &

string validateString(string &); //use the validation method to vaildate and return a data type string pass by reference &

template

T getValidatedInput()

{

// Get input of type T

T result;

cin >> result;

// Check if the failbit has been set, meaning the beginning of the input

// was not type T. Also make sure the result is the only thing in the input

// stream, otherwise things like 2b would be a valid int.

if (cin.fail() || cin.get() != ' ')

{

// Set the error state flag back to goodbit. If you need to get the input

// again (e.g. this is in a while loop), this is essential. Otherwise, the

// failbit will stay set.

cin.clear();

// Clear the input stream using and empty while loop.

while (cin.get() != ' ')

;

// Throw an exception. Allows the caller to handle it any way you see fit

// (exit, ask for input again, etc.)

throw ios_base::failure("Invalid input.");

}

return result;

}

//Function Definitions

int validateInt(int &intInput)

{

while (true)

{

cout << "Enter an integer: ";

try

{

intInput = getValidatedInput();

}

catch (exception e)

{

cerr << e.what() << endl;

continue;

}

break;

}

return intInput;

}

double validateDouble(double &doubleInput)

{

while (true)

{

cout << "Enter a number with or without decimals (double): ";

try

{

doubleInput = getValidatedInput();

}

catch (exception e)

{

cerr << e.what() << ": Invalid input."<< endl;

continue;

}

break;

}

return doubleInput;

}

char validateChar(char &charInput)

{

while (true)

{

cout << "Enter a single letter or number (1 digit): ";

try

{

charInput = getValidatedInput();

}

catch (exception e)

{

cerr << e.what() << ": Invalid input."<< endl;

continue;

}

break;

}

return charInput;

}

string validateString(string &stringInput)

{

while (true) //use cin, getline() for this

{

cout << "Enter a word (no spaces): ";

try

{

stringInput = getValidatedInput();

}

catch (exception e)

{

cerr << e.what() << ": Invalid input."<< endl;

continue;

}

break;

}

return stringInput;

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Systems An Application Oriented Approach Complete Version

Authors: Michael Kifer, Arthur Bernstein, Richard Lewis

2nd Edition

0321268458, 978-0321268457

More Books

Students also viewed these Databases questions

Question

In an Excel Pivot Table, how is a Fact/Measure Column repeated?

Answered: 1 week ago