LAB 1b

C++ Structs and Array of Struct

Learn to manipulate C++ structs
Provide practice in using arrays of structs

In this laboratory assignment we will consider the following:

A. Using C++ Structures
B. Array of Structs

A. Using C++ Structures

In reviewing our previous knowledge of simple data types, we remember that variables of these types have a major restriction: these variables may only store one data item. For example, if the following declaration is made:

  int oneNumber;  

oneNumber may only store one integer value. To store multiple data items of the same type, we have the array structured data type. For example,

  const unsigned int MAX_VALUES = 50;
 int manyNumbers[MAX_VALUES];  


will set up an array called manyNumbers which will have the ability to store as many as 50 integers. One major disadvantage of the simple data type and the array structure is the fact that each type only stores one data type. For example, the manyNumbers array can store only integers and the oneNumber variable may store only an integer. There are many applications where it would be beneficial to have one variable which could store several types of information. C++ provides the structure data type to satisfy this need. The C++ struct is a group of data items which do not necessarily have the same data type. The struct should be used whenever the programmer wishes to store related information into one variable name. For example, the following examples need structures because they each require the storage of more than one piece of information (all require more than one field):

  • a variable to store information related to a movie owned by a video store which will store the name of the movie, the number of copies owned by the store, and the type of movie

  • a variable to store information concerning a customer of the video store including the name of a person renting a movie, the person's id number, name of a movie rented, and the return date of the movie rental.

Declaring Structs

The syntax for the C++ struct type follows:

  struct StructName  
  {  
       list of member types and member names;  
  };  


Exercise 1:

Copy the files lab1.cpp, customers.dat, videos.dat. lab1.cpp is a short program that reads data from videos.dat, places it into an array of video records, and then allows the contents to be printed. This program displays a menu so that the user can select one of the following options:

1) Print a list of all videos owned by the Movie Rent Video Store

2) Print a list of all customers

3) Print out a list of rental information by video titles

0) Exit this program


Presently, this program reads inventory information from a file called videos.dat which contains the following information for each video owned by the company: name of video, the number of copies of a particular title, and the video type (mystery, comedy, etc.). The program, as it is given, will allow the user to choose option 1) or option 0) only. We will be adding code to this file so that the other options are eventually added to the code. Compile and execute this program to make sure that you understand what the program is doing.


In observing our file, we realize that we need a struct that will store the following information for each video: name of the video, the number of copies of the video owned by the video store and the type of video. The variable, videoInfo, to declare a video struct of this type follows:

  struct Video  
  {  
       string movieTitle;                     //name of movie  
       unsigned int numberCopies;    //number of copies  
       string videoType;                     //type of video  
  };  
  Video videoInfo;  

NOTE that Video is a TYPE not a variable!!! Thus, we must declare a variable to be of type video for memory locations to be associated with the member elements. Also, note that the variable name for this struct is videoInfo!!

The items included in the C++ struct are called members of the struct and we will learn later that structs can have data members (as shown above) and/or function members.

As stated earlier, we need to add code to the program so that it will also evaluate customers and print out a list of video customer information. Let's now observe the file. This file contains customer information for the video store. All data in this file is in the following form for each customer:

name of customer

(a string)

id number of customer

(integer)

name of video rented by the customer

(a string)

return date of the video

(a string)


Exercise 2:

Using the above struct declaration as a guide, show code to set up a struct called CheckOut and a variable called customer which will store a customer's name, the customer's id number (integer), the title of the movie/video rented, and when the video is to be returned. Insert the code for this declaration into the position indicated by comments in the lab1.cpp program.


Manipulating C++ Structs

The dot operator, "." , is used when accessing structs. It is preceded by the struct variable name and is followed by the name of a member of that struct. Hence, the "." operator is often called a membership operator. The following code will read from the stream videoIn and print values for the variable videoInfo as declared above.

  //get the video information 
  getline(videoIn, videoInfo.movieTitle); 
  myin >> videoInfo.numberCopies; 
  myin.get(endOfLine); 
  getline(videoIn, videoInfo.videoType);  
  //print out video information 
  cout << "\n\nName of video:\t\t"  
      << videoInfo.movieTitle << endl;  
  cout << "Number of copies:\t"  
      << videoInfo.numberCopies << endl;  
  cout << "Video type:\t\t" 
      << videoInfo.videoType << endl;  

Exercise 3:

Add code to read values for the variable customer described in exercise 4. This is accomplished by a call to the function getCustomer(). Complete the function getCustomer() so that it will read values for one customer from the indicated input file stream. Include this code at the end of the lab1.cpp program.


