Program Crashing When Compiled

I am trying to make this program to give me the cheapest price of the cars within the array. When I compile and run it, I do get the cheapest price. However, the message does not appear which should say the price of this car is cheapest and stuff (as you can see in the code below). Moreover, the program crashes. Please help.

HOW DO I DO THE FOLLOWING QUESTIONS?

8. CHANGE this program so that it now displays the information about the cheapest car in the array.
9. CHANGE this program so that it now displays the information about the one with the smallest engine in the array.
10. CHANGE this program so that it displays the information about the most expensive, the least expensive and the smallest engine. That is, it displays stuff about 3 different cars at the end. You will need to think carefully about all the data your program needs to remember while looping through the array.


// Session6.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <iomanip> // only used to tidy up the console output here
#include <string>
using namespace std;

const int MAXCARS =5; //Always use this instead of magic numbers in code!

int main()
{
string ModelName[MAXCARS] = {"Fiesta", "Impreza", "Gallardo", "Corsa", "Polo"};
string Registration[MAXCARS] = {"FH52ABC", "SUB123", "LAM80", "AH56XYZ", "NG11BNS"};
double EngineSize[MAXCARS] = {1.56, 2.5, 6.5, 1.4, 1.5};
double Price[MAXCARS] = {9695, 17737, 201900, 7895, 10490};

int IndexMostCheap;
double PriceMostCheap;

// initialise to the details of first in array
IndexMostCheap=0;
PriceMostCheap = Price[IndexMostCheap];

// display a header line - note use of \t for tabs to try to line up - not successful, but I'm not bothered
cout<<"Model"<<"\t\tReg Num"<<"\tPrice"<<"\tEngine Size"<<endl;
// note use here of iomanip functions to set the width of EACH field (eg 10 chars) and filling the spare with ' '
// this makes for a nicer alignment than the commented out line which gets messed up by the Lamborghini gallardo
for (int i=0; i<MAXCARS; i--)
{
//cout<<ModelName[i]<<"\t"<<Registration[i]<<"\t"<<Price[i]<<"\t"<<EngineSize[i]<<endl;
cout<<setfill(' ')<<setw(10)<<ModelName[i];
cout<<setfill(' ')<<setw(10)<<Registration[i];
cout<<setfill(' ')<<setw(10)<<Price[i];
cout<<setfill(' ')<<setw(10)<<EngineSize[i];
cout<<endl;

// now find out of this car is the most expensive
if (Price[i]<PriceMostCheap)
{
IndexMostCheap =i; //update this index
PriceMostCheap = Price[i]; //update this price
}
}

// gone through all the cars.
// now can display details about most expensive car
cout<<"Most cheapest Car is "<<ModelName[IndexMostCheap]<<" at a price of "<<Price[IndexMostCheap]<<endl;
cout<<"With reg num of "<<Registration[IndexMostCheap]<<" and engine size of "<<EngineSize[IndexMostCheap]<<endl;

return 0;
}
Last edited on
Topic archived. No new replies allowed.