SavitchChap10 - Structured Data A major problem with...

Info icon This preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
 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);
Image of page 1

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

View Full Document Right Arrow Icon
 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; };
Image of page 2
 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.
Image of page 3

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

View Full Document Right Arrow Icon
 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++; }
Image of page 4
 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! int startSession(); char askUserForMore(); int howManyQuery(); PriceTag getTagInfo(int store); void outputTag(PriceTag tag); The PriceTag structure contains a character array, an integer, and a floating-point number. The closing brace after a structure definition must be followed by a semicolon! A function with type PriceTag. A PriceTag parameter, passed by value.
Image of page 5

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

View Full Document Right Arrow Icon
 Savitch -   CS 150 66 The PriceTag Example (Continued) ////////////////////////////////////////////////// // // The main function repeatedly asks the user for // // info regarding desired price tags, and then // // generates the text for the desired price tags. // ////////////////////////////////////////////////// // void main() { PriceTag pTag; int quantity; int i; int storeNo = startSession(); char YorN = askUserForMore(); while (YorN =='Y') { quantity = howManyQuery(); pTag = getTagInfo(storeNo);
Image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern