Question

the source code I have is what i'm supposed to fix for the assignment at the...

the source code I have is what i'm supposed to fix for the assignment at the bottom.

cars.cpp source code:

/* Program Cars.cpp reads records from file and writes
them back to another file with the price member
increased by 10%.
*/
#include <fstream>
#include <iostream>
#include <string>
#include "car.h" // car.h header file is included here

using namespace std;

/* Reads data from a file and using it to populate a Car struct and then
return that Car struct
Pre: File dataIn has been openedand contains valid information in the format:
customer name, car make, price , month, date, year (date separated by
space)
Post: The fields of car are read from file dataIn.
*/
Car GetCar(ifstream& dataIn);
/* Writes the data from a Car struct out to a given file
Pre: File dataOut has been opened and contains valid data
Post: The fields of car are written to file dataOut,
appropriately labeled
*/
void WriteCar(ofstream& dataOut, Car car);
int main() {
Car car;
int carCount = 0; // Intialize car count to 0
ifstream dataIn;
ofstream dataOut;

dataIn.open("cars.dat");
dataOut.open("cars.out");
cout << fixed
<< showpoint; // Set numerical formatting to show decimal points

car = GetCar(dataIn); // Priming read to get first car from file

// Continue reading all data in file
while (dataIn) {
carCount++; // Increment car counter

car.price = car.price * 1.10; // Increase car price by 10%

WriteCar(dataOut, car); // Write out updated car data to output file

car = GetCar(dataIn); // Get next car from file
}
// Display number of cars read from file and written
cout << carCount << " cars processed.\n";

return 0;
}

// ********************************************************

Car GetCar(/*INOUT */ ifstream& dataIN) {
Car car; // Local variable to hold and return car info

// Read all data from file into car variable
// Changed in input to read first name and last name seperately using struct
// personType
dataIN >> car.customer.firstName >> car.customer.lastName >> car.make;
dataIN >> car.price >> car.purchased.day >> car.purchased.month >>
car.purchased.year;

// Return car with data from file
return car;
}

// **********************************************************

void WriteCar(/* INOUT */ ofstream& dataOut, /*IN*/ Car car) {
// Write out all data contained in car to output file
// Changed in output to write first name and last name
dataOut << "Customer: " << car.customer.firstName << " "
<< car.customer.lastName << endl
<< "Make: " << car.make << endl
<< "Price: " << car.price << endl
<< "Purchased: " << car.purchased.day << "/" << car.purchased.month
<< "/" << car.purchased.year << endl;
}

car.h code:

#ifndef CAR /* include guard to ensure that they are not inserted multiple times into a single .cpp file. */
#define CAR


// Structure to hold information about customer
struct personType {
string firstName;
string lastName;
};
// Structure to hold date information
struct Date {
int month;
int day;
int year;
};
// Structure to hold information on a car sold
struct Car {
personType customer;
string make;
float price;
Date purchased;
};

#endif

assignment details:

This one uses the same cars.cpp as a base, but has nothing to do with pointers. (That's why it's a separate exercise.) In this case you're going to modify the program so that rather than just printing data directly to the screen, it will also store the data in an array of type carArray, assuming no more than 50 cars.

Use the definitions:

Car carArray[50];
int carCount = 0;

Modify cars.cpp so that it will store each car read in the next element of the array.

Submit:

  • Updated cars.cpp with commented source code
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program code to copy:- (The changes made in cars.cpp is in bold)

//cars.cpp code

/* Program Cars.cpp reads records from file and writes
them back to another file with the price member
increased by 10% using array.
First data read from file will be stored in array and
the content of array is written into output file.
*/
#include <fstream>
#include <iostream>
#include <string>
#include "car.h" // car.h header file is included here

using namespace std;

