-4

Edit 1

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;
}

Edit 2

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;
}
Biffen
  • 6,249
  • 6
  • 28
  • 36
madeluccar
  • 157
  • 1
  • 1
  • 8
  • line 77 main.cpp:74:30: error: expected unqualified-id vehicule.->addData(VIN, miles, dealer, price); – madeluccar Sep 11 '15 at 21:35
  • 1
    OK, should have said ‘*What's the error?*’. – Biffen Sep 11 '15 at 21:35
  • by the way I apologize for the bad formatting, first post on Stack will reply any questions regardless – madeluccar Sep 11 '15 at 21:37
  • And what do you hope to achieve with `.->`? There are numerous issues in your code. Have you read a book or tutorial on C++? – Biffen Sep 11 '15 at 21:37
  • the compiler suggested the implementation of .-> because Cars is a pointer – madeluccar Sep 11 '15 at 21:38
  • I doubt that. It might have suggested `->`, however. – Biffen Sep 11 '15 at 21:39
  • 1
    The compiler would have suggested `->`, not `.->` (note the lack of the `.`). However, I would suggest (along with several others here, if I'm not mistaken) to instead change `Cars *vehicule = new Cars()` to `Cars vehicule;` (i.e., make it not a pointer) – R_Kapp Sep 11 '15 at 21:40
  • I am still new at this so my coding skills and my code quality is not that good IMO, however I do wanna get better at this! – madeluccar Sep 11 '15 at 21:43
  • @Biffen I implemented a list of pointers the way many sources suggest and I still get that error. It looks fine to me. – madeluccar Sep 13 '15 at 02:48
  • 1
    I haven't read through your new code in detail, but the error message you get is due to the IDE you are using. Somehow, the file `newcppsimpletest.cpp` was not added to your project properly, and the compiler doesn't know where it is. Without more information, I can't be of too much help, unfortunately. – R_Kapp Sep 13 '15 at 03:45
  • @madeluccar If you have a new question then please post a new question. – Biffen Sep 13 '15 at 08:56
  • Possible duplicate of [What are the differences between a pointer variable and a reference variable in C++?](http://stackoverflow.com/questions/57483/what-are-the-differences-between-a-pointer-variable-and-a-reference-variable-in) – madeluccar Nov 04 '16 at 10:20

3 Answers3

0

Use

vehicule->addData(VIN, miles, dealer, price);

Instead of

vehicule.->addData(VIN, miles, dealer, price);
Dhirendra
  • 780
  • 9
  • 26
0

You've declared vehicule as a pointer, but you're attempting to access methods with the '.' operator. You need to use '->' when working with pointers. Like: vehicule->addData(VIN, miles, dealer, price);

You'll need to update any code that references vehicule.

Joel
  • 2,227
  • 1
  • 17
  • 23
  • However I have another compiler error of type : main.cpp:72:26: error: no viable conversion from 'Cars *' to 'Cars' Cars vehicule = new Cars(); @Joel – madeluccar Sep 11 '15 at 21:51
  • @madeluccar `new Cars()` returns a `Cars*` and you're trying to assign it to a `Cars` variable. It's either `Cars* vehicule = new Cars();` or `Cars vehicule;`, depending on whether you want a pointer or not. It's not exactly recommended to use `new`, but if you do you should have a matching `delete` somewhere. I really recommend you read a book or something, there are numerous issues in your code. – Biffen Sep 12 '15 at 08:21
  • @Biffen what other issues have you seen? And actually when you use new you should create a new object of type Cars – madeluccar Sep 12 '15 at 19:18
  • @madeluccar Issues of varying seriousness, of the top of my head: Implementation code in a header file (will cause problems later), odd variable scope, numerous cases of ‘pointer confusion’, strange class design, lack of `const`, all the inexplicable `" "` strings, memory leak. As for your second sentence: I don't understand what you're saying. – Biffen Sep 12 '15 at 19:25
  • @Biffen you are saying using new creates a pointer variable, but shouldn't that line create a new object of type Cars – madeluccar Sep 12 '15 at 19:28
  • @madeluccar `new Cars();` creates a new object of type `Cars` (it doesn't create a variable, I don't know where you got that) and gives you *a pointer* to it. – Biffen Sep 12 '15 at 19:30
  • @Biffen what would the most effective way to create a list of objects, because I need to keep track os each object of type Cars and be able to get their instance variables by calling class methods. – madeluccar Sep 12 '15 at 19:43
  • @madeluccar Depends on what you mean by ‘*effective*’, but the common way is to create a class called `Car` (singular(!)) and a list of cars would be `std::list` (or `std::vector`; depends on what you plan to do with it). Unless you plan to create subclasses of `Car` you shouldn't need to (indeed you shouldn't) use pointers *anywhere*, and thus no `new`s or `->`s. – Biffen Sep 12 '15 at 19:47
  • @Biffen and how can I create and traverse a list pf Objects so I can call each objects methods? – madeluccar Sep 12 '15 at 21:57
  • @madeluccar Google is your friend. There should be plenty of examples on the internet. – Biffen Sep 12 '15 at 22:05
  • @Biffen would you look at it now? I still get an error but this one is different, the compiler doesn't even returns the location of the error in the main function. – madeluccar Sep 13 '15 at 03:00
-1

OK, I saw these two issues. You have declared vehicule as a pointer and allocated some memory like this.

Cars *vehicule = new Cars();

Then you're calling vehicule like this:

vehicule.->addData(VIN, miles, dealer, price);
vehicule.addToBase(outfile);

Strike the above.

The calls should be:

vehicule->addData(VIN, miles, dealer, price);
vehicule->addToBase(outfile);

While this seems like a small piece of code, it is always advised that, once you're done allocating, you should deallocate the memory.

Since, you add vehicule in the list, carsList, after the work is completed, do clean up the list.

You can do that, like this:

carsList.clear();

Hope this helps, You sound like you're new to this. So, trying to be as detailed as possible.

Biffen
  • 6,249
  • 6
  • 28
  • 36
Umashankar Das
  • 601
  • 4
  • 12
  • `carsList` will clear its own memory, what's more worrying is the `new` without a `delete`. – Biffen Sep 11 '15 at 21:52
  • Your point about new and delete is correct. But, a clear becomes relevant if there are repeated calls and he is using a push_back. Anyhow, for basic code, the precautions are meaningless , since, program stops execution immediately. – Umashankar Das Sep 11 '15 at 21:59
  • 2
    You really need to format the code in your answer to make it readable – NathanOliver Sep 11 '15 at 22:03
  • Sorry, A little new to stack overflow. Come here once in a blueMoon. – Umashankar Das Sep 11 '15 at 22:05
  • @UmashankarDas Why are you mentioning deleting pointers inside a list? OP hasn't got any lists of pointers. – Biffen Sep 11 '15 at 22:08
  • @Biffen - What about this line? list carsList; – Umashankar Das Sep 11 '15 at 22:10
  • @UmashankarDas That's a list of objects. A list of pointers would be `list`. – Biffen Sep 11 '15 at 22:15
  • @Biffen Thanks. I was trying to do this as a pointer implementation. Would it not be a good idea to learn pointers instead of scaring people away from it? – Umashankar Das Sep 11 '15 at 22:16
  • @UmashankarDas It is my, and many others', opinion that pointers should be avoided. If pointer-like functionality is necessary then one of the smart pointer implementations should be used. But I don't see what any of this has got to do with lists of pointers. – Biffen Sep 11 '15 at 22:19
  • I understand a bit about pointer funcionality, I mean I tried to implement a List of Objects but its still returning an error about not being able to transform a pointer to an object list, but I don't understan where I'm trying to do this is the code @Biffen – madeluccar Sep 12 '15 at 02:08