Can someone please help me get my code running! I am trying to do a sectional sort based on mileage. I have got a lot of code wrote based on the book, but I can not get it to work or figure it out! Please assist!

Class

#include <iostream>
#include <fstream>
#include <iomanip>
#include <functional>
#include <algorithm>
#include <string>
#include <cstdlib>
#include <sstream>
using namespace std;

//Vehicle Class
class Car
{
protected:
	string make;  //make
    string model; // model
    string color;  // color
    int	year;  // year
    int mileage;  // miles on car
	long long int VIN;  //Vin Number of car

public:
		//Constructor that will set information for a new car
	void New_vehicle (string a, string b, string c, int d, int e, int f) 
	{make = a; model = b; color = c; year = d; mileage = e; VIN = f;}
	
	Car(); //Default constructor
	Car(string, string, string, int, int,int);
	//mutator and accessor functions
	void setMake(string);
    void setModel(string);
    void setColor(string);
    void setYear(int);
    void setMileage(int);
	void setVIN(int);

    string getMake();
    string getModel();
    string getColor();
    int getYear();
    int getMileage();
	long long int getVIN();

	//Check mileage to see if valid
    void valid_mileage(int);
    void car_details();
    string string_car_details();
};

//Sets to default values
Car::Car() {
	make = " ";
    model = " ";
    color = " ";
    year = 0;
    mileage = 0;
	VIN = 0;
}
	// My Vehicle set up(Make, model, color, year, type,bedsize, bike, mileage)
Car::Car(string make, string model, string color, int year, int mileage, int VIN) {
	Car::make =  make;
	Car::model = model;
	Car::color = color;
	Car::year = year;
    valid_mileage(mileage);
	Car::VIN = VIN;
}

void Car::setMake(string make) {
    Car::make = make;
}

void Car::setModel(string model) {
    Car::model = model;
}

void Car::setColor(string color) {
    Car::color = color;
}

void Car::setYear(int year) {
    Car::year = year;
}

void Car::setMileage(int mileage) {
    valid_mileage(mileage);
}

void Car::setVIN(int VIN) {
	Car::VIN = VIN;
}


string Car::getMake() {
    return make;
}
string Car::getModel() {
    return model;
}
string Car::getColor() {
    return color;
}
int Car::getYear() {
    return year;
}
int Car::getMileage() {
    return mileage;
}

long long int Car::getVIN() {
	return VIN;
}


void Car::valid_mileage(int mileage) {
    if (mileage>=0)
        Car::mileage=mileage;
    else {
        Car::mileage=0;
        cout << "WARNING! You have entered invalid mileage!\n";
	}
    }

	void Car::car_details() {
	    cout << "The current car is a " << year << ' ' << color << ' '
			<< make << ' ' << model << " with " << mileage << " miles.\n\n";
	}

	string Car::string_car_details() {
	    stringstream buf;
	    buf << "The current car is a " << year << ' ' << color << ' '
	    << make << ' ' << model << " with " << mileage << " miles.\n\n";
	    return buf.str();
	}

Cpp file

#include "CarClass.h"
using namespace std;

//Set up Search 
int search(Car object[], int size, int value)
{
   int index = 0;         // Used as a subscript to search array 
   int position = -1;     // Used to record position of search value 
   bool found = false;    // Flag to indicate if the value was found 

   while (index < size && !found)
   {
      if (object[index].getVIN() == value) // If the value is found
      {
         found = true;          // Set the flag 
         position = index;      // Record the value's subscript 
      }
      index++;                  // Go to the next element 
   }
   return position;             // Return the position
}// End search 


int search(Car[], int, int);
void selectionSort(Car[], int);
void showArray(Car[], int);

int main() {
        
	const int SIZE = 9;
			
	//Array of 9 cars
	Car Car_array[SIZE] = { Car("Porsche", "911", "Silver", 2005, 18990, 1237362727), 
							Car("Ford", "Mustang", "Red", 2007, 49842, 7337372239),
							Car("Chevrolet", "Beretta", "Black", 1989, 90332, 2873644922),
							Car("Ford", "Focus", "White", 2008, 150, 9236498273),
							Car("Voltzwagon", "Jetta", "Black", 2006, 28002, 4673992056),
							Car("Rolls Royce", "Ghost", "Silver", 2005, 10000, 9292983855),
							Car("Mazda", "626", "Blue", 2002, 84754, 7364646463),
							Car("Toyota", "Camry", "Red", 2004, 50332, 2133737227),
							Car("Bugatti", "Veyron 16.4", "White", 2010, 5, 5712893401)};

	
	long long int manualVIN= 2873644922;
	long long int desiredVIN;
	int posLin; // Used to search for VIN 2873644922
	int pos;  // Used for when user wil input the vin they want to search

	//MANUAL LINEAR SEARCH FOR VIN NUMBER
	
		posLin = search(Car_array, SIZE, manualVIN); 

		if (posLin == -1)
			cout << "You do not have a car with the VIN Number 2873644922.\n";
		else 
		{
			cout << "You do have a car on the lot with the VIN Number 2873644922.\n";
		}
		cout << "_______________________________________________________\n";
	// END MANUAL LINEAR SEARCH FOR VIN NUMBER

	// Get the VIN to search for

	 // LINEAR SEARCH FOR VIN NUMBER
		cout << "Enter a VIN number to search for: ";
		cin >> desiredVIN;

	// Linear Search for VIN Number
		pos = search(Car_array, SIZE, desiredVIN); 

		if (pos == -1)
			cout << "You do not have a car with the VIN Number " << desiredVIN << ".\n";
		else 
		{
			cout << "You do have a car on the lot with the VIN Number " << desiredVIN << ".\n";
		}
		cout << "_______________________________________________________\n";

	 // END LINEAR SEARCH FOR VIN NUMBER
	
	// Loop to display car details
		for (int x=0; x<9; x++) {
			Car_array[x].car_details();
		}

		cout << "_______________________________________________________\n";


		//Selection Sort

		cout << " This is the Array of Cars in a selection sort!!\n\n";

			// Sort the array
	selectionSort(Car_array, SIZE);
	
	// Display the values again
	cout << "The sorted values are\n";
	showArray(Car_array, SIZE);


		return 0;

}