Other manipulations which might occur using structs will be addressed by example.

How are structs passed to functions?

This question might best be answered by showing the complete program to process one video.

  #include <fstream> 
  using namespace std; 
  //set up global struct type 
  struct Video 
  { 
      string movieTitle;          //name of movie 
      unsigned int numberCopies;  //number copies owned 
      string videoType;           //type of video 
  }; 
  //function prototyes 
  void getVideo(Video&, ifstream&);   //get one video 
  int main() 
  { 
      //variable declarations 
      Video videoInfo;             //one video 
      //declare and open the file containing  
      //video information 
      ifstream videoIn; 
      videoIn.open("videos.dat"); 
      //read from the file until eof is reached 
      getVideo(videoInfo, videoIn);  
      while (myin) 
          getVideo(videoInfo, videoIn); 
      return 0; 
  } //end of main 
  //Function to get one movie's information 
  void getVideo (Video& oneVideo,         
                ifstream & videoIn)      
  { 
      char endOfLine; 
      //get the video information 
      getline(videoIn, videoInfo.movieTitle); 
      videoIn >> videoInfo.numberCopies; 
      videoIn.get(endOfLine); 
      getline(videoIn, videoInfo.videoType); 
  }  

Can assignment statements be used between two struct variables of the same type?

YES!!! Note the following function which swaps two structs of type Video.

  void swap (Video& one, Video& two) 
  { 
      Video temp; 
      temp = one; 
      one = two; 
      two = temp; 
  }  

C. Array of structs

A more common application of structs occurs when multiple structs of the same type are needed in a program. Suppose that we need to write a program which performs manipulations on as many as 100 videos. The data structure that would be needed for this application is an array of structs.

Defining an Array of Videos

Remember that when working with structs, one of the first things that is accomplished is the creation of a struct type. This type is used to declare an array of this new type. For example, the following code defines a variable called allVideos which will store information for as many as 100 videos.

  const unsigned int MAX_VIDEOS = 100; 
  Video allVideos[MAX_VIDEOS];  

Exercise 4:

Add code to the program to define a variable called allCustomers which will store information contained in the customers.dat file for as many as 20 customers as described in Exercise 5.


Manipulation of Arrays of Structs

Data included in an array of structs is accessed by denoting the following in the order specified: 1) indicate array variable name 2) indicate which struct to be accessed (show the subscript) 3) indicate which member of the struct that is to be accessed.

For example, the following:

  cout << allVideos[0].movieTitle << endl;  


will print out the movie title of the first video included in the list of rental videos.


Exercise 5:

Write code to print out the id number of the second customer included in the allCustomers array as described in exercise 6. Place your answer on the answer sheet.


Passing an Array of Structs to a Function

Suppose that we need a function called findAndPrint which will search the allVideos array for a particular type of video and will print out all videos included in the list which have this type. The function should receive the allVideos array so that it can be searched and secondly, the function should receive the type of videos that it is searching for. The function header definition will appear as:

void findAndPrint (Video shows[], string showType)

and we can call this function using the following statement:

findAndPrint(allVideos, "comedy");

Exercise 6:

Show function header definition and body for a function called loadCustomers which is passed allCustomers, and a open file stream (which you will open at the start of the program), and will load all of the customer records into the array of customer records.

Exercise 7:

Show function header definition and body for a function called printRentalInfo which will receive the allCustomers array defined in Exercise 6, a movie title, and the number of customers and will print out a list of all the names of customers who have rented the video indicated in the movie title sent to the function.


Exercise 8:

Now complete the Movie Rent Video Store program. Insert a function called printCustomer which will receive one struct of type CheckOut and will print out all information related to this customer (name, id, name of video rented, and return date of video).


Exercise 9:

Remove comments from the program so that all menu selections can be selected by the user. Compile and execute this program showing that all options in the menu are operational. Turn in a script containing a listing of the program, a compile of the program and a run of the program for grading.

Topic attachments
I Attachment Action Size Date Who Comment
Datdat customers.dat manage 0.7 K 2011-02-21 - 15:51 JimSkon Customers data file
Cppcpp lab1.cpp manage 2.7 K 2011-02-21 - 16:00 JimSkon Lab 1 demo
Datdat videos.dat manage 0.3 K 2011-02-21 - 15:51 JimSkon Video Data
Topic revision: r2 - 2011-02-25 - JimSkon
 
This site is powered by the TWiki collaboration platformCopyright &© by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback