Hey guys...last project of the semester going on right now, and of course it has me totally stumped. The point of the program is to:
-Open a text file containing data on 10 cars
-Read these items into items of Car class type
-Assign an array of pointers to correspond to the array of Car items
-Sort them based on model
-Search the array for a user entered model
As you can see, I'm only up to trying to assign the array of pointers to the car objects, and I'm already banging my face on the keyboard. I'm getting this error:
1 2
|
PROG6/prog6.cpp: In function âvoid assignPointers(Car*, Car**)â:
PROG6/prog6.cpp:73: error: cannot convert âCarâ to âCar*â in assignment
|
To be honest, I don't really understand how to do this. I thought at first I could read the data from the file directly into the class items using the pointers, but that didn't want to work because I can't run a getData type function from a pointer to a Car, only from a Car itself.
Here's my code. If anyone can tell me what I'm doing wrong, I'd appreciate it so much. This is due Monday and I think I've got a long day ahead tomorrow.
Jade
prog6.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
|
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
#include "Car.h"
void readFile (ifstream&, Car[]);
void sortByModel(Car[]);
void searchByModel(Car[]);
void assignPointers(Car[], Car*[]);
int main ()
{
Car* carPtr[10];
Car carArray[10];
ifstream fin;
string ifName;
// for (int i=0; i < 10; i++)
cout << "Enter the name of the file you want to open: ";
cin >> ifName;
fin.open(ifName.c_str());
if (fin.fail())
{
cout << "Could not open the specified file.";
}
readFile (fin, carArray);
assignPointers(carArray, carPtr);
}
void readFile (ifstream&inFile, Car carArr[])
{
for (int i=0; i < 10; i++)
{
string aMake, aModel;
int aYear;
double aValue;
getline(inFile, aMake);
getline(inFile, aModel);
inFile >> aYear;
inFile >> aValue;
carArr[i].setMake(aMake);
carArr[i].setModel(aModel);
carArr[i].setYear(aYear);
carArr[i].setValue(aValue);
inFile.ignore(100, '\n');
inFile.ignore(100, '\n');
if(inFile.eof())
{
break;
}
}
}
void assignPointers(Car carArr[], Car*carPoint[])
{
for (int j=0; j < 10; j++)
{
carPoint[j] = carArr[j];
}
}
|
Car.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
|
/*************************
* Jade Scholz *
* CSCI 123 - Spring 2012 *
* Program # 4 *
* Due 05.04.12 *
*************************/
#include <string>
#include <iomanip>
#include <fstream>
#include <iostream>
using namespace std;
class Car
{
private: // Fields for Car class
string make;
string model;
int year;
double value;
public:
Car (string, string, int, double);
Car ();
void setMake (string); // Sets car make
void setModel (string); // Sets car model
void setYear (int); // Sets car year
void setValue (double); // Sets car value
string getMake(); // Returns car make
string getModel(); // Returns car model
int getYear(); // Returns car year
double getValue(); // Returns car value
void print(ofstream&); // Prints data to output file
void print(); // Prints data to screen
ofstream outFile;
};
Car::Car(string make, string model, int year, double value) // Initialize
{}
Car::Car() : make("no make"), model("no model"), year(2012), value(0.00) // Set defaults
{}
void Car::setMake(string carMake ) // Sets car make
{
make = carMake;
}
void Car::setModel(string carModel ) // Sets car model
{
model = carModel;
}
void Car::setYear(int carYear) // Sets car year
{
year = carYear; // If year is less than 1769, more than 2012, returns 2012
if (year < 1769 || year > 2012)
{
year = 2012;
}
}
void Car::setValue(double carValue) // Sets car value (sets to 0 if negative)
{
value = carValue;
if (value < 0)
{
value = 0.00;
}
}
string Car:: getMake() // Returns car make
{
return make;
}
string Car::getModel() // Returns car model
{
return model;
}
int Car::getYear() // Returns car year
{
return year;
}
double Car::getValue() // Returns car value
{
return value;
}
void Car::print(ofstream&outFile) // Prints car data out to file
{
outFile << setw(6) << left << year << setw(15) << left << make << setw(20) << left
<< model << setw(10) << setprecision(2) << fixed << right << value << endl;
}
void Car::print() // Prints car data out to screen
{
cout << setw(6) << left << year << setw(15) << left << make << setw(20) << left
<< model << setw(10) << setprecision(2) << fixed << right << value << endl;
}
|