void selectionSort(Car array[], int size)
{
	int startScan, minIndex, minValue;

	for (startScan = 0; startScan < (size - 1); startScan++)
	{
		minIndex = startScan;
		minValue = array[startScan];
		for(int index = startScan + 1; index < size; index++)
		{
			if (array[index].getVIN < minValue)
			{
				minValue = array[index];
				minIndex = index;
			}
      }
		array[minIndex] = array[startScan];
		array[startScan] = minValue;
	}
}

void showArray(Car array[], int size)
{
	for (int count = 0; count < size; count++)
		cout << array[count] << " ";
	cout << endl;
}

Recommended Answers

All 3 Replies

Then IMO you need to start over. Write a little bit, get it working. Then write a little more, get it working. Write a little more....
Trying to debug 260 lines with no idea where the errors could be is very difficult.

Well let me post what I have working:

#include "CarClass.h"
using namespace std;

//Set up Search 
int search(Car object[], int size, int value)
{
   int index = 0;         // Used as a subscript to search array 
   int position = -1;     // Used to record position of search value 
   bool found = false;    // Flag to indicate if the value was found 

   while (index < size && !found)
   {
      if (object[index].getVIN() == value) // If the value is found
      {
         found = true;          // Set the flag 
         position = index;      // Record the value's subscript 
      }
      index++;                  // Go to the next element 
   }
   return position;             // Return the position
}// End search 

//Sort Array
void selectionSort(array[], int size)

{
int startScan, minIndex, minValue;

for (startScan = 0; startScan < (size - 1); startScan++)
{
	minIndex = startScan;
	minValue = array[startScan];
		for(int index = startScan + 1; index < size; index++)
	{
		if (array[index].VIN < minValue)

	{
		minValue = array[index];
		minIndex = index;
	}
	}
		array[minIndex] = array[startScan];
		array[startScan] = minValue;
	}
	}


int search(Car[], int, int);
void selectionSort(array[], int);
void showArray();
int main() {
        
	const int SIZE = 9;
			
	//Array of 9 cars
	Car Car_array[SIZE] = { Car("Porsche", "911", "Silver", 2005, 18990, 1237362727), 
							Car("Ford", "Mustang", "Red", 2007, 49842, 7337372239),
							Car("Chevrolet", "Beretta", "Black", 1989, 90332, 2873644922),
							Car("Ford", "Focus", "White", 2008, 150, 9236498273),
							Car("Voltzwagon", "Jetta", "Black", 2006, 28002, 4673992056),
							Car("Rolls Royce", "Ghost", "Silver", 2005, 10000, 9292983855),
							Car("Mazda", "626", "Blue", 2002, 84754, 7364646463),
							Car("Toyota", "Camry", "Red", 2004, 50332, 2133737227),
							Car("Bugatti", "Veyron 16.4", "White", 2010, 5, 5712893401)};

	
	long long int manualVIN= 2873644922;
	long long int desiredVIN;
	int posLin; // Used to search for VIN 2873644922
	int pos;  // Used for when user wil input the vin they want to search

	//MANUAL LINEAR SEARCH FOR VIN NUMBER
	
		posLin = search(Car_array, SIZE, manualVIN); 

		if (posLin == -1)
			cout << "You do not have a car with the VIN Number 2873644922.\n";
		else 
		{
			cout << "You do have a car on the lot with the VIN Number 2873644922.\n";
		}
		cout << "_______________________________________________________\n";
	// END MANUAL LINEAR SEARCH FOR VIN NUMBER

	// Get the VIN to search for

	 // LINEAR SEARCH FOR VIN NUMBER
		cout << "Enter a VIN number to search for: ";
		cin >> desiredVIN;

	// Linear Search for VIN Number
		pos = search(Car_array, SIZE, desiredVIN); 

		if (pos == -1)
			cout << "You do not have a car with the VIN Number " << desiredVIN << ".\n";
		else 
		{
			cout << "You do have a car on the lot with the VIN Number " << desiredVIN << ".\n";
		}
		cout << "_______________________________________________________\n";

	 // END LINEAR SEARCH FOR VIN NUMBER
	
	// Loop to display car details
		for (int x=0; x<9; x++) {
			Car_array[x].car_details();
		}

		cout << "_______________________________________________________\n";


		//Selection Sort

		cout << " This is the Array of Cars in a selection sort!!\n\n";

		//Sort the Array
		selectionSort(Car_array, SIZE);

		//Display 
		showArray();
		
		//Show Array

		void showArray();
		{
			for (int x=0; x<9; x++) {
					Car_array[x].car_details();
				}
		}
		return 0;

}

If you run this with the class file it will do my linear searches and print out the information! Now I just need to add a sectional sort to that! If you or someone could help me!

Hey guys I appreciate the help and you taking time to look at this for me! I am trying to work on some programs in the back of the book(this is one) to help me study for my test later this week! So it would b great to be able to get this selection sort down pat. And once I get this sort done I plan to do a binary search with it! So please assist me guys!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.