Previous Page TOC Next Page



- Project 8 -
Lots of Data


In this lesson, you learned about advanced variable access techniques. You saw the following:

Project 8 Listing. Searching through arrays for property data.


  1:// Filename: PROJECT8.CPP

  2:// A simple in-memory property database program. Through a menu,

  3:// the user decides if he or she wants to see a property

  4:// database on the screen or search for a specific property.

  5:#include <iostream.h>

  6:#include <string.h>   // For strcmp()                     

  7:

  8:void DisplayMenu();

  9:int  GetAnswer();

 10:void DisplayProps(char * code[], float price[],

 11:                  char * addr[], float commPer[]);

 12:void SearchProps(char * code[], float price[],

 13:                 char * addr[], float commPer[]);

 14:

 15:// Eight properties maximum due to next constant

 16:int const NUM = 8;

 17:

 18:void main()

 19:{

 20:  int ans;

 21:  // Define the program's data in parallel arrays

 22:  // A code that uniquely identifies each property

 23:  char * code[NUM] = { "231DV", "821WQ", "199OI", "294JU",

 24:                       "901RE", "829BN", "483LQ", "778AS" };

 25:  // The price of each property

 26:  float price[NUM] = { 89432.34, 123029.34, 321293.95,

 27:                       214293.20, 68402.92, 421034.53,

 28:                       232456.54, 432123.40};

 29:   // The address of each property

 30:   char * addr[NUM] = { "919 N. Elm", "2202 West Sycamore",

 31:                        "7560 E. 26th Pl.", "213 W. 104th Ave",

 32:                        "123 Willow Rd.", "5629 S. 188th",

 33:                        "45 North Harvard", "17093 Lansford" };

 34:   // The broker's commission on each property

 35:   float commPer[NUM] = {.072, .07, .065, .091,

 36:                         .078, .0564, .102, .0834 };

 37:  do

 38:  {

 39:    DisplayMenu();

 40:    ans = GetAnswer();

 41:

 42:    switch (ans)

 43:    { case (1) : { DisplayProps(code, price, addr, commPer);

 44:                   break; }

 45:      case (2) : { SearchProps(code, price, addr, commPer);

 46:                   break; }

 47:      case (3) : { return;

 48:                   break; }   // "unreachable code"

 49:    }   // If user entered bad value, while loop will repeat

 50:  } while (ans != 3);   // Keep looping until return takes over

 51:  return;

 52:}

 53://*********************************************************

 54:void DisplayMenu()

 55:{   // Display a menu for the user

 56:  cout << endl << endl;

 57:  cout << "\t\t** Property Database Menu **" << endl << endl;

 58:  cout << "Here are your choices:" << endl << endl;

 59:  cout << "\t1. Look at the property listing" << endl;

 60:  cout << "\t2. Search for a property by its code" << endl;

 61:  cout << "\t3. Quit the program" << endl;

 62:  cout << endl << "What is your choice? ";

 63:  return;

 64:}

 65://*********************************************************

 66:int GetAnswer()

 67:{   // Get the user's menu choice

 68:  int ans;   // Local variable also named ans

 69:  cin >> ans;   // Answer to menu

 70:  cin.ignore(80,'\n');

 71:  return (ans);

 72:}

 73://*********************************************************

 74:void DisplayProps(char * code[], float price[],

 75:                  char * addr[], float commPer[])

 76:{   // Display a list of properties

 77:  int ctr;   // for-loop control variable

 78:  cout.precision(2);

 79:  cout.setf(ios::showpoint);

 80:  cout.setf(ios::fixed);

 81:  for (ctr = 0; ctr < NUM; ctr++)

 82:    { 

 83:      cout << endl << "Code: " << code[ctr] 

 84:           << "\t Price: $" << price[ctr] << endl;

 85:      cout << "Address: " << addr[ctr] << endl;

 86:      cout << "Commission percentage: "

 87:           << commPer[ctr] * 100.0 << "%" << endl << endl;

 88:      if (ctr == 3)   // Don't scroll off too fast

 89:        { 

 90:          cout << "Press enter to continue...";

 91:          cin.ignore(80,'\n');

 92:        }

 93:    }

 94:  cout << "Press enter to continue...";

 95:  cin.ignore(80,'\n');

 96:

 97:  return;

 98:}

 99://*********************************************************

