i am trying to get my program to cout only certain arrays but i keep getting 0's and bus errors and segmentation faults. i think i have the general idea but i cant find a way to fix this problem.
#include<iostream>
#include<fstream>
#include<cstring>
#include<iomanip>
using namespace std;
void findvin(int[],string[],string[],int[],int[],double[]);
void carsbrand (int[],string[],string[],int[],int[],double[]);
void carsmodel(int[],string[],string[],int[],int[],double[]);
void carsyear(int[],string[],string[],int[],int[],double[]);
void leastmileage(int[],string[],string[],int[],int[],double[]);
void highestmileage (int[],string[],string[],int[],int[],double[]);
int main()
{
const int SIZE=30;
int vin[SIZE];
string brand[SIZE];
string model[SIZE];
int year[SIZE];
int mileage[SIZE];
double mpg[SIZE];
int choice=0;
ifstream infile;
infile.open("usedcars.txt");
int i=0;
if (!infile)
cout<<"Could Not Open File"<<endl;
else
{
while (!infile.eof() && i<30)
{
infile>>vin[i];
getline (infile, brand[i]);
getline (infile, model[i]);
infile>>year[i];
infile>>mileage[i];
infile>>mpg[i];
i++;
}
cout<<fixed<<showpoint<<setprecision(2);
do
{
cout<<"Welcome To the Used Car Management Program!"<<endl;
cout<<"1. Find Car by VIN#"<<endl;
cout<<"2. List Cars by Brand"<<endl;
cout<<"3. List Cars by Model"<<endl;
cout<<"4. List Cars by Year"<<endl;
cout<<"5. Find Car with Least Mileage"<<endl;
cout<<"6. Find Car with Highest MPG(Miles Per Gallon)"<<endl;
cout<<"7. Quit"<<endl;
cout<<"Enter your choice: "<<endl;
cin>>choice;
if (choice<0 || choice>7)
{
cout<<"Enter a valid choice"<<endl;
cin>>choice;
}
}
while (choice<0 || choice>7);
switch (choice)
{
case 1: findvin(vin,brand,model,year,mileage,mpg);
break;
case 2: carsbrand(vin,brand,model,year,mileage,mpg);
break;
case 3: carsmodel(vin,brand,model,year,mileage,mpg);
break;
case 4: carsyear(vin,brand,model,year,mileage,mpg);
break;
case 5: leastmileage(vin,brand,model,year,mileage,mpg);
break;
case 6: highestmileage(vin,brand,model,year,mileage,mpg);
break;
case 7: cout<<"Goodbye!"<<endl;
break;
}
}
infile.close();
return 0;
}
void findvin (int vin[],string brand[],string model[],int year[],int mileage[],double mpg[])
{
int num;
const int SIZE=30;
cout<<"Enter VIN Number: "<<endl;
cin>>num;
for(int i=0; i<SIZE; i++)
{
if (vin[i] == num)
{
cout<<vin[i]<<" "<<brand[i]<<" "<<model[i]<<" "<<year[i]<<endl;
}
}
}
void carsbrand (int vin[],string brand[],string model[],int year[],int mileage[],double mpg[])
{
const int SIZE=30;
for (int i=0; i<SIZE;i++)
cout<<brand[i]<<" "<<model[i]<<" "<<year[i]<<endl;
}
//my carsbrand function shows what it is supposed to but also gives me a lot of 0's and random numbers
void carsmodel (int vin[],string brand[],string model[],int year[],int mileage[],double mpg[])
{
const int SIZE=30;
for (int i=0; i<SIZE;i++)
cout<<model[i]<<" "<<brand[i]<<" "<<year[i]<<endl;
}
void carsyear(int vin[],string brand[],string model[],int year[],int mileage[],double mpg[])
{
const int SIZE=30;
for (int i=0; i<SIZE;i++)
cout<<year[i]<<" "<<brand[i]<<" "<<model[i]<<endl;
}
void leastmileage (int vin[],string brand[],string model[],int year[],int mileage[],double mpg[])
{
int i = 1;
int min=mileage[0];
while (i<30)
{
if (min<mileage[i])
min=mileage[i];
i++;
}
cout<<"The Least Mileage is: "<<endl;
cout<<brand[i]<<" "<<model[i]<<" "<<year[i]<<" "<<mileage[i]<<endl;
}
void highestmileage (int vin[],string brand[],string model[],int year[],int mileage[],double mpg[])
{
int i = 1;
int max=mileage[0];
while (i<30)
{
if (max>mileage[i])
max=mileage[i];
i++;
}
cout<<"The Highest MPG is: "<<endl;
cout<<brand[i]<<" "<<model[i]<<" "<<year[i]<<" "<<mpg[i]<<endl;
}
//this function gives me a bus error