Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Lab 8 Manipulating Data Using a struct Question Answer (Circle the correct answe

ID: 3685668 • Letter: L

Question

Lab 8

Manipulating Data Using a struct

Question

Answer
(Circle the correct answer)

1.The order that struct members are defined does not affect the order in which they are accessed

a.     true

b.    false

2.A struct is accessed by the struct variable name, the name of the struct, and the struct member name

a.     true

b.    false

3.Given:

struct animal

{

bool mammalian;

string habitat;

int longevity;

};

animal cat;

Which of the following correctly initializes cat?

a.     mammalian = true;

habitat = “house”;

longevity = 12;

b.    cat.mammalian = true;

cat.habitat = “house”

cat.longevity = true;

c. animal.cat.mammalian = true;

animal.cat.habitat = “house”;

animal.cat.longevity = 12;

d. all the above may be used

4.Use the struct animal defined above and the statements:

animal cat;

animal dog;

Which of the following statements are valid in C++?

a.     cat = dog;

b.    cat.mammalian = dog.mammalian; cat.habitat = dog.habitat; cat.longivity = dog.longivity;

c.    both a and b

d.    false

            Classes and Data Abstraction

Question

Answer
(Circle the correct answer)

5.A class is

a.     A structured data type

b.    A homogenous data type

c.    A simple data type

6.All data members of a class must be of the same data type.

a.     true

b.    false

7.You define an object of a class in the same way that you define a variable of a data type.

a.     true

b.    false

8.It is optional to initialize an object at declaration.

a.     true

b.    false

9.A data member is another term for an object.

a.     true

b.    false

Critical Thinking Exercise: Create the design for a program for a car lot.   You will prompt the user for the specifications of a car and check your inventory to see if the car is on the car lot. The inventory is found in the file CarLot.dat. You will read in the inventory using the struct Cars defined in lab 11.1, exercise 5a. You will read data from an input file named CarLot.dat into a struct variable and the user’s specifications into a struct. Since you do not want to lose the customer, ask the customer if they want a list of all vehicles that match some specifications or only the exact car being searched.   Hint: Since you are only reading a record at a time and you loop until the end of file flag, if you need to go through the file again, you must clear all file flags using the clear function. The following specifications apply to the cars in the lot.

Valid car types include (C)oupe, (M)inivan, or (S)UV.

The coupe gets 25 mpg at a base cost of $15,000, and seats 4.

The minivan gets 20 mpg at a base cost of $18,000. and seats 7.

The SUV gets 18 mpg at a base cost of $22,000 and seats 5.

CarLot.txt

C 4 25 15000 green

C 4 25 15000 yellow

M 7 20 18000 silver

S 5 18 22000 blue

S 5 18 22000 silver

S 5 18 22000 green

C 4 25 15000 white

S 5 18 22000 black

// Enter your name as a comment for program identification

// Program assignment CarSales.cpp

// Enter your class section, and time

/* The program CarSales.cpp prompts the user for the specifications of a car and reads the file, CarLot.txt to see if those specifications are found in the car lot. The user is asked if an exact match is necessary or if he/she wants any car that has some of the specifications. The program will display a message if the car is found and if requested, will display a list of other cars that have some of the specifications. */

/* An input file CarLot.txt is used to enter the data and the user enters specifications. */

/* The sorted vector is displayed. */

//header files

/* use the correct preprocessor directives for input/output, strings, and file stream */

#include <string>

#include <fstream>

#include <iostream>

using namespace std;

// define a structure Cars with “type” char, “seating” int, “mpg” “price” double, “color” string

struct Cars

{

            char ______;

            int ________;

            double ___; //miles per gallon

            double _____;

            string _____;

};

// function prototypes

/* The function instruct describes the use    and purpose of the program. */

void instruct();

/* The function getSpecifications prompts   the user for car specifications. */

bool getSpecifications (Cars &customer);

/* The function openFile opens the   data file */

void openFile(ifstream &);

/* The function inputSeating prompts the user for the seating requirement. */

bool inputSeating (Cars &customer);

/* The function checkPrice prompts    the user for the price. */

bool checkPrice(Cars &customer);

/* The function exactMatch reads the cars from    the data file and looks for an exact match. */

char exactMatch (ifstream &inData, Cars &customer);

/* The function closeMatch reads the cars from the data file and looks for a close match. */

char closeMatch (ifstream &inData, Cars &customer);

int main()

{

            // declare variables

            /* a Cars variable customer to enter the customers car specifications, an ifstream variable inData to read in the data file, and a Boolean variable exact to specify if an exact match is to be found */

            Cars …………..;//the variable name is customer

            ifstream …………….;the variable name is inData

            bool ……………;the variable name is exact

            char …………….. = 'Y';the variable name is answer

            // call the function instruct

            instruct();

            // loop until the user quits. Use variable answer

            while (…………. == 'Y')

            {

                        /* assign to exact the value returned from   getSpecifications */

                        __________ = _______________(customer);

                        // call the function openFile

                        openFile(inData);

                        // clear the end of file marker

                        inData.clear();

                       

                        // test for an “exact” match

                        if (_______)

                                    /* assign to answer the value returned from

                                       the function exactMatch */

                                    answer = exactMatch(inData, customer);

                        else

                        /* assign to answer the value returned from the function closeMatch */

                                    answer = closeMatch(inData, customer);

           

                        // close the input file

                        inData.close();

            }

            inData.close();

            return 0;

}

/* The function instruct describes the use and purpose of the program which prompts the user for specifications of a car. That is checked with CarLot file */

void instruct()

{

            cout << "…………………………………………… "

                        << "………………………………………… "

                        << "………………………………………… "

                        << "………………………………………… "

                        << "………………………………………… "

                       

}

/* The function getSpecifications prompts the user for car specifications. */

bool getSpecifications (Cars &customer)

{

            // declare variables

/* a Boolean variables goodType=false to validate   the type entered by the user and exact=true to see if the user requires an exact match,   a character variable answer to hold the user's   answer */

            bool ____________ = false, ______ = true;

            char ________;

            // loop until the user enters a valid type (use goodType)

            while (!goodType)

            {          

                        // prompt the user for the type of car wanted

                        cout << " Enter type of car:(C)oupe, (M)inivan, or (S)UV: ";

                        // input the type (struct)

                        cin >> ……………….;

                        // convert the type to uppercase

                        customer.type = toupper(customer.type);

                        // validate the type entered for customer.type = 'C' or 'M' or 'S'

                        if (…………… == 'C' || ………………. == 'M' || …………….. == 'S')

                                    goodType = true;

                        else                 

                                    // display a message to the user to re enter data

                                    cout << "Your must select from the menu given. ";

                       

                        /* assign to goodType the value returned from a call to the function inputSeating */

                        if (goodType)

                                    goodType = inputSeating(customer);

                        /* assign to goodType the value returned from a call   to the function checkPrice */

                        if (goodType)

                                    goodType = checkPrice(customer);

            }

                        // prompt the user for the color selection

                        cout << "Enter the color of your choice: ";

                        // read the customer.color selection

                        cin >> ……………..;

                        // prompt the user for an exact or close match

                        cout << "Are you only interested in an exact match? (y)es/(n)o ";

                        // read the user's answer

                        cin >> answer;

                        // convert the answer to uppercase

                        answer = toupper(answer);

                        // toggle the exact switch

                        if (answer == 'N')

                                    exact = false;

                        return exact;

}

/* The function openFile opens the   data file */

void openFile(ifstream &inData)

{

            // open the file

            inData.open("CarLot.txt");

            // check to see if the file opened

            if (!inData)

            {

                        cout <<"Cannot open input file. "

                                    <<"The program terminates." << endl;

                        exit(1);

            }

}

/* The function inputSeating enters the   seating capacity of the car. */

bool inputSeating (Cars &customer)

{

            // declare variables

            /* a Boolean variable warning to as the user for   a new search */

            bool warning = true;

            // prompt the user for customer.seating capacity needed

            cout << "Enter the seating capacity that you want: ";

            // input the user's seating capacity (struct)

            cin >> ………………….;

            // check to see if the car chosen has the customer.seating capacity – use switch with customer.type

            switch (……………………..)

            {

            case 'C': if (……………………. > 4)

                                    {

                                                warning = false;

                                                cout << "Warning: Coupes only seat 4 passengers. ";

                                    }

                                    break;

            case 'S': if (…………………….. > 5)

                                    {

                                                warning = false;

                                                cout << "Warning: SUVs only seat 5 passengers. ";

                                    }

                                    break;

            case 'M': if (……………… > 7)

                                    {

                                                warning = false;

                                                cout << "Warning : Minivans only seat 7 passengers. ";

                                    }

            } // end of switch statement

/* if the car does not have the seating capacity instruct the user to make another selection */

            if (!warning)

                        cout << "Please make another selection. ";

            return warning;

}

/* The function checkPrice prompts    the user for the price. */

bool checkPrice(Cars &customer)

{

            // declare variables

/* a character variable answer to search again, a Boolean variable warning to allow the user to select a different car */

            char ………….;

            bool ………… = true;

            // Set the customer.seating, mpg, and price if car chosen. Use customer.type

            switch (……………..)

            {

            case 'C': …………………. = 4;

                                    customer.mpg = 25;

                                    customer.price = 15000;

                                    cout << "A coupe gets 25 mpg and has a base price of $15,000. ";

                              break;

            case 'M': ………………….. = 7;

                              customer.mpg = 20;

                                    customer.price = 18000;

                              cout << "A minivan gets 20 mpg and has a base price of $18,000. ";

                              break;

            case 'S': …………………….. = 5;

                              customer.mpg = 18;

                              customer.price = 22000;

                              cout << "An SUV gets 18 mpg and has a base price of$22,000. ";

            }

            // prompt the user to make another selection

            cout << "Do you want to change your selection? (y)es/(n)o: ";

            // input the user's answer

            cin >> answer;

            // convert the answer to uppercase

            answer = toupper(answer);

            // if user chooses, set warning for another selection

            if (answer == 'Y')

                        warning = false;

            return warning;

}

char exactMatch (ifstream & inData, Cars &customer)

{

            // declare variables

            /* a Cars variable carLot to read from the data file, a Boolean variable found for displaying the description line, and a character variable answer to see if the user wants to continue the program. */

            bool …….. = false;

            char ……… = 'N';

            Cars carLot;

            // loop until the end of file

            while (!inData.eof())   //eof() end of file function

            {

                        // read in the first file in the record

                        inData >> carLot.type >> carLot.seating

                                       >> carLot.mpg

                                       >> carLot.price

                                       >> carLot.color;

                        // check to see if the car type and color is a match

                        if (carLot._______ == customer._______

                                    && carLot._______== customer._______)

                        {

                                    // toggle the found switch

                                    found = true;

                                    break;

                        }

            }

            // display a message that the exact car was found

            if (found)

                        cout << " Congratulations. We have the car you want in stock. ";

            else

            {

                        // display a message that the exact car was not found

                        // prompt the user for another search

                        cout << " I'm sorry, we do not have the exact car you want. "

                             << "Do you want to try another selection? (y)es/(n)o ";

                        // read in the user's answer

                        cin >> answer;

                        // convert the answer to uppercase

                        answer = toupper(answer);

            }

            return answer;

}

/* The function closeMatch reads the cars from the data file and looks for a close match. */

char closeMatch (ifstream & inData, Cars &customer)

{

            // declare variables

            /* a Cars variable carLot to read from the data file,   a Boolean variable first for displaying the   description line, and a character variable answer   to see if the user wants to continue the program. */

            Cars carLot;

            bool first = true;

            char answer;

            // continue the loop until the end of file

            while (!inData.eof())

            {

                        // read in a file record

                        inData >> carLot.type >> carLot.seating

                                       >> carLot.mpg

                                       >> carLot.price

                                       >> carLot.color;

                        // see if a similar car is available

                        if (carLot.type == customer.type

                                    || carLot.color == customer.color)

                        {

                                    // check if this is the first time in loop

                                    if (first)

                                    {

                                                            //describe the output message

                                                cout << " We have the following cars that you might "

                                                            << "want to consider. ";

                                                // toggle the first switch

                                                first = false;

                                    }

                                    // display the a possible car match

                                    cout << "A " << carLot.color;

                                    switch (carLot.type)

                                    {

                                                case 'C': cout << " coupe. ";

                                                                  break;

                                                case 'M': cout << " minivan. ";

                                                                        break;

                                                case 'S': cout << " SUV. ";

                                    }

                        }

            }

            // display a message that no car was found

            // prompt the user to start another search

            if (first)

            {

                        cout << " I'm sorry, we do not have a car you want. "

                             << "Do you want to try another selection? (y)es/(n)o ";

                        // input the user's answer

                        cin >> answer;

                        // convert the answer to uppercase

                        answer = toupper(answer);

            }

            return answer;

}

Question

Answer
(Circle the correct answer)

1.The order that struct members are defined does not affect the order in which they are accessed

a.     true

b.    false

2.A struct is accessed by the struct variable name, the name of the struct, and the struct member name

a.     true

b.    false

3.Given:

struct animal

{

bool mammalian;

string habitat;

int longevity;

};

animal cat;

Which of the following correctly initializes cat?

a.     mammalian = true;

habitat = “house”;

longevity = 12;

b.    cat.mammalian = true;

cat.habitat = “house”

cat.longevity = true;

c. animal.cat.mammalian = true;

animal.cat.habitat = “house”;

animal.cat.longevity = 12;

d. all the above may be used

4.Use the struct animal defined above and the statements:

animal cat;

animal dog;

Which of the following statements are valid in C++?

a.     cat = dog;

b.    cat.mammalian = dog.mammalian; cat.habitat = dog.habitat; cat.longivity = dog.longivity;

c.    both a and b

d.    false

Explanation / Answer

By chegg guideline poilicy I am answering 1st question only