100:void SearchProps(char * code[], float price[],

101:                 char * addr[], float commPer[])

102: {   // Ask the user for a property code and display match

103:   int ctr;         // for-loop control variable

104:   int found = 0;   // Initially not found

105:   char buf[6];      // Code plus null zero size

106:   // Get the search key

107:   cout << "I'll now search for a specific property." << endl;

108:   cout << "What is the property's code? ";

109:   cin.getline(buf, 6);

110:   for (ctr = 0; ctr < NUM; ctr++)

111:     { 

112:       if (!strcmp(code[ctr], buf))

113:         { 

114:           cout << endl << "Code: " << code[ctr] 

115:                << "\t Price: $" << price[ctr] << endl;

116:           cout << "Address: " << addr[ctr] << endl;

117:           cout << "Commission percentage: " 

118:                << commPer[ctr]*100.0

119:                << "%" << endl << endl;   // Show as a percent

120:           found = 1;

121:           break;

122:         }

123:      }

124:   if (!found)

125:     { 

126:       cout << endl << "* I'm sorry, but I don't find code "

127:            << buf;

128:     }

129:  return;

130: }

Output

Description

1: A C++ comment that includes the program's filename.

2: A C++ comment that contains the program's description.

3: The program's description continues.

4: The program's description continues.

5: cout and cin need information in the IOSTREAM.H header file.

6: The strcmp() function requires STRING.H.

7: Place blank lines throughout your code to improve your program's readability.

8: The functions written by the programmer are prototyped. DisplayMenu takes no parameters.

9: The second prototyped function, GetAnswer, takes no parameters.

10: The third prototyped function, DisplayProps, takes four parameters.

11: The third prototype continues here to make the parameter list readable.

12: The fourth prototyped function. SearchProps takes four parameters.

13: The fourth prototype continues here to make the parameter list readable.

14: A blank line helps separate the prototypes from the rest of the program.

15: Comments the defined constant that follows.

16: Defines a named integer constant that holds the number of properties in the database.

17: A blank line helps separate the opening code from main().

18: main() begins.

19: All functions begin with an opening brace.

20: An integer variable that will hold the user's menu response.

21: Comments that describe the data.

22: Comments that describe the data.

23: The first of four parallel arrays that hold property data. code contains a unique code number for each property.

24: The code array's values are still being initialized.

25: Place comments throughout your code.

26: The parallel array that holds the price of each property.

27: The price array's values are still being initialized.

28: The price array's values are still being initialized.

29: Place comments throughout your code.

30: The parallel array that holds the address of each property.

31: The addr array's values are still being initialized.

32: The addr array's values are still being initialized.

33: The addr array's values are still being initialized.

34: Place comments throughout your code.

35: The parallel array that holds the broker's commission.

36: The commPer array's values are still being initialized.

37: Start of the loop that displays a menu.

38: All loop bodies should contain braces.

39: Displays a menu for the user.

40: Gets the user's menu response from the GetAnswer() function.

41: A blank line to help separate the switch statement.

42: switch will determine what code executes in response to the user's answer.

43: If the user entered a 1, calls the property-displaying function and passes the parallel arrays to the function to be printed.

44: break keeps the rest of the case code from executing.

45: If the user entered a 2, calls the property-searching function and passes the parallel arrays to the function to be printed.

46: break keeps the rest of the case code from executing.

47: If the user entered a 3, terminates the program.

48: This break is for completeness. If return executes, execution will never get here.

49: Close all switch statements with a right brace.

50: Keeps displaying the menu as long as the user doesn't enter a 3.

