SavitchChap10 - Savitch - CS 150 Savitch - CS 150 11...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Savitch - CS 150 Savitch - CS 150 11 Structured Data A major problem with modular code is the need to use a large number of parameters to deal with several objects that might be closely related to each other. A preferable approach would be to group these values together in a single structure, which could easily be passed to a function as a single parameter. void processSale (string inventoryNbr, float price, float weight, int &nbrInStock, int &nbrSoldThisYear, int storeNbr, int month, int day, int year, float time); void processSale (Merchandise &itemSold, Time timeOfSale); Savitch - CS 150 Savitch - CS 150 22 The C++ struct C++ provides a struct mechanism for defining such structures. struct Time { int month; int day; int year; float timeOfDay; }; struct Merchandise { string inventoryNbr; float price; float weight; int nbrInStock; int nbrSoldThisYear; int storeNbr; }; Savitch - CS 150 Savitch - CS 150 33 Structure Variables and Parameters Once a struct has been defined, variables of that type can be declared. void processSale (Merchandise &itemSold, Time timeOfSale); Time calculateStoreClosing(int monthNo, int dayNo, int yearNo); void main() { closingTime = calculateStoreClosing(12, 24, 1998); processSale(saleItem, closingTime); } const float EmployeeDiscountPercent; int dailyQuota; Merchandise saleItem, itemSold; Time timeOfSale, openingTime, closingTime; And parameters and functions of that type can be used. Savitch - CS 150 Savitch - CS 150 44 Working with the Structure Members The data elements comprising a struct are known as its members . The members are accessed by means of the C++ dot notation. Time calculateStoreClosing(int monthNo, int dayNo, int yearNo) { Time t; t.month = monthNo; t.day = dayNo; t.year = yearNo; if ((monthNo == 12) && (dayNo == 24)) t.timeOfDay = 18.00F; else t.timeOfDay = 22.00F; return t; } void processSale (Merchandise &itemSold, Time timeOfSale); { if (timeOfSale.timeOfDay < 8.00) itemSold.price -= EmployeeDiscountPercent*itemSold.price; itemSold.nbrInStock--; itemSold.nbrSoldThisYear++; } Savitch - CS 150 Savitch - CS 150 55 A Complete Example ///////////////////////////////////////////////// // // priceTag.cpp // // This program generates the text for a store's // // price tags, using a predefined output format. // ///////////////////////////////////////////////// // #include <iostream> #include <iomanip> #include <stdlib> #include <string> using namespace std; const int TAG_HEIGHT = 7; const int TAG_WIDTH = 20; const int INV_NBR_SIZE = 10; const int PRICE_TEXT_SIZE = 5; const char BORDER_CHAR = '*'; struct PriceTag { string inventoryNumber; float price; int storeNumber; }; // Notice the REQUIRED semicolon!...
View Full Document

Page1 / 30

SavitchChap10 - Savitch - CS 150 Savitch - CS 150 11...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online