Access Violation

My compiler seems to hate a function I have written.

Page 1 of 1

1 Replies - 4184 Views - Last Post: 05 March 2007 - 05:57 AM Rate Topic: -----

#1 cybermole  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 05-March 07

Access Violation

Posted 05 March 2007 - 03:50 AM

Hey all. I'm a programming student at university, haven't been using C++ all that long, but I can usually figure out the error messages I get and iron them out accordingly. However, this access violation I get when I try to build my program has me stumped.

The overall program is a sort of sales system for a fictional carpet shop (thrilling I know), but it basically starts, reads customer details entered previously into an array of structures, the user can then input some more 'orders' and it will save it all out to a text file at the end.

I can write stuff out to text files fine, but my compiler seems to hate my read from file function. The debugger gives me this Access Violation error:

Project1.exe raised exception class EAccessViolation with message 'Access Violation at address 00402DBD. Write of address 00130008.' Process stopped. Use step or run to continue.

Then the debugger jumps into the istream function. When I comment out my read file function, the rest of the program works fine.

Here are the parts of my program that use this function:


struct RecDetails {
									int	 QuoteNum, GuaranteePeriod;
									char	Name[NAME_LEN];
									float   Cost, DeliveryCharge;
								  }; // structure which declares my array type


RecDetails ReadRecords(RecDetails Quotes[MAX_QUOTES],int MaxQuotes); // Function prototype

RecDetails	Quotes[50]; // Array of structures

ReadRecords(Quotes, MaxQuotes); // Function call in main


RecDetails ReadRecords(RecDetails Quotes[MAX_QUOTES], int MaxQuotes)
{
   ifstream InputFile;
   int I = 0;

   InputFile.open("quotes.txt");
   while(!InputFile.eof())
   {
	  InputFile >> Quotes[I].QuoteNum;
	  InputFile.getline(Quotes[I].Name,NAME_LEN);
	  InputFile >> Quotes[I].Cost;
	  InputFile >> Quotes[I].DeliveryCharge;
	  InputFile >> Quotes[I].GuaranteePeriod;

	  I++;
   }
   InputFile.close();
   MaxQuotes = I;

   return Quotes[MAX_QUOTES];
} // The function itself



As I said, when I comment out the function and the function call, the rest of it works fine, so the problem must be something to do with my ReadRecords function, I just can't figure out what's wrong with it. Any help would be appreciated :)

Is This A Good Question/Topic? 0
  • +

Replies To: Access Violation

#2 horacio  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 67
  • Joined: 29-January 07

Re: Access Violation

Posted 05 March 2007 - 05:57 AM

RecDetails ReadRecords(RecDetails Quotes[MAX_QUOTES], int MaxQuotes)
{
   ifstream InputFile("qoutes.txt", "r");
   int I = 0;
   while(!InputFile.eof())
   {
	  InputFile >> Quotes[I].QuoteNum;
	  InputFile.getline(Quotes[I].Name,NAME_LEN);
	  InputFile >> Quotes[I].Cost;
	  InputFile >> Quotes[I].DeliveryCharge;
	  InputFile >> Quotes[I].GuaranteePeriod;
	  I++;
   }
   InputFile.close();
   MaxQuotes = I;

   return Quotes[MAX_QUOTES];
} // The function itself


this should work..
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1