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<iostre am.h>
#include<string .h>
#include<fstrea m.h>
#include<IO.h>
struct carinfo
{
char vehicle[50],manu[50],model[50];
long year,cmilage,vi n;
float drentprice,mren tprice;
};
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("ca r.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(8 0,'\n');
cout<<car[x].manu;
infile.get(car[x].model, 49);
infile.ignore(8 0,'\n'); cout<<car[x].model;
infile.ignore(8 0,'\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.e of());
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<iostre am.h>
#include<string .h>
#include<fstrea m.h>
#include<IO.h>
struct carinfo
{
char vehicle[50],manu[50],model[50];
long year,cmilage,vi n;
float drentprice,mren tprice;
};
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("c ar.dat",ios::ou t);
if (outfile)
{
cout<<"Please fill in the information by inputing the indicated number.\n";
for (x=1;x<=numtime s;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+=numtim es;
}
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.... 3 2064 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)<(c ar[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("ca r.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(8 0,'\n');
cout<<car[x].manu;
infile.get(car[x].model, 49);
infile.ignore(8 0,'\n');
cout<<car[x].model;
infile.ignore(8 0,'\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.e of());
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("c ar.dat",ios::ou t);
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 ";
}
}
}
Banfa 9,065
Recognized Expert Moderator Expert
Having the input file would help too
Banfa 9,065
Recognized Expert Moderator Expert
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).
Sign in to post your reply or Sign up for a free account.
Similar topics |
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 pid 29141 exit signal Segmentation fault (11)
MMCACHE: PHP crashed on opline 14 of imagettfbbox() at
test.php:13
Seems like it might be a GD...
|
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 going
to miss any
any critical php5 functionality withouth php_threads.dll? Thanks
|
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 be out of order? Another thing I don't
understand is why an increase in the number rows constitutes a warning?
Hoping to here from MySQL users...
|
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 with db2dart. But I cant dump
some of the important ones.
>Table inspection start: D03T060
>>
|
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 application I get the following
standard error.
"Microsoft Access has encountered a problem and needs to close. We are
sorry for the inconvenience."
| |
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 binding, strong typing & just-in-time (JIT) compiling to native code.
What do "compiled rather than interpreted" & "early binding" mean? Also since...
|
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 server crashed and i re-created the
webservice virtual directory, and now the webservices within the app dont
work.
i created a new test app to call...
|
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 recon with some chance event in which
I myself had no hand at all? My website runs
at a host with an excellent reputation.
Adrian.
|
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 asked to rectify the IIS which is crashed and the web page only specifies "Server Crashed" and no other information is provided?
Could you plz specify a...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| |