Register | Sing In

New posts

Stacks in C programming Linked Lists in C programming File Input/Output in C programming Check prime numbers in C Programming C Programming Enumeration C Programming Union

Archives

2013-10 2013-09 2013-08

C++ Arrays and classes

June 7, 2014 Posted by: Hossein Views: 1709

read cars from a file At the bottom of the input function, declare a Car object named temp using the constructor that takes the five parameters.

Create a file containing the following three lines of data .
    Type   ARR   number   kind          loaded   destination
      Car    CN    819481   maintenance   false    NONE       

      Car    SLSF   46871   business      true     Memphis  

      Car    AOK      156   tender        true     McAlester

In the input function, declare an object of type ifstream named inputFile, which we will use to read from the file.At the beginning of the code for the input function, open thefile. If the open fails, send a message to stderr and exit the        program.

1. main 

2. Car member functions 

   1. constructors in the order     

   1. default constructor   

     2. copy constructor     

   3. other constructors   

 2. output     

 3. operator== with Car parameters

 4. input

Best Answer



#include <cstdlib>
#include <iostream>
#include <string>
#include <fstream>
 
 
 
using namespace std;
 
class Car{
private:
  string Type;
  string ARR;
  int number;
  string kind;
  string loaded;
  string destination;
public:
   // default constructor   
  Car(){}
  //  constructor with arguments
  Car(string Type,string ARR,int number,string kind,string loaded,string destination){
     this->Type=Type;
     this->ARR=ARR;
     this->number=number;
     this->kind=kind;
     this->loaded=loaded;
     this->destination=destination;
  }
 
  //set data
  void setType(string Type){
    this->Type=Type;
  }
  void setARR(string ARR){
    this->ARR=ARR;
  }
  void setNumber(int number){
    this->number=number;
  }
  void setKind(string kind){
    this->kind=kind;
  }
  void setLoaded(string loaded){
    this->loaded=loaded;
  }
  void setDestination(string destination){
    this->destination=destination;
  }
  //get data
  string getType(){
    return this->Type;
  }
  string getARR(){
    return this->ARR;
  }
  int getNumber(){
    return this->number;
  }
  string getKind(){
    return this->kind;
  }
  string getLoaded(){
    return this->loaded;
  }
  string getDestination(){
    return this->destination;
  }
  bool Car::operator==( Car &other)  {
    bool flag=false;
    if(this->number==other.getNumber()){
      flag=true;
    }
 
    return flag;
  }
};
 
int main(int argc, char *argv[])
{
    Car cars[10];
    int totalCar=0;
    string line;
    ifstream inputFile ("cars.txt");
    if (inputFile.is_open())
    {
        while (inputFile.good() )
        {
		          getline (inputFile,line);
				  char* cstr = new char [line.size()+1];
				  strcpy (cstr, line.c_str());
				  char* token= strtok(cstr,",");
				  int count=0;
 
		          string Type;
                  string ARR;
                  int number;
                  string kind;
                  string loaded;
                  string destination;
 
                  while (token != NULL)
                  {
					  if(count==0){
					    Type=token;
					  }
					  if(count==1){
					    ARR=token;
					  }
					  if(count==2){
					    number=atoi(token);
					  }
					  if(count==3){
					    kind=token;
					  }
					  if(count==4){
					    loaded=token;
					  }
					  if(count==5){
					    destination=token;
					     count=-1;
					  }
                      token = strtok (NULL, ","); 	
 
					  count++;
                  }	
                  Car temp(Type,ARR,number,kind,loaded,destination);
                  cars[totalCar]=temp;
                  totalCar++;
		}
        inputFile.close();//cloe file
	}
	else{ cout << "Unable to open file\n\n";}
    cout<<" Type   ARR     number   kind          loaded      destination\n";    
    for(int i=0;i<totalCar;i++){
      cout<<cars[i].getType()<<"\t"<<cars[i].getARR()<<"\t"<<cars[i].getNumber()<<"\t"<<cars[i].getKind()<<"\t"<<cars[i].getLoaded()<<"\t"<<"\t"<<cars[i].getDestination()<<"\n";
    }
    system("PAUSE");
    return EXIT_SUCCESS;
}

Output

Arrays

Download this example

Online Chat

LiveZilla Live Help

Popular posts

Function strtok in C programming Check prime numbers in C Programming C Programming Union Linked Lists in C programming File Input/Output in C programming C Programming Enumeration

RSS-subscribe

New materials

Subscribe