51: Returns to the QuickWin, even though the return in line 47 actually keeps this return from ever executing.

52: All functions end with a closing brace.

53: The asterisk comment helps separate functions from each other.

54: The definition line for the menu-displaying function.

55: All functions begin with an opening brace.

56: Prints the menu text.

57: Prints the menu text.

58: Prints the menu text.

59: Prints the menu text.

60: Prints the menu text.

61: Prints the menu text.

62: Prints the menu text.

63: Returns to calling program, in this case, main().

64: All functions end with a closing brace.

65: The asterisk comment helps separate functions from each other.

66: The definition line for the menu-answer function.

67: All functions begin with an opening brace.

68: A local variable is defined for the function's answer.

69: Gets the user's answer.

70: Throws away any garbage typed in after the first number.

71: Returns the answer to main()'s line 40.

72: All functions end with a closing brace.

73: The asterisk comment helps separate functions from each other.

74: The definition line for the property-displaying function. A loop in the body of this function prints all the property data.

75: The rest of the function's parameter list, neatly aligned.

76: All functions begin with an opening brace.

77: A for loop always needs a control variable.

78: Ensures that two decimal places print.

79: Always show the decimal point.

80: Guards against scientific notation.

81: Starts the counting through the property's parallel values.

82: Multistatement for loops need a compound statement.

83: Prints the first line of property-data output with the property code and price.

84: Line 83's cout concludes.

85: Prints the second line of property-data output with the property address.

86: Prints the third line of property-data output with the commission percentage.

87: The commission is stored as a decimal, so the percentage is multiplied by 100.0 to display the value as a decimal.

88: If three properties are on-screen, temporarily pauses the output to give the user a chance to read the screen's contents.

89: Multiple action if clauses need compound statements.

90: Tells the user how to proceed.

91: Waits for the user's keystroke.

92: Closes the if compound statement.

93: All for loops end with a closing brace.

94: When the list is finished displaying, gives the user another chance to read the screen's contents.

95: Waits for the user's keystroke.

96: Blank lines help separate parts of the program.

97: Returns to main()'s line 43.

98: All functions end with a closing brace.

99: The asterisk comment helps separate functions from each other.

100: The definition line for the property-searching function.

101: The parameter list continues.

102: All functions begin with an opening brace.

103: The for loop control variable.

104: Until a match is found, the found trigger variable will remain false.

105: Reserves a place for the user's search code (the key).

106: Place comments throughout your code.

107: Tells the user what is happening.

108: Prompts the user for the search code.

109: Gets no more than six characters from the user for the search code. Uses getline() to remove Enter keystroke.

110: Starts the loop that begins the property search.

111: The for loop starts with an opening brace.

112: Compares the user's search code to each code in the parallel arrays. strcmp() compares strings and returns 0 if they match.

113: The if statement needs a compound statement for multiple actions.

114: If the search code is found, starts printing the property data.

115: Continues printing the found property's data.

116: Continues printing the found property's data.

117: Continues printing the found property's data.

118: Continues printing the found property's data.

119: Continues printing the found property's data.

120: Sets the found variable to true because a match was made.

121: Stops the search because the match was found.

122: if tests end with a closing brace if opened with a brace.

123: for loops end with a closing brace if opened with a brace.

124: In case no match was made, prepares to apologize to the user.

125: The if statement can use braces even enclosing a single statement.

126: Prints the message telling the user no match was made.

127: Continues the message.

128: Ends the if with a closing brace.

129: Returns to main()'s line 45.

126: All functions end with a closing brace.



8: Prototype all your functions.

23: All data is assigned in advance.

30: Squeezing too much data on one line makes your programs harder to read.

39: The code in main() is kept simple.

55: Although this code is trivial, splitting into functions clarifies main().

66: Each function should perform a single task.

100: The user's property code will be asked for, and a search for that property will be made.

112: strcmp() tests strings for equality.


Previous Page Page Top TOC Next Page