• Home
  • Blog
  • Lab9: Write a program in C++ that updates data from a dataset

Lab9: Write a program in C++ that updates data from a dataset

0 comments

Review

Simple Data Types

The built-in, simple data types in C++ are integral types, enumeration types, and floating types. The integral types are char, short, int, long, and bool. The floating types are float, double, and long double. All of these data types are called simple or atomic because they are composed of single, indivisible values. The range of values that can be stored in a simple, integral data type depends on the number of bytes of storage assigned to the data type. C++ provides a unary operator, sizeof, that returns the number of bytes in its operand. C++ does not specify the number of bytes for any data type except char, which is defined to be one byte.

Recall that a data type is made up of a set of values and a set of allowable operations on those values. The operations on numeric data types are the arithmetic operators and the relational operators. The operations on Boolean values are the relational operators and the Boolean operators.

Enumeration Types

An enumeration type is a type in which the constant identifiers (called enumerators) are explicitly listed. For example, the code below defines two user-defined enum types BirdType and LetterGradeType.

enum BirdType {BLUEJAY, CARDINAL, ROBIN, SEAGULL, SWALLOW};
enum LetterGradeType {A, B, C, D, F};

These enum types are equivalent to the following 10 constant declarations.

const int BLUEJAY = 0;
const int CARDINAL = 1;
const int ROBIN = 2;
const int SEAGULL = 3;
const int SWALLOW = 4;

const int A = 0;
const int B = 1;
const int C = 2;
const int D = 3;
const int F = 4;

Enum types are used primarily to make computer programs more understandable to human readers.

Here is an example:

BirdType myBird;
myBird = ROBIN;

myBird is a variable of type BirdType that can contain any of the enumerators (constants) listed in enum data type BirdType.

Here is another example:

LetterGradeType yourGrade;
yourGrade = A;
if (yourGrade == B)
points = 3.0;

In this example, yourGrade can contain any of the enumerators listed in data type LetterGradeType. The enumerators of an enumeration type can be used in a program just like any other constant.

Note that the variable yourGrade does not contain the character A. Rather, yourGrade contains a value of the data type LetterGradeType, the enumerator A. The enumerator A is represented internally by the integer value 0. For obvious reasons, enumeration types are called user-defined data types.

Enumerators are ordered by the way in which they are listed (starting at 0). In a relational expression, enumerators are evaluated exactly as characters would be: whether one enumerator come before the other in the ordering of the enumerators.

Stream I/O is not defined for enumeration types. Printing out the values of an enumeration type must be done by converting the value from the enumeration type into a string that corresponds to the enumerator.

Since enum types are integral types, these variables can be used in switch statements. Examine the switch statement below:

switch (aBird)
{
case BLUEJAY : cout << “BlueJay”;
break;
case CARDINAL: cout << “Cardinal”;
break;

}

Values of an enumeration type cannot be read; they must be set in the program. However, the name of the enumeration type can be used to convert, or type cast, a number representing the position of an enumerator in the listing of the data type into the enumerator in that position. For example,

myBird = BirdType(0);

stores BLUEJAY into variable myBird because, like other data types in C++, the name (identifier) for the type can be used to cast the integer value (0) to the first of the enumerators. You can use this technique to input values of enumeration types. The user can be given a menu showing the enumerators and asked to key in the number representing the enumerator they wish to input. The number is read and type cast into the enumerator.

If you create an enumeration type that you might want to use again, you can store the definition in a file with the specification extension and use #include to access the file. Rather than putting the file name in angled brackets, you put it in double quotes. This formatting tells the preprocessor to look for the file in your current directory.

Type coercion is defined from an enum type to an int type. S above, you can use type conversion (type casting) to change an integer value into an enumerator.

For another example, look at the following statement:

myBird = BirdType(myBird + 1);

On the right-hand side of the assignment operator (=), myBird is coerced to int and 1 is added. However, the result must be type cast back to the enumeration type to be stored in a variable of BirdType.

