Initial idea:
MAIN.CPP
:
#include <cstdlib>
#include "Cars.h"
#include "Dealer.h"
#include "Manufacturer.h"
#include <fstream>
#include <iostream>
#include <string>
#include <iostream>
#include <iomanip>
#include <list>
using namespace std;
//Instance variables for each class
string VIN = " ";
int miles;
string dealer = " ";
int price;
string vinCode=" ";
string manuCode = " ";
string manuName = " ";
string dealerName = " ";
int zipcode;
string dealerPhone = " ";
int main(int argc, char** argv) {
char command;
//Cars vehicule;
Manufacturer maker;
Dealer dealership;
ifstream infile;
ofstream outfile;
list <Cars> carsList;
//Checks if the data file exists
infile.open("database.txt", ifstream::in);
outfile.open("database.txt", ios_base::app);
//each command is a different program option
cout << "Enter a command:" << endl;
cin >> command;
while (command!='q')
{
switch (command)
{
case 'a':
{
cin >> command;
//adds a car
if (command=='c')
{
//creates a new car object and calls constructor
Cars *vehicule = new Cars();
//gets user input a assign then to variables
//for the method calls
cin >> VIN >> miles >> dealer >> price;
// 1. this is were the compiler complains
vehicule.->addData(VIN, miles, dealer, price);
vehicule.addToBase(outfile);
carsList.push_back(vehicule);
list<Cars*>::iterator it;
for(it=carsList.begin(); it!=carsList.end(); it++)
{
cout << *it->getVIN() << endl; // compile error
}
}
break;
}
//new command to keep the while loop going
cout << "Enter a command:" << endl;
cin >> command;
}
}
outfile.close();
return 0;
}
CARS.H
:
#ifndef CARS_H
#define CARS_H
#include <string>
#include <iostream>
#include <fstream>
using namespace std;
//Object that contains all information about cars (this is the class documentation)
class Cars {
public:
//class methods
*Cars(){VIN=" "; mileage=0; dealership=" "; price=0;}
void addData(string, int, string, int);
void addToBase(ofstream&);
string getVin(){return this->VIN;}
int getMiles(){return this->mileage;}
string getDealer(){return this->dealership;}
int getPrice(){return this->price;}
//private variables containing object information
private:
string VIN;
int mileage;
string dealership;
int price;
string vinCode;
};
void Cars::addData(string identification, int mile, string dealer, int money)
{
VIN=identification;
mileage=mile;
dealership=dealer;
price=money;
vinCode = VIN.substr(0,3);
return;
}
void Cars::addToBase(ofstream& file)
{
file << "c" << endl << VIN << endl << mileage << endl <<
dealership << endl << price << endl;
return;
}
New version of what I have gotten so far:
#include "Car.h"
#include "Dealer.h"
#include "Manufacturer.h"
#include <fstream>
#include <iostream>
#include <string>
#include <iostream>
#include <iomanip>
#include <list>
using namespace std;
string VIN;
int miles;
string dealer;
int price;
string vinCode;
string manuCode;
string manuName;
string dealerName;
int zipcode;
string dealerPhone;
int main(int argc, char** argv) {
char command;
ifstream infile;
ofstream outfile;
list<Car*> carsList;
//Checks if the data file exists
infile.open("database.txt", ifstream::in);
outfile.open("database.txt", ios_base::app);
//Reads in user input
cout << "Enter a command:" << endl;
cin >> command;
while (command != 'q')
{
switch (command)
{
case 'a': //Add
{
cin >> command;
if (command == 'c') //Add car
{
cin >> VIN >> miles >> dealer >> price;
Car* vehicule = new Car(VIN, miles, dealer, price); //New pointer
vehicule->addToBase(outfile);
carsList.push_back(vehicule);
list<Car*>::const_iterator iterator;
for (std::list<Car*>::const_iterator iterator = carsList.begin(),
end = carsList.end(); iterator != end; ++iterator)
{
cout << (*iterator)->getVin();
}
//end of for loop
}//end of if loop
}//end of case loop
break;
}//end of switch loop
cout << "Enter a command:" << endl;
cin >> command;
}//end of while loop
infile.close();
outfile.close();
return 0;
}
I still get an error:
"/Applications/Xcode.app/Contents/Developer/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf "/Applications/Xcode.app/Contents/Developer/usr/bin/make" -f nbproject/Makefile-Debug.mk dist/Debug/GNU-MacOSX/project_1 make[2]:
*** No rule to make target `newcppsimpletest.cpp', needed by `build/Debug/GNU-MacOSX/newcppsimpletest.o'. Stop. make[1]: *** [.build-conf] Error 2 make: *** [.build-impl] Error 2
Car.h
:
#ifndef CARS_H
#define CARS_H
#include <string>
#include <iostream>
#include <fstream>
using namespace std;
class Car {
public:
Car();
Car(string, int, string, int);
void addToBase(ofstream&);
string getVin(){return this->VIN;}
int getMiles(){return this->mileage;}
string getDealer(){return this->dealership;}
int getPrice(){return this->price;}
string getVinCode(){return this->vinCode;}
private:
string VIN;
int mileage;
string dealership;
int price;
string vinCode;
};
Car::Car()
{
string VIN;
int mileage=0;
string dealership;
int price=0;
string vinCode;
}
Car::Car(string vin, int miles, string carDealer, int dollars)
{
string VIN=vin;
int mileage=miles;
string dealership=carDealer;
int price=dollars;
string vinCode = VIN.substr(0,3);
}
void Car::addToBase(ofstream& file)
{
file << "c" << endl << VIN << endl << mileage << endl <<
dealership << endl << price << endl;
return;
}
Use
vehicule->addData(VIN, miles, dealer, price);
Instead of
vehicule.->addData(VIN, miles, dealer, price);