/* Reads data from a file and using it to populate a Car struct and then
return that Car struct
Pre: File dataIn has been openedand contains valid information in the format:
customer name, car make, price , month, date, year (date separated by
space)
Post: The fields of car are read from file dataIn.
*/
Car GetCar(ifstream& dataIn);
/* Writes the data from a Car struct out to a given file
Pre: File dataOut has been opened and contains valid data
Post: The fields of car are written to file dataOut,
appropriately labeled
*/
void WriteCar(ofstream& dataOut, Car car[], int carCount);
int main() {
   //Definition of array of type Car
   Car car[50];

   int carCount = 0; // Intialize car count to 0
  
   ifstream dataIn;
   ofstream dataOut;

   dataIn.open("cars.dat");
   dataOut.open("cars.out");
   cout << fixed
   << showpoint; // Set numerical formatting to show decimal points

   // Priming read to get first car from file and
   //storing into array
   car[carCount] = GetCar(dataIn);

   // Continue reading all data in file
   //and storing into array of type Car
   while (dataIn) {
       carCount++; // Increment car counter

   //Storing price into array
   car[carCount].price = car[carCount].price * 1.10; // Increase car price by 10%

   // Get next car from file and storing into array
   car[carCount] = GetCar(dataIn); // Get next car from file

}
   // Write out updated car data to output file
   //Function will be called only once as all the data read
   //from file is stored in array.
   //Array and number of car count is passed as a paramenter
   WriteCar(dataOut, car, carCount);

   // Display number of cars read from file and written
   cout << carCount << " cars processed.\n";

   return 0;
}

// ********************************************************

Car GetCar(/*INOUT */ ifstream& dataIN) {
   Car car; // Local variable to hold and return car info

   // Read all data from file into car variable
   // Changed in input to read first name and last name seperately using struct
   // personType
   dataIN >> car.customer.firstName >> car.customer.lastName >> car.make;
   dataIN >> car.price >> car.purchased.day >> car.purchased.month >>
   car.purchased.year;

   // Return car with data from file
   return car;
}

// **********************************************************
//Function receives output file stream, array of type Car and
//car count as parameter
void WriteCar(/* INOUT */ ofstream& dataOut, /*IN*/ Car car[], int carCount) {
   // Write out all data contained in car array to output file
   // Changed in output to write first name and last name
   for(int i=0; i<carCount; i++) {
       dataOut << "Customer: " << car[i].customer.firstName << " "
   << car[i].customer.lastName << endl
   << "Make: " << car[i].make << endl
   << "Price: " << car[i].price << endl
   << "Purchased: " << car[i].purchased.day << "/" << car[i].purchased.month
   << "/" << car[i].purchased.year << endl;
   }  
}

//car.h code

#include <iostream>
using namespace std;
#ifndef CAR /* include guard to ensure that they are not inserted multiple times into a single .cpp file. */
#define CAR


// Structure to hold information about customer
struct personType {
string firstName;
string lastName;
};
// Structure to hold date information
struct Date {
int month;
int day;
int year;
};
// Structure to hold information on a car sold
struct Car {
personType customer;
string make;
float price;
Date purchased;
};

#endif

Screenshot of output:-

Screenshot of content of file names "cars.out" created after the execution of program :-

Before execution of above program code you need to create a input file named "cars.dat" .

Sample of file "cars.dat":-

Ryan Hilliard CHEVROLET 25000 15 10 2014
Kyle Jiwani FORD 31000 17 9 2016
Edward Maun TOYOTA 20000 20 11 2017
Hannah Shrestha VOLKSWAGEN 29000 21 5 2018