Problem 1

One night you receive a call from your uncle who owns the APEX USED CAR COMPANY. He asks you to write a program that will modify a file containing information about the cars that your uncle is getting ready to sell this month from his used car lot. Your uncle wants your program to allow him to increase the price on some cars and decrease the price on other cars.

The data file that he wants modified is presented below:

Tiny Tim 55000 1 1 1985 F
Mary Murphy 12500 2 7 1995 N
Bear Bare 44444 9 6 1990 L
Sally Sale 7500 6 3 1970 M
Betty Bye 18888 4 8 1988 C
Alice Alas 23005 6 6 1992 F
Randy Law 12098 1 4 2009 A
Carl Lane 45000 12 3 2003 P
Janis Smith 23450 7 11 2001 V
George Lee 14560 4 4 1999 H

The file contains the first and last names of the customers along with the quoted cost of the car, the day month and year of pickup, and the make of the car using the codes below:

F – Ford

L – Lexus

N – Nissan

M – Mercedes

H – Honda

A – Audi

C – Chrysler

V – Volvo

P – Porche

Requirements

1) Your program should follow the structure chart below:

xid 9140878 1

To save you time, here are the required structs and function prototypes that you should use:

enum CarMakeType {Audi, Chrysler, Ford, Nissan, Mercedes, Honda, Volvo, Porsche, Lexus}; //Make enumerated list of cars that company sells.

struct DateType { //Make a date struct that contains information about day, month, and year.
int day; //Declare struct variables (ALL INT).
int month;
int year;
};
struct CarType{ //Make struct that contains information about the customer, delivery date of the car, cost, and car model.
string fname; //Declare struct variables.
string lname;
float cost;
DateType date_delivery;
CarMakeType car_make;
};

CarType Get_Car_data(ifstream&); //Function Prototypes
void Lookup_Car_data(char, CarType[]);
void Modify_Car_data(float, int, CarType[]);
void Output_car_data(CarType[], ofstream&);
void Print_Car_report(CarType[]);
void Write_Car_Output(CarType[], ofstream&);

2) You should define an enum type for the “CarMakeType” with values {Audi, Chrysler, Ford, …}. Use this definition at the beginning of your program

enum CarMakeType {Audi, Chrysler, Ford, Nissan, Mercedes, Honda, Volvo, Porsche, Lexus};

3) You should define a struct named “DateType” with data members day, month, and year

4) Your program must declare a user-defined struct named “CarType“. This type should have data members for the first and last name of the customer, the cost of the car, the date of delivery (using the DateType struct) and a enum value using the CarMakeType enum type.

5) The main function should use an end-of-file loop to call the Get_Car_data function and use an array for CarType structs to store each of the Car structs that are returned.

6) The Get_Car_data function reads one set of car data from the input file, stores the data in a local CarType struct and returns this struct to main.

7) After the input file has been read, the main function should use a do while loop (with a sentinel value of “N”) to prompt the user for the make of the car to modify. After the user enters the make of the car, the Lookup_Car_data function is called.

8) The Lookup_Car_data function should find the first car of this make in the Car array and display the car data to the user and prompt the user for the desired discount. Then this function should call the Modify_Car_data function with the discount percentage, the integer index of the selected car, and the Car array.

9) The Modify_Car_data function should apply the discount to the price data member of the Car struct in the Car array for the selected car as indicated by the Car index parameter.

10) In the main function, once the user enters “N” indicating that they are finished modifying car data, the Output_car_data function should be called with the output file and the Car array

11) The Output_car_data function should call the Print_Car_Report function with the Car array. Next, the Write_Car_Output is called with the output file and the Car array.

12) The Write_Car_output function loops through the data in the Car array and outputs the car data to lines of the output file.

13) The Print_Car_Report function prints a heading and then loops through the data in the Car array and displays the car data to lines of the console window.

Your program output should look like:

xid 9140879 1

About the Author

Follow me


{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}