I have to write a program to take in user input (VIN#, Car Maker, Car Model) and reorganize it based on either alphabetical order (for Car Maker only) or numerical order (VIN#). I'm using g++ to compile the code on CentOS. I've done a majority of the code but I get this error:
('1' is the name of the compiled code)
-bash-2.05b$ ./1
Welcome to Chris's Friendly Automobile Program
Choices for commands are:
A = Add: enter another car
V = VIN: view the cars in order by VIN number
M = Make: view cars in alphabetical order of Make
R = Remove a car
Q = Quit
Enter your choice (A, V, M, R, Q): a
Enter VIN#: 111222
Enter Make: caddy
Enter Model: cts
Enter fee: 90.00
Enter your choice (A, V, M, R, Q): v
VIN Make Model Fee
111222 caddy cts 90.00
Enter your choice (A, V, M, R, Q): m
VIN Make Model Fee
111222 caddy cts 90.00
Enter your choice (A, V, M, R, Q): a
Enter VIN#: 222222
Enter Make: toyota
Segmentation fault
here is my code
#include <iostream>
#include <iomanip>
#include <cstring>
using namespace std;
//Welcome message and intructions
void *Greeting()
{
cout<<"Welcome to Chris's Friendly Automobile Program\n"
<<"Choices for commands are:\n"
<<"\t A = Add: enter another car\n"
<<"\t V = VIN: view the cars in order by VIN number\n"
<<"\t M = Make: view cars in alphabetical order of Make\n"
<<"\t R = Remove a car\n"
<<"\t Q = Quit\n";
}
//Function for inputting data
void *AddData(int *VIN_Number, char *Make, char *Model, float *Fee)
{
cout<<"Enter VIN#: ";
cin>>*VIN_Number;
cin.ignore(256, '\n');
cout<<"Enter Make: ";
cin.getline(Make,256);
cout<<"Enter Model: ";
cin.getline(Model, 256);
cout<<"Enter fee: ";
cin>>*Fee;
cin.ignore(256, '\n');
cout<<"\n";
}
//Function for sorting based on VIN
void *VINsort(int *VIN_Numbers, char **Makers, char **Models, float *Fees, int *CarArrayIndex)
{
for (int i=0; i<*CarArrayIndex; i++)
{
int SmallerIndex = i;
for (int j=i+1; j<*CarArrayIndex; j++)
{
if (VIN_Numbers[SmallerIndex]>VIN_Numbers[j])
SmallerIndex=j;
}
if (SmallerIndex!=i)
{
int *VIN_temp = new int;
char *Make_temp = new char[256];
char *Model_temp = new char[256];
float *Fee_temp = new float;
*VIN_temp = VIN_Numbers[i];
strcpy(Make_temp , Makers[i]);
strcpy(Model_temp, Models[i]);
*Fee_temp = Fees[i];
VIN_Numbers[i] = VIN_Numbers[SmallerIndex];
strcpy(Makers[i], Makers[SmallerIndex]);
strcpy(Models[i], Models[SmallerIndex]);
Fees[i] = Fees[SmallerIndex];
strcpy(Models[i], Models[SmallerIndex]);
Fees[i] = Fees[SmallerIndex];
VIN_Numbers[SmallerIndex] = *VIN_temp;
strcpy(Makers[SmallerIndex],Make_temp);
strcpy(Models[SmallerIndex], Model_temp);
Fees[SmallerIndex] = *Fee_temp;
delete VIN_temp;
delete Make_temp;
delete Model_temp;
delete Fee_temp;
}
}
//Display sorted list
cout<<"\t"<<"VIN\t"<<"Make\t"<<"Model\t"<<"Fee\t"<<endl;
for(int i=0; i<*CarArrayIndex; i++)
{
cout<<"\t"<<setw(5)<<setfill('0')<<VIN_Numbers[i]<<"\t"<<Makers[i]<<"\t"<<Models[i]<<"\t"
<<right<<setprecision(2)<<showpoint<<fixed<<Fees[i]<<endl;
}
cout<<"\n";
}
//Function for sorting based on Make of car
void *MakeSort(int *VIN_Numbers, char **Makers, char **Models, float *Fees, int *CarArrayIndex)
{
for (int i=0; i<*CarArrayIndex; i++)
{
int SmallerIndex = i;
for (int j=i+1; j<*CarArrayIndex; j++)
{
if (strcmp(Makers[SmallerIndex], Makers[j])>0)
SmallerIndex=j;
}
if (SmallerIndex!=i)
{
int *VIN_temp = new int;
char *Make_temp = new char[256];
char *Model_temp = new char[256];
float *Fee_temp = new float;
*VIN_temp = VIN_Numbers[i];
strcpy(Make_temp , Makers[i]);
strcpy(Model_temp, Models[i]);
*Fee_temp = Fees[i];
VIN_Numbers[i] = VIN_Numbers[SmallerIndex];
strcpy(Makers[i], Makers[SmallerIndex]);
strcpy(Models[i], Models[SmallerIndex]);
Fees[i] = Fees[SmallerIndex];
VIN_Numbers[SmallerIndex] = *VIN_temp;
strcpy(Makers[SmallerIndex],Make_temp);
strcpy(Models[SmallerIndex], Model_temp);
Fees[SmallerIndex] = *Fee_temp;
Fees[SmallerIndex] = *Fee_temp;
delete VIN_temp;
delete Make_temp;
delete Model_temp;
delete Fee_temp;
}
}
//Display sorted list
cout<<"\t"<<"VIN\t"<<"Make\t"<<"Model\t"<<"Fee\t"<<endl;
for(int i=0; i<*CarArrayIndex; i++)
{
cout<<"\t"<<setw(5)<<setfill('0')<<VIN_Numbers[i]<<"\t"<<Makers[i]<<"\t"<<Models[i]<<"\t"
<<right<<setprecision(2)<<showpoint<<fixed<<Fees[i]<<endl;
}
cout<<"\n";
}
int main()
{
//Declare variables for main function
char *choice = new char;
int *VIN_Numbers = new int[15];
char **Makers = new char*[15];
char **Models = new char*[15];
float *Fees = new float[15];
int *CarArrayIndex = new int;
*CarArrayIndex = 0;
for(int i=0; i<15; i++)
{
Makers[i]=new char[256];
Models[i]=new char[256];
//Welcome screen and instructions
Greeting();
//Prompt to continue through program
while (true)
{
cout<<"Enter your choice (A, V, M, R, Q): ";
cin>>*choice;
switch (*choice)
{
case 'A':
case 'a': AddData(&VIN_Numbers[*CarArrayIndex], Makers[*CarArrayIndex],
Models[*CarArrayIndex], &Fees[*CarArrayIndex]); (*CarArrayIndex)++; break;
case 'V':
case 'v': VINsort(VIN_Numbers, Makers, Models, Fees, CarArrayIndex); break;
case 'M':
case 'm': MakeSort(VIN_Numbers, Makers, Models, Fees, CarArrayIndex); break;
case 'R':
case 'r': DeleteVIN(); return 0; break;
case 'Q':
case 'q': cout<<"Have a nice day. Bye."<<endl; return 0; break;
}
}
}
return 0;
}
from what I've read of segmentation faults, there is a place in memory that cannot be accessed. I'd imagine one of my pointer variables is deleted somewhere. I'm stuck and not sure what to do. Any help would be vastly appreciated.
Thanks!
Chris
(ps. there are so many pointer variables because our teacher wants us to learn how to use them. every variable must be a pointer)