Add a comment
Know the answer?
Add Answer to:
the source code I have is what i'm supposed to fix for the assignment at the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need to make a few changes to this C++ program,first of all it should read...

    I need to make a few changes to this C++ program,first of all it should read the file from the computer without asking the user for the name of it.The name of the file is MichaelJordan.dat, second of all it should print ,3 highest frequencies are: 3 words that occure the most.everything else is good. #include <iostream> #include <map> #include <string> #include <cctype> #include <fstream> #include <iomanip> using namespace std; void addWord(map<std::string,int> &words,string s); void readFile(string infile,map<std::string,int> &words); void display(map<std::string,int>...

  • I need to add something to this C++ program.Additionally I want it to remove 10 words...

    I need to add something to this C++ program.Additionally I want it to remove 10 words from the printing list (Ancient,Europe,Asia,America,North,South,West ,East,Arctica,Greenland) #include <iostream> #include <map> #include <string> #include <cctype> #include <fstream> #include <iomanip> using namespace std; void addWord(map<std::string,int> &words,string s); void readFile(string infile,map<std::string,int> &words); void display(map<std::string,int> words);    int main() { // Declaring variables std::map<std::string,int> words;       //defines an input stream for the data file ifstream dataIn;    string infile; cout<<"Please enter a File Name :"; cin>>infile; readFile(infile,words);...

  • /* BEGIN - DO NOT EDIT CODE */ // File: main.cpp #include "LoginAccount.h" #include "RegisteredLoginAccount.h" #include...

    /* BEGIN - DO NOT EDIT CODE */ // File: main.cpp #include "LoginAccount.h" #include "RegisteredLoginAccount.h" #include "GuestLoginAccount.h" #include <iostream> #include <fstream> #include <string> #include <sstream> using namespace std; //prototypes and constants const int ARRAY_SIZE = 20; using number_of_records_t = int; //Function Declarations. void readAccountsFromFile(ifstream& fin, RegisteredLoginAccount registeredAccounts[], number_of_records_t& registeredLength, GuestLoginAccount guestAccounts[], number_of_records_t& guestLength); bool openFileForInput(ifstream& ifs, const string& filename); bool openFileForOutput(ofstream& ofs, const string& filename); void writeAccountsToFile(ofstream& ofs, RegisteredLoginAccount registeredAccounts[], const number_of_records_t registeredLength, GuestLoginAccount guestAccounts[], const number_of_records_t guestLength); void writeAccountToFile(ofstream&...

  • my code deosn't run and it doesn't show any error. what's wrong about it !! it...

    my code deosn't run and it doesn't show any error. what's wrong about it !! it should loads data from a text file into an array of structs and prints the array to the screen here is the code: #include <iostream> #include <string> #include <fstream> using namespace std; struct Company {    string Departmentname;    int Departmentnum; }; const int MAX = 20; int main() { ifstream File;    Company arrayofdepartment[MAX];    int Departmentcount ;      File.open("comapny.txt");       if...

  • How do I complete my code using an external file? External file: data.txt //the number 6...

    How do I complete my code using an external file? External file: data.txt //the number 6 is in the external file Incomplete code: #include <iostream> #include <fstream> using namespace std; class example {    int data[1]; public:    example();    void read(ifstream &); }; example::example() {    ifstream infile;    infile.open("data.txt");    } void example::read(ifstream &infile) {    infile >> data[1];    cout << data[1] << endl;    } int main() {    example example, read; //system("pause");    return 0;...

  • I need help with this exercise. Than you for the help. Language is C++ 2 Enumeration...

    I need help with this exercise. Than you for the help. Language is C++ 2 Enumeration Data Types Reformat is the shell of a program designed to read characters and process them in the following way Lowercase character Uppercase character Digit Blank Newline Any other character Converts to uppercase and writes the character Writes the character Writes the digit Writes a blank Writes newline Does nothing / Program Reformat reads characters from file DataIn and // writes them to DataOut...

  • The 4th deliverable is to create the program the makes the buy recommendation. It uses the...

    The 4th deliverable is to create the program the makes the buy recommendation. It uses the class Stock to store and retrieve the stock information. I need to make this program compatible with my stocks class. Program I need to change: #include <iostream> #include <cmath> #include <fstream> #include <iomanip> #include <string> #include <stdlib.h> using namespace std; // read the data file, store in arrays int openDatafile(ifstream&,string [], float[], float[], int[]); // opens the data file void readData(ifstream &, string[], float[],...

  • This is assignment and code from this site but it will not compile....can you help? home...

    This is assignment and code from this site but it will not compile....can you help? home / study / engineering / computer science / computer science questions and answers / c++ this assignment requires several classes which interact with each other. two class aggregations ... Question: C++ This assignment requires several classes which interact with each other. Two class aggregatio... (1 bookmark) C++ This assignment requires several classes which interact with each other. Two class aggregations are formed. The program...

  • I need to get this two last parts of my project done by tonight. If you...

    I need to get this two last parts of my project done by tonight. If you see something wrong with the current code feel free to fix it. Thank you! Project 3 Description In this project, use project 1 and 2 as a starting point and complete the following tasks. Create a C++ project for a daycare. In this project, create a class called child which is defined as follows: private members: string First name string Last name integer Child...

  • Can someone help me put the string removee the return to an outfile and fix?? prompt: CS 575 LabEx14: no e’s Let the user specify an input file and an output file (text files). Read the input file and...

    Can someone help me put the string removee the return to an outfile and fix?? prompt: CS 575 LabEx14: no e’s Let the user specify an input file and an output file (text files). Read the input file and write the output file; the output file should be the same as the input file, except that it has no e’s. For example, if the input file had the line. Streets meet in the deep. Then the output file would have...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT