Question

In: Computer Science

Modify the program so that, rather than printing data directly to the screen, it will read...

Modify the program so that, rather than printing data directly to the screen, it will read the data into an array of car structs and then print out the contents of the array.

#include "car.h"
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>

Car GetCar(ifstream& dataIn);

// Pre:  File dataIn has been opened.

// Post: The fields of car are read from file dataIn.

void WriteCar(ofstream& dataOut, Car car);

// Pre:  File dataOut has been opened.

// Post: The fields of car are written on file dataOut,

//       appropriately labeled.  

int main ()

{

  Car  car;

  ifstream dataIn;

  ofstream dataOut;

  dataIn.open("cars.dat");

  dataOut.open("cars.out");

  cout << fixed << showpoint;

  car = GetCar(dataIn);

  while (dataIn)

  {

    car.price = car.price * 1.10f;

    WriteCar(dataOut, car);

    car = GetCar(dataIn);

  }

  return 0;

}

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

Car GetCar(ifstream&  dataIn)

{

  Car car;

  dataIn >> car.customer;

  dataIn >> car.price  >> car.purchased.day        

         >> car.purchased.month  >> car.purchased.year;

  dataIn.ignore(2, '\n');

  return car;

}

                                                            

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

void  WriteCar(ofstream&  dataOut, Car  car)

{

  dataOut << "Customer: " << car.customer << endl

    << "Price:    " << car.price << endl

    << "Purchased:"  << car.purchased.day << "/"

    << car.purchased.month << "/"

    << car.purchased.year << endl;

}

.h file

#include <string>

struct PersonType {
    std::string firstname;
    std::string lastname;
};

struct Date
{
  int month;
  int day;
  int year;
};

struct Car
{
  float price;
  Date purchased;
  PersonType customer;
};

Solutions

Expert Solution

Answer:

Here is the modified code :

#include "car.h

#include <iostream>

#include <iomanip>

#include <fstream>

#include <string>

Car GetCar(ifstream& dataIn);

// Pre: File dataIn has been opened. //

Post: The fields of car are read from file dataIn. void WriteCar(ofstream& dataOut, Car car);

// Pre: File dataOut has been opened. //

Post: The fields of car are written on file dataOut;

// appropriately labeled.

int main ()

{

Car car;

ifstream dataIn;

ofstream dataOut;

dataIn.open("cars.dat");

dataOut.open("cars.out");

cout << fixed << showpoint; car = GetCar(dataIn);

while (dataIn)

{

car.price = car.price * 1.10f;

WriteCar(dataOut, car);

car = GetCar(dataIn); } return 0;

}

//***************************************************** Car GetCar(ifstream& dataIn)

{

Car car;

Cout<<"enter range of i;

Cin>>I; For(i=0;i<i;i++)

{

dataIn >> car.customer[i];

dataIn >> car.price[i] >> car.purchased.day[i] >> car.purchased.month.[i] >> car.purchased.year[i];

}

dataIn.ignore(2, '\n');

return car;

}

//***************************************************** void WriteCar(ofstream& dataOut, Car car)

{

dataOut << "Customer: " << car.customer << endl << "Price: " << car.price << endl << "Purchased:" << car.purchased.day << "/" << car.purchased.month << "/" << car.purchased.year << endl;

}

.h file

#include <string>

struct PersonType

{

std::string firstname;

std::string lastname;

};

struct Date

{

int month;

int day;

int year;

};

struct Car

{

float price[50];

Date purchased[50];

PersonType customer[50];

};


Related Solutions

In Java. Modify the attached GameDriver2 to read characters in from a text file rather than...
In Java. Modify the attached GameDriver2 to read characters in from a text file rather than the user. You should use appropriate exception handling when reading from the file. The text file that you will read is provided. -------------------- Modify GaveDriver2.java -------------------- -----GameDriver2.java ----- import java.io.File; import java.util.ArrayList; import java.util.Scanner; /** * Class: GameDriver * * This class provides the main method for Homework 2 which reads in a * series of Wizards, Soldier and Civilian information from the user...
C++ 1. Modify this program to open the file "Customers.dat" so that all data is written...
C++ 1. Modify this program to open the file "Customers.dat" so that all data is written to the end of the file AND to allow the file to be read. 2. Create a method called "getLargestCustomerNumber" and call it after the "Customers.dat" file has been opened. Read all the existing customerNumbers and determine the largest customerNumber - do not assume the last record in the file is the largest number. Use this number as the base when adding new customer...
Rather than titrating the active ingredient in an antacid directly with a standard solution of a...
Rather than titrating the active ingredient in an antacid directly with a standard solution of a strong acid in our buret, in this experiement we first dissolve the antacid tablet in a measured excess of standard acid, and then titrate (with standard sodium hydroxide solution) the portion of the acid that was not consumed by the tablet. Explain.
Modify the Movie List 2D program -Modify the program so it contains four columns: name, year,...
Modify the Movie List 2D program -Modify the program so it contains four columns: name, year, price and rating (G,PG,R…) -Enhance the program so it provides a find by rating function that lists all of the movies that have a specified rating def list(movie_list): if len(movie_list) == 0: print("There are no movies in the list.\n") return else: i = 1 for row in movie_list: print(str(i) + ". " + row[0] + " (" + str(row[1]) + ")") i += 1...
What is the output of the following program? Slightly modify the program so that: [Pts. 10]...
What is the output of the following program? Slightly modify the program so that: [Pts. 10] The Parent process calculates the val such a way that its value is 20 and it prints “This is parent process and val = 20”. Also, slightly modify The child process calculates the val such a way that its value is 25 and it prints “This is child process and val = 25”. int main() { int val = 15; int pid; if (pid...
"4. (Modify) Modify Program 7.14 so that the user inputs the initial set of numbers when...
"4. (Modify) Modify Program 7.14 so that the user inputs the initial set of numbers when the program runs. Have the program request the number of initial numbers to be entered." //C++ Program 7.14 as follows #include #include #include #include using namespace std; int main() { const int NUMELS = 4; int n[] = {136, 122, 109, 146}; int i; vector partnums(n, n + NUMELS); cout << "\nThe vector initially has the size of " << int(partnums.size()) << ",\n and...
C++ Modify FunctionTable.cpp so each that each function returns a string(instead of printing out a message)...
C++ Modify FunctionTable.cpp so each that each function returns a string(instead of printing out a message) and so that this value is printed inside of main(). //: C03:FunctionTable.cpp // Using an array of pointers to functions #include <iostream> using namespace std; // A macro to define dummy functions: #define DF(N) void N() { \ cout << "function " #N " called..." << endl; } DF(a); DF(b); DF(c); DF(d); DF(e); DF(f); DF(g); void (*func_table[])() = { a, b, c, d, e,...
Modify the Assignment program from Module 1 to read a user's first and last name read...
Modify the Assignment program from Module 1 to read a user's first and last name read three integer scores, calculate the total and average determine the letter grade based on the following criteria - Average 90-100 grade is A, 80-89.99 grade is B, 70-79.99 grade is C, all others F (use a nested if) Output formatted results consisting of the full name, the three scores, the total, average (to 2 decimal places), and the letter grade go to step 1...
Modify the AlienDirection program from this chapter so that the image is not allowed to move...
Modify the AlienDirection program from this chapter so that the image is not allowed to move out of the visible area of the window. Ignore any key that would allow this to happen. *Ask if you have any questions about the assignment I will try to clarify Textbook - JAVA FOUNDATIONS: INTRODUCTION TO PROGRAM DESIGN AND DATA STRUCTURES 5TH EDITION Starter Code Provided : import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.scene.input.KeyEvent; import javafx.scene.paint.Color; import javafx.stage.Stage;...
In c++, modify this program so that you allow the user to enter the min and...
In c++, modify this program so that you allow the user to enter the min and maximum values (In this case they cannot be defined as constants, why?). // This program demonstrates random numbers. #include <iostream> #include <cstdlib> // rand and srand #include <ctime> // For the time function using namespace std; int main() { // Get the system time. unsigned seed = time(0); // Seed the random number generator. srand(seed); // Display three random numbers. cout << rand() <<...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT