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

Instead of a bunch of parallel arrays like this:

int vin[SIZE]; 
string brand[SIZE];
string model[SIZE];
int year[SIZE];
int mileage[SIZE];
double mpg[SIZE];

try a single array of structs like this:

struct Car
{
    int vin;
    string brand;
    //etc.
};

Car car[SIZE];

And all your function signatures become: void findvin( const Car& car ); Much neater!

As for your errors, the thing to do first is to print out the data right after you've read it in. I suspect you have not read it in properly.

thanks. in passing the structs into the functions i am getting errors.

void findvin( const Car& car ); //my prototype
findvin(car); function call

i am getting this error:
24: in passing argument 1 of `void findvin(const Car&)'
86: could not convert `&car' to `const Car&'

Oops. Looks like I gave you the wrong prototype. Try: void findvin( const Car* car ); Also, remember to use SIZE and not the number 30 in you code (since that's what SIZE is for).

it gave me

Undefined                       first referenced
 symbol                             in file
carsyear(Car const*)                /var/tmp//ccAqFXYo.o
carsbrand(Car const*)               /var/tmp//ccAqFXYo.o
leastmileage(Car const*)            /var/tmp//ccAqFXYo.o
carsmodel(Car const*)               /var/tmp//ccAqFXYo.o
highestmileage(Car const*)          /var/tmp//ccAqFXYo.o
findvin(Car const*)                 /var/tmp//ccAqFXYo.o

isnt an array automatically passed by reference?

I forgot that you will need to pass in the size of the array as well (unless you can change it to a vector). Here's the idea:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

struct Car
{
    int vin;
    //etc.
};

void showVins( const Car* car, size_t size );

const int SIZE = 30;

int main()
{
    Car car[ SIZE ];
    
    ifstream infile( "usedcars.txt" );
    if( !infile )
    {
        cerr << "Cannot open file\n";
        exit( -1 );
    }

    size_t size = 0;
    while( (size < SIZE) && (infile >> car[ size ].vin) )
    {
        // etc..
        ++size;
    }

    infile.close();

    showVins( car, size );
}


void showVins( const Car* car, size_t size )
{
    for( size_t i = 0; i < size; ++i )
    {
        cout << car[ i ].vin << endl;
    }
}
This article has been dead for over six months. Start a new discussion instead.