2. destination = d;
}
};
void input(string &reportingMark, int &carNumber, string&
kind, bool &loaded,string choice, string& destination);
void output(string &reportingMark, int &carNumber, string&
kind, bool &loaded, string &destination);
//*********** Main **************
int main()
{
string reportingMark;
int carNumber;
string kind;
string choice;
bool loaded;
string destination;
input (reportingMark, carNumber, kind, loaded, choice,
destination);
Car cPtr;
cPtr.setUpCar(reportingMark,carNumber, kind, loaded,
destination);
output (reportingMark, carNumber, kind, loaded, destination);
return 0;
}
/* ************* Function output ************
show all results of the program
******************************************* */
void output(string &reportingMark, int &carNumber, string&
kind, bool &loaded, string &destination)
{
string strLoaded;
if (loaded==true)
{
3. strLoaded = "TRUE";
}
else
{
strLoaded = "FALSE";
}
cout << fixed << left << setw(18) << "Reporting Mark" <<
reportingMark << endl;
cout << fixed << left << setw(18) << "Car Number" <<
carNumber << endl;
cout << fixed << left << setw(18) << "Kind"<< kind << endl;
cout << fixed << left << setw(18) << "Loaded" << strLoaded
<< endl;
cout << fixed << left << setw(18) << "Destination" <<
destination << endl;
cout << endl;
}
/* ************ Function Input **************
Collect the data
****************************************** */
void input(string &reportingMark, int &carNumber, string&
kind, bool &loaded,string choice, string& destination)
{
int len;
cout<< "Enter reportingMark with 5 or less upper case
characters: ";
getline(cin, reportingMark);
len= reportingMark.length();
while (len > 5)
{
cout<< "invalid! Enter reportingMark with 5 or less upper case
characters: ";
getline(cin, reportingMark);
len= reportingMark.length();
}
cout<< "Enter Car Number: ";
4. cin>> carNumber;
cout << "Enter kind of car: ";
cin>> kind;
do
{
cout<<"Enter the loaded only true or false:";
cin>>choice;
if (choice == "true")
{
loaded = true;
}
else if (choice == "false")
{
loaded = false;
}
else
{
cout<<"error!"< }
}
while (choice != "true" && choice != "false");
if(loaded==true)
{
cout<<"Entre the destination:";
cin.ignore();
getline(cin,destination);
}
if(loaded ==false)
{
destination = "NONE";
}
}
this is lab2 and the qustion is:
Use the same format for problem and function headings as
assignment A.
5. Problem 3.1
Copy the solution from problem 2.2
Make the following additions and changes:
1. Arrange the functions in the order:
* main
* output
* setUp (was setUpCar)
* input
2. Change the output function so it is a member function of the
class Car.
3. Change the name of the setUpCar function to setUp and make
it a
member function of the class Car, with five parameters
specified as
constant reference parameters.
4. Make all the data in the Car class private and make the
member
functions public.
5. Do NOT make the input function a member function.
6. Change the parameters of the input function to five reference
parameters corresponding to the five fields in the class.
7. In main define five variables. Pass these variables to the
input
function so the input function can set their values.
8. In main define a Car object. This time the Car object will be
in the
stack for main, rather than in the heap. Name this object car1.
9. In main call the setUp function and pass the five variables,
rather
than calling it from the input function.
10. In main call the output function.
11. Test the program with the same data as was used in
assignment B.
6. Solution
Try the below changed code...it may help you to an extent
#include <iostream>
#include <string>
#include<iomanip>
using namespace std;
/* ********* Class Car *************
********************************* */
class Car
{
private:
string reportingMark;
int carNumber;
string kind;
bool loaded;
string choice;
string destination;
public:
Car()
8. destination);
Car car1;
car1.setUp(reportingMark,carNumber, kind, loaded,
destination);
car1.output (reportingMark, carNumber, kind, loaded,
destination);
return 0;
}
/* ************* Function output ************
show all results of the program
******************************************* */
void Car :: output(string &reportingMark, int &carNumber,
string& kind, bool &loaded, string &destination)
{
string strLoaded;
if (loaded==true)
{
strLoaded = "TRUE";
}
else
{
strLoaded = "FALSE";
}
cout << fixed << left << setw(18) << "Reporting Mark" <<
reportingMark << endl;
9. cout << fixed << left << setw(18) << "Car Number" <<
carNumber << endl;
cout << fixed << left << setw(18) << "Kind"<< kind <<
endl;
cout << fixed << left << setw(18) << "Loaded" <<
strLoaded << endl;
cout << fixed << left << setw(18) << "Destination" <<
destination << endl;
cout << endl;
}
/* ************ Function SetUp **************
Sets the data
****************************************** */
void Car :: setUp(const string &r,const int &c ,const string
&k,const bool& l,const string &d)
{
reportingMark =r;
carNumber = c;
kind = k;
loaded = l;
destination = d;
}
/* ************ Function Input **************
Collect the data
****************************************** */
10. void input(string &reportingMark, int &carNumber, string&
kind, bool &loaded,string choice, string& destination)
{
int len;
cout<< "Enter reportingMark with 5 or less upper case
characters: ";
getline(cin, reportingMark);
len= reportingMark.length();
while (len > 5)
{
cout<< "invalid! Enter reportingMark with 5 or less upper
case characters: ";
getline(cin, reportingMark);
len= reportingMark.length();
}
cout<< "Enter Car Number: ";
cin>> carNumber;
cout << "Enter kind of car: ";
cin>> kind;
do
{
cout<<"Enter the loaded only true or false:";
cin>>choice;
if (choice == "true")
{