Question

I created a struct for Car and now I need to turn it into a class...

I created a struct for Car and now I need to turn it into a class for Car. Below is the code that I wrote for the structure. For the class, we are suppose to make the data in the class Car private, revise the input so the input function will only read the data from the user, and then it will call an additional function named setUpCar which will put the data into the object. Not sure how to change from a struct to a class. The results should look like :

reportingMark SP

carNumber     12345

kind flat

loaded true

destination Salt Lake City

MY CODE THAT I WROTE
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

struct Car{
string reportingMark;
int carNumber;
string kind;
bool loaded;
string destination;
};

void input(Car *cPtr);
void output(Car *cPtr);

int main(){
Car *cPtr;
cPtr = new Car;
input(cPtr);
output(cPtr);
delete cPtr;
return 0;
}

void input(Car *cPtr){
string rMark;
int len;
cout<<"Enter reporting mark of car: ";
getline(cin, rMark);
len = rMark.length();

while(len>5){
cout<<"Invalid! Enter reporting mark with 5 or less upper case char: ";
getline(cin, rMark);
len = rMark.length();
}
for(int i=0;i<int(rMark.length());i++){
rMark[i] = toupper(rMark[i]);
}
cout<<"Enter car number: ";
cin>>cPtr->carNumber;
cout<<"What kind of car is this(flat,tank,box,other): ";
cin>>cPtr->kind;
cout<<"Is the car loaded(1 if it is, else 0): ";
cin>>cPtr->loaded;
cout<<"Enter the destination: ";
cin.ignore();
getline(cin, cPtr->destination);

cPtr->reportingMark = rMark;
}

void output(Car *cPtr){
string rMark;
cout<<"\nCar Info: \n";
cout<< "ReportingMark: "<< cPtr->reportingMark<<endl;
cout<< "carNumber: "<<cPtr->carNumber<<endl;
cout<< "kind: "<<cPtr->kind<<endl;
cout<< "loaded: "<<cPtr->loaded<<endl;
cout<< "destination: "<<cPtr->destination<<endl;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Thanks for the question.


Here is the completed code for this problem. Let me know if you have any doubts or if you need anything to change.


Thank You !!


==============================================================================================

#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

class Car{
  
   public:
      
       // setter functions
       void setCarNumber(int number){
           this->carNumber=number;
       }
       void setKind(string kind){
           this->kind=kind;
       }
       void setLoad(bool loaded){
           this->loaded=loaded;
       }
       void setDestination(string destination){
           this->destination=destination;
       }
       void setReportingMark(string reportingMark){
           this->reportingMark=reportingMark;
       }
       // getter functions
       string getReportingMark() const{ return reportingMark;}
       string getKind() const{ return kind;}
       string getDestination() const{ return destination;}
       int getCarNumber() const{return carNumber;}
       bool isLoaded() const{return loaded;}
  
private:
   string reportingMark;
   int carNumber;
   string kind;
   bool loaded;
   string destination;
};

void input(Car *cPtr);
void output(Car *cPtr);

int main(){
Car *cPtr;
cPtr = new Car;
input(cPtr);
output(cPtr);
delete cPtr;
return 0;
}

void input(Car *cPtr){
string rMark;
int len;
cout<<"Enter reporting mark of car: ";
getline(cin, rMark);
len = rMark.length();

while(len>5){
cout<<"Invalid! Enter reporting mark with 5 or less upper case char: ";
getline(cin, rMark);
len = rMark.length();
}
for(int i=0;i<int(rMark.length());i++){
rMark[i] = toupper(rMark[i]);
}
int num;
cout<<"Enter car number: ";cin>>num;
cPtr->setCarNumber(num);
string type;
cout<<"What kind of car is this(flat,tank,box,other): ";cin>>type;
cPtr->setKind(type);
int isLoaded;
cout<<"Is the car loaded(1 if it is, else 0): ";cin>>isLoaded;
cPtr->setLoad(isLoaded==1?true:false);
string destination;
cout<<"Enter the destination: ";
cin.ignore();
getline(cin, destination);
cPtr->setDestination(destination);
cPtr->setReportingMark(rMark);
}

void output(Car *cPtr){
string rMark;
cout<<"\nCar Info: \n";
cout<< "ReportingMark: "<< cPtr->getReportingMark()<<endl;
cout<< "carNumber: "<<cPtr->getCarNumber()<<endl;
cout<< "kind: "<<cPtr->getKind()<<endl;
cout<< std::boolalpha<< "loaded: "<<cPtr->isLoaded()<<endl;
cout<< "destination: "<<cPtr->getDestination()<<endl;
}

Add a comment
Know the answer?
Add Answer to:
I created a struct for Car and now I need to turn it into a class...
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
  • #include<iostream> #include<string> #include<iomanip> using namespace std; /* ********* Class Car ************* ********************************* */ class Car {...

    #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() { reportingMark = ""; carNumber = 0; kind = "Others"; loaded = 0; destination = "NONE"; } ~Car() { } void setUpCar(string &reportingMark, int &carNumber, string &kind, bool &loaded, string &destination); }; void input(string &reportingMark, int &carNumber, string &kind, bool &loaded,string choice, string &destination); void output(string &reportingMark, int &carNumber,...

  • Need help doing program In bold is problem E2 #include<iostream> #include<string> #include<vector> using namespace std; //Car...

    Need help doing program In bold is problem E2 #include<iostream> #include<string> #include<vector> using namespace std; //Car class //Defined enum here enum Kind{business,maintenance,other,box,tank,flat,otherFreight,chair,seater,otherPassenger}; //Defined array of strings string KIND_ARRAY[]={"business","maintenance","other,box","tank,flat","otherFreight","chair","seater","otherPassenger"}; class Car { private: string reportingMark; int carNumber; Kind kind; //changed to Kind bool loaded; string destination; public: //Defined setKind function Kind setKind(string knd) { for(int i=0;i<8;i++) //repeat upto 8 kinds { if(knd.compare(KIND_ARRAY[i])==0) //if matched in Array kind=(Kind)i; //setup that kind } return kind; } //Default constructor Car() { } //Parameterized constructor...

  • C++ getline errors I am getting getline is undefined error messages. I would like the variables...

    C++ getline errors I am getting getline is undefined error messages. I would like the variables to remain as strings. Below is my code. #include <iostream> #include<string.h> using namespace std; int index = 0; // variable to hold how many customers are entered struct Address //Structure for the address. { int street; int city; int state; int zipcode; }; // Customer structure struct Customer { string firstNm, lastNm; Address busAddr, homeAddr; }; // Functions int displayMenu(); Customer getCustomer(); void showCustomer(Customer);...

  • #include <iostream> #include <string> #include <fstream> #include <sstream> using namespace std; struct transition{ // transition structure...

    #include <iostream> #include <string> #include <fstream> #include <sstream> using namespace std; struct transition{ // transition structure char start_state, to_state; char symbol_read; }; void read_DFA(struct transition *t, char *f, int &final_states, int &transitions){ int i, j, count = 0; ifstream dfa_file; string line; stringstream ss; dfa_file.open("dfa.txt"); getline(dfa_file, line); // reading final states for(i = 0; i < line.length(); i++){ if(line[i] >= '0' && line[i] <= '9') f[count++] = line[i]; } final_states = count; // total number of final states // reading...

  • Fix my code, if I the song or the artist name is not on the vector,...

    Fix my code, if I the song or the artist name is not on the vector, I want to user re-enter the correct song or artist name in the list, so no bug found in the program #include <iostream> #include <vector> #include <string> #include <algorithm> using namespace std; class musicList{ private: vector<string> songName; vector<string> artistName; public: void addSong(string sName, string aName){ songName.push_back(sName); artistName.push_back(aName); } void deleteSongName(string sName){ vector<string>::iterator result = find(songName.begin(), songName.end(), sName); if (result == songName.end()){ cout << "The...

  • ​c++ program that takes user input from the console and store the data into a linked list.

    c++ program that takes user input from the console and store the data into a linked list. The input made of 4 objects associated to a car: model, make, mileage and year. My program should take the how many inputs the user want to make, followed by the inputs themselves. After the input, my program should print the data inside the linked list. So far, the issue is that my program print some of the input, but not all. Input sample:3HondaSentra89002017ToyotaCamri1098271999NissanSentra87261987current...

  • Hi, I need to write a program for linux (putty) and please read carefully (I've asked...

    Hi, I need to write a program for linux (putty) and please read carefully (I've asked the same question 3 times because the experts who answered my questions wrote their own carmain1.cpp file. Please leave it where it is and only create car.cpp and car.h files.) Here's carmain1.cpp /* carmain1.cpp * Please don't rewrite this file * I will be using the exact * same file below when grading * */ #include <iostream> #include "car.h" using namespace std; int main()...

  • I need help debugging this C++ prgram. What Am i doing wrong? //******************************************************** // This program...

    I need help debugging this C++ prgram. What Am i doing wrong? //******************************************************** // This program reads two input files whose lines are //ordered by a key data field. This program should merge //these two files, writing an output file that contains //all lines from both files ordered by the same key field. // //********************************************************* #include <iostream> #include<string> #include<fstream> //prototype void mergeTwoFiles (ifstream&,ifstream&, ofstream&); using namespace std; int main() {string inFile1,inFile2,outFile; // input and output files ifstream in1; ifstream in2;...

  • CODES: main.cpp #include <iostream> #include <string> #include "ShoppingCart.h" using namespace std; char PrintMenu() { char answer;...

    CODES: main.cpp #include <iostream> #include <string> #include "ShoppingCart.h" using namespace std; char PrintMenu() { char answer; cout << "MENU" << endl; cout << "a - Add item to cart" << endl; cout << "d - Remove item from cart" << endl; cout << "c - Change item quantity" << endl; cout << "i - Output items' descriptions" << endl; cout << "o - Output shopping cart" << endl; cout << "q - Quit" << endl << endl; while (true) {...

  • I am having trouble trying to output my file Lab13.txt. It will say that everything is...

    I am having trouble trying to output my file Lab13.txt. It will say that everything is correct but won't output what is in the file. Please Help Write a program that will input data from the file Lab13.txt(downloadable file); a name, telephone number, and email address. Store the data in a simple local array to the main module, then sort the array by the names. You should have several functions that pass data by reference. Hint: make your array large...

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