473,396 Members | 1,779 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

C++ compiled and crashed error

I'm in my last week in High School and my last program has seemed to have been shot. My teacher blames it on the computers and says that my program should run, but I have an error opening my files.
This is my header:

---------------------------------------------------------------------------
#include<iostream.h>
#include<string.h>
#include<fstream.h>
#include<IO.h>
struct carinfo
{
char vehicle[50],manu[50],model[50];
long year,cmilage,vin;
float drentprice,mrentprice;
};
int numhold,x;
carinfo car[50];

void enterinfo();
void output();
void findtype();
void dayrent();
void open();
void sort();
void adddel();
void years();
void main()
{...
}
-------------------------------------------------------------------------
The program works fine until my open function when it displays 00_Done Checking about 30 times, and two lines in there contain randon junk (letters, numbers, music notes, etc.). Then the Just-In-Time debugger comes up and tells me there is something wrong. Visual Basic told me the error is where I bolded it below. I took it home to my free software and said that there was a parse error in that line. Here is my open function:
--------------------------------------------------------------------------
void open()
{ ifstream infile;
infile.open("car.dat", ios::in);
int x;
x=1;
do
{
infile.get(car[x].vehicle, 49);
cout<<car[x].vehicle;
infile.ignore (80,'\n');
infile.get(car[x].manu, 49);
infile.ignore(80,'\n');
cout<<car[x].manu;
infile.get(car[x].model, 49);
infile.ignore(80,'\n');
cout<<car[x].model;
infile.ignore(80,'\n');
infile >> car[x].year;

cout<<car[x].year;
infile >> car[x].cmilage;
cout<<car[x].cmilage;
infile >> car[x].drentprice;
cout<<"Done Checking OPEN.\n";
infile >> car[x].mrentprice;

infile >> car[x].vin;

x=x+1;
}
while(!infile.eof());
numhold=x-1;
infile.close();
}
--------------------------------------------------------------------------
I'm out of ideas, and I'm almost falling into my last resort of making them all strings. If someone knows anything it would be apprecaited. My whole program is below if you must see it.
I'm not on my schools computer right now, so I can't give you in detail what might have flown up onto the screen, and my free software at home is a piece of junk.
--------------------------------------------------------------------------
Whole Program:
#include<iostream.h>
#include<string.h>
#include<fstream.h>
#include<IO.h>
struct carinfo
{
char vehicle[50],manu[50],model[50];
long year,cmilage,vin;
float drentprice,mrentprice;
};
int numhold,x;
carinfo car[50];

void enterinfo();
void output();
void findtype();
void dayrent();
void open();
void sort();
void adddel();
void years();
void main()
{
int choice;
numhold=0;
cout<<"This program keeps vehicle inventory.\n\n";

do
{
cout<<"\n\n\n\n\n";
cout<<"Menu\n";
cout<<"----\n";
cout<<"(1) Enter Vehicles (Erase All Previous Data)\n";
cout<<"(2) Enter More Vehicles\n";
cout<<"(3) Print Vehicles (ABC)\n";
cout<<"(4) Find Type Of Vehicle (car/truck)\n";
cout<<"(5) Show By Expense on Day Rental\n";
cout<<"(6) Delete by VIN\n";
cout<<"(7) Search for a vehicle by year.\n";
cout<<"(8) Exit\n";
cout<<"What is your choice:";
cin>>choice;
switch(choice)
{case 1:
enterinfo();
break;
case 2:
open();
enterinfo();
break;
case 3:
open();
output();
break;
case 4:
open();
sort();
findtype();
break;
case 5:
open();
sort();
dayrent();
break;
case 6:
open();
sort();
adddel();
break;
case 7:
open();
years();
break;
}
}while(choice!=8);
}
void enterinfo()
{
int input,numtimes;
cout<<"How Many Cars Do You Plan On Entering:";
cin>>numtimes;
ofstream outfile;
outfile.open("car.dat",ios::out);
if (outfile)
{
cout<<"Please fill in the information by inputing the indicated number.\n";
for (x=1;x<=numtimes;x++)
{
cin.ignore (80,'\n');
cout<<"Car #"<<x+numhold<<"\n";
cin.ignore (80,'\n');
cout<<"Car (1) or Truck (2):";
cin>>input;
if (input==1)
{
strcpy(car[x+numhold].vehicle,"Car");
outfile<<car[x+numhold].vehicle<<'\n';
cout<<car[x+numhold].vehicle;
}
else
{
strcpy(car[x+numhold].vehicle,"Truck");
outfile<<car[x+numhold].vehicle<<'\n';
cout<<car[x+numhold].vehicle;
}
cin.ignore (80,'\n');
cout<<"Who was the Manufacture?\n";
cout<<"(1) Ford (2) GM (3) Chrysler (4) Toyota (5) Honda:";
cin>>input;
if (input==1)
{
strcpy(car[x+numhold].manu,"Ford");
outfile<<car[x+numhold].manu<<'\n';
cout<<car[x+numhold].manu;
}
else if (input==2)
{
strcpy(car[x+numhold].manu,"GM");
outfile<<car[x+numhold].manu<<'\n';
cout<<car[x+numhold].manu;
}
else if (input==3)
{
strcpy(car[x+numhold].manu,"Chrysler");
outfile<<car[x+numhold].manu<<'\n';
cout<<car[x+numhold].manu;
}
else if (input==4)
{
strcpy(car[x+numhold].manu,"Toyota");
outfile<<car[x+numhold].manu<<'\n';
cout<<car[x+numhold].manu;
}
else
{
strcpy(car[x+numhold].manu,"Honda");
outfile<<car[x+numhold].manu<<'\n';
cout<<car[x+numhold].manu;
}
cin.ignore (80,'\n');
cout<<"\nWhat is the body model:\n";
cout<<"(1) Pickup (2) Sedan (3) Station Wagon (4) Van (5) SUV:";
cin>>input;
if (input==1)
{
strcpy(car[x+numhold].model,"Pick Up");
outfile<<car[x+numhold].model<<'\n';
cout<<car[x+numhold].model;
}
else if (input==2)
{
strcpy(car[x+numhold].model,"Sedan");
outfile<<car[x+numhold].model<<'\n';
cout<<car[x+numhold].model;

}
else if (input==3)
{
strcpy(car[x+numhold].model,"Station Wagon");
outfile<<car[x+numhold].model<<'\n';
cout<<car[x+numhold].model;
}
else if (input==4)
{
strcpy(car[x+numhold].model,"Van");
outfile<<car[x+numhold].model<<'\n';
cout<<car[x+numhold].model;
}
else
{
strcpy(car[x+numhold].model,"SUV");
outfile<<car[x+numhold].model<<'\n';
cout<<car[x+numhold].model;
}
cin.ignore (80,'\n');
cout<<"\n\n";
cout<<"What is the year (####):";
cin>>car[x+numhold].year;
cout<<car[x+numhold].year;
outfile<<car[x+numhold].year<<'\n';
cout<<"\n";
cout<<"What is the current milage:";
cin>>car[x+numhold].cmilage;
cout<<car[x+numhold].cmilage;
cout<<"Done Checking INPUT";
outfile<<car[x+numhold].cmilage<<'\n';
cout<<"\n";
cout<<"What is the daily rental price: $";
cin>>car[x+numhold].drentprice;
outfile<<car[x+numhold].drentprice<<'\n';
cout<<car[x+numhold].drentprice;
cout<<"\n";
cout<<"What is the milage rental price: $";
cin>>car[x+numhold].mrentprice;
outfile<<car[x+numhold].mrentprice<<'\n';
cout<<"\n";
cout<<"What is the VIN number:";
cin>>car[x+numhold].vin;
outfile<<car[x+numhold].vin<<'\n';
cout<<"\n\n\n\n\n\n\n";


} numhold+=numtimes;
}
else
{
cout<<"File not open.\n";
}
outfile.close();
//numhold is a global varible keeping track of the student count.
}
void output()
{
cout<<"\n\n";
cout<<"Type |Manufacturer\n";
cout<<"Model | Year \n";
cout<<"----------------------------------------------------------\n";
for (x=1;x<=numhold;x++)
{
cout<<x<<"\n";
cout<<car[x].vehicle<<"--"<<car[x].manu<<" \n";
cout<<""<<car[x].model<<"--"<<car[x].year<<" \n\n";
cout<<"Current Mileage: "<<car[x].cmilage<<"\n";
cout<<"Daily Rent Price: $"<<car[x].drentprice<<"\n";
cout<<"Milage Rent Price: $"<<car[x].mrentprice<<"\n";
cout<<"VIN: "<<car[x].vin<<"\n";
cout<<"---------------------------------\n\n";
}



cout<<"\n\n\n\n";
}

continued on the next thread due to size....
May 23 '06 #1
3 2058
Here is the rest of it...
void sort()
{
int done;
carinfo placehold;
do
{
done=1;
for (x=1;x<numhold;x++)
{
if ((car[x].drentprice)<(car[x+1].drentprice))
{

placehold=car[x];
car[x]=car[x+1];
car[x+1]=placehold;
done=0;
}
}
}
while (done==0);
}
void findtype()
{
int input,check;
cout<<"What is the type your are looking for? (1) Car (2) Truck:";
cin>>input;
cout<<"\n\n";
cout<<"Type |Manufacturer\n";
cout<<"Model | Year \n";
cout<<"----------------------------------------------------------\n";
check=0;
for (x=1;x<numhold;x++)
{
if (input==1)
{
if (strcmp(car[x].vehicle,"Car"))
{cout<<car[x].vehicle<<"--"<<car[x].manu<<" \n";
cout<<""<<car[x].model<<"--"<<car[x].year<<" \n\n";
cout<<"Current Mileage: "<<car[x].cmilage<<"\n";
cout<<"Daily Rent Price: $"<<car[x].drentprice<<"\n";
cout<<"Milage Rent Price: $"<<car[x].mrentprice<<"\n";
cout<<"VIN: "<<car[x].vin<<"\n";
cout<<"---------------------------------\n\n";
check=1;
}
}
if (input==2)
{
if (strcmp(car[x].vehicle,"Truck"))
{cout<<car[x].vehicle<<"--"<<car[x].manu<<" \n";
cout<<""<<car[x].model<<"--"<<car[x].year<<" \n\n";
cout<<"Current Mileage: "<<car[x].cmilage<<"\n";
cout<<"Daily Rent Price: $"<<car[x].drentprice<<"\n";
cout<<"Milage Rent Price: $"<<car[x].mrentprice<<"\n";
cout<<"VIN: "<<car[x].vin<<"\n";
cout<<"---------------------------------\n\n";
check=1;
}
}
}
if (check==0)
{
cout<<"There are none of your requested type of vehicle.\n";
}
}
void dayrent()
{
float max;
int check;
cout<<"List vehicles under what daily value price: $";
cin>>max;
check=0;
for (x=1;x<numhold;x++)
{
if (car[x].drentprice<max)
{
cout<<car[x].vehicle<<"--"<<car[x].manu<<" \n";
cout<<""<<car[x].model<<"--"<<car[x].year<<" \n\n";
cout<<"Current Mileage: "<<car[x].cmilage<<"\n";
cout<<"Daily Rent Price: $"<<car[x].drentprice<<"\n";
cout<<"Milage Rent Price: $"<<car[x].mrentprice<<"\n";
cout<<"VIN: "<<car[x].vin<<"\n";
cout<<"---------------------------------\n\n";
check=1;
}
}
if (check==0)
{
cout<<"There are none lower than requested price of $"<<max<<"\n";
}
}
void open()
{ ifstream infile;
infile.open("car.dat", ios::in);
int x;
x=1;
do
{
infile.get(car[x].vehicle, 49);
cout<<car[x].vehicle;
infile.ignore (80,'\n');
infile.get(car[x].manu, 49);
infile.ignore(80,'\n');
cout<<car[x].manu;
infile.get(car[x].model, 49);
infile.ignore(80,'\n');
cout<<car[x].model;
infile.ignore(80,'\n');
infile >> car[x].year;
cout<<car[x].year<<;
infile >> car[x].cmilage;
cout<<car[x].cmilage;
infile >> car[x].drentprice;
//cout<<"Done Checking OPEN.\n";
infile >> car[x].mrentprice;

infile >> car[x].vin;

x=x+1;
}
while(!infile.eof());
numhold=x-1;
infile.close();
}
void adddel()
{
int del,y;
cout<<"What is the VIN number you would like to delete:";
cin>>del;
cout<<"\n\n";
ofstream outfile;
outfile.open("car.dat",ios::out);
if (outfile)
{
for (x=1;x<numhold;x++)
{
if ((car[x].vin)==(del))
{
for (y=x;y<numhold;y++)
{
car[y].cmilage=car[y+1].cmilage;
outfile<<car[y].cmilage<<'\n';
car[y].drentprice=car[y+1].drentprice;
outfile<<car[y].drentprice<<'\n';
strcpy(car[y].manu,car[y+1].manu);
outfile<<car[y].manu<<'\n';
strcpy(car[y].model,car[y+1].model);
outfile<<car[y].model<<'\n';
car[y].mrentprice=car[y+1].mrentprice;
outfile<<car[y].mrentprice<<'\n';
strcpy(car[y].vehicle,car[y+1].vehicle);
outfile<<car[y].vehicle<<'\n';
car[y].vin=car[y+1].vin;
outfile<<car[y].vin<<'\n';
car[y].year=car[y+1].year;
outfile<<car[y].year<<'\n';
}
numhold=numhold-1;
}
else
{
cout<<"There are no cars with the VIN "<<del<<".\n";
}
}
}
}
void years()
{
int yr;
cout<<"What is the year you are looking for:";
cin>>yr;


for (x=1;x<numhold;x++)
{
if ((car[x].year)==(yr))
{
cout<<"\n\n";
cout<<"Type |Manufacturer\n";
cout<<"Model | Year \n";
cout<<"----------------------------------------------------------\n";
cout<<car[x].vehicle<<"--"<<car[x].manu<<" \n";
cout<<""<<car[x].model<<"--"<<car[x].year<<" \n\n";
cout<<"Current Mileage: "<<car[x].cmilage<<"\n";
cout<<"Daily Rent Price: $"<<car[x].drentprice<<"\n";
cout<<"Milage Rent Price: $"<<car[x].mrentprice<<"\n";
cout<<"VIN: "<<car[x].vin<<"\n";
cout<<"\n\n";
}
else
{
cout<<"There are no vehicles from the year "<<yr<<".\n";
}
}

}
May 23 '06 #2
Banfa
9,065 Expert Mod 8TB
Having the input file would help too
May 23 '06 #3
Banfa
9,065 Expert Mod 8TB
You do have errors in your open function, I have not been able to get it to work properly and read data correctly from a file, I suspect your teacher either does not know how or can not be bothered to locate the problem.

I run using VC++ (I assume that when you say Visual Basic you mean Visual C++ since this is clearly C++ and not BASIC) as well so what I suggest that you do is put a break point in the open function and step through.

Using those file classes seems to be more hassle than it's worth for just reading strings however for some reason by the time it has finished the first time round the loop it has already got out of sync with the file (i.e. the line it is reading does not contain the data the program thinks it does). The end of file detection is not working and since you have nothing protecting the variable x from going too high for the array car in the end car[x] writes to a location that is not physical memory causing your crash.

Additionally car is declared as

carinfo car[50];

This gives valid indexes in the array car in the range 0 - 49 (50 entries in total) but you always start from 1 so you never use the first entry.

And lastly during all these file operations you never check if it worked or fail, you open a file but you don't check that it has worked, you read from the file but you don't check that it has worked, this is also very bad methodology, where ever possible you should check to make sure that what you just attempted worked and if it didn't handle the error (display a message or something).
May 23 '06 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Subhash | last post by:
Hello, I am trying to generate dynamic graphics using the GD library and TTF fonts. The resulting image shows up fine. But in the apache error_log file the following lines are appended: child...
0
by: Sarah Tanembaum | last post by:
It turns out that the culprit that crashed my php5ts.dll as below is php_threads.dll. Is there any fix for this? Thanks BTW: I remove php_threads.dll from extensions and it works now. Am I...
0
by: Matt | last post by:
Hi everyone, I'm using the command "mysqlcheck --all-databases --auto-repair --silent" and I see these warnings and errors (listed below) on a daily occurrence. What could cause these indexes to...
3
by: Walter Raboch | last post by:
Hi all, a database of one of my customers chrashed two days ago. They have no current backup - dont ask why but I think I have to shoot someone there. I was able to restore most of the tables...
4
by: Fish | last post by:
I have an Access applicaion I built for my buiness. Last week the application crashed and I haven't been able to get it running again since in any form. Now every time I try to run the...
1
by: Arpan | last post by:
I am a newbie ASP.NET programmer. While going through the MSDN ASP.NET tutorial, I came across the following sentence: All ASP.NET code is compiled rather than interpreted which allows early...
5
by: Milsnips | last post by:
hi there, i created a program and compiled it as a setup file, installed it on a computer, and within the application, it access 2 webservices on a my remote web server.. a few weeks ago the...
2
by: > Adrian | last post by:
Suddenly my website is one hell of a mess. I would presume that the style sheets were blown. Is that a thing that could happen maliciously? Does anyone have that kind of experience? Or must I...
1
by: raghulvarma | last post by:
Hai friends, I need to know what would happen when my IIS gets crashed? what would happen to all the projects present inside it?How should I rectify that?What should I do when I am...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.