Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Microsoft error report generated when trying to run this code. Why?? 1

Status
Not open for further replies.

cols2910

Programmer
Sep 17, 2003
2
0
0
GB
Hi all

I have been trying to run the following code. The .exe file is created without a hitch, but when I try to run the file, I am greeted with the "records.exe has encountered a problem and needs to close" dialogue box.

The code came from the book C++ Programming in Easy Steps, and all the other examples have worked perfectly until this one.

Can anyone shed any light on this?

It is meant to read a selection of records from a text file, and I have included this file at the end of the code.

I am running WindowsXP Home Edition.

Many thanks in advance, and here is the simple example code.

<CODE>

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

int main()
{
// Create a string array
string str[20];

int i = 0, j = 0, last; // Create counter variables

ifstream myFile(&quot;records.txt&quot;); // Create input file object

if (! myFile) // Always check this
{
cout << &quot;Unable to open input file&quot; << endl;
return -1;
}

while (! myFile.eof()) // Loop through data...
{
if ((i + 1) % 4 ==0) getline(myFile, str[i++]); //up to newline or
else getline(myFile, str[i++], '\t'); // up to each tab delimiter
}

last = i;
i = 0;
while(i < last) // Display each record formatted
{
cout << &quot;\nRecord Number:\t&quot; << ++j << endl;
cout << &quot;Forename:\t&quot; << str[i++] << endl;
cout << &quot;Surname:\t&quot; << str[i++] << endl;
cout << &quot;Department:\t&quot; << str[i++] << endl;
cout << &quot;Tel.No.:\t&quot; << str[i++] << endl;
}
myFile.close(); // Remember to close the file
return 0;
}

</CODE>

<TEXTFILE>

John Smith Sales 555-1234
Mary Jones Wages 555-9876
Paul Harris Accts 555-4321
Alice Berry Recep 555-6789
Tony Denton Sales 555-4567


</TEXTFILE>
 
Do you have a debugger that you can step through to find what line causes the problem ?

K
 
If that code comes from a Tutorial, I'd say its a crappy book.

The whole thing with having i++ inside [] is terrible I think, it obfuscates the code quite a lot.

There are other things to say about it too, like why arent all variables initialized when constructed. And why are some constructed long before they are used.

Anyway, to the problem:
Code:
  cout << &quot;\nRecord Number:\t&quot; << ++j << endl;
        cout << &quot;Forename:\t&quot;   << str[i++] << endl;
    cout << &quot;Surname:\t&quot;    << str[i++] << endl;
    cout << &quot;Department:\t&quot; << str[i++] << endl;
    cout << &quot;Tel.No.:\t&quot;    << str[i++] << endl;
[code]
There are 4 i++ here...are you sure i is never >=20?




/Per
[sub]
if (typos) cout << &quot;My fingers are faster than my brain. Sorry for the typos.&quot;;
[/sub]
 
The program would work, but I'll bet you a dollar you have an extra line at the end of your &quot;records.txt&quot; file that is causing this problem. Make sure the fields are seperated by tabs, and the last line of text is also the last line of the text file. (In notepad for instance, hitting the right arrow at the end of the last line of text should do nothing. It should not move you down to the beginning of the next line, because the last line of text *is* the last line of the file.)

&quot;Fixing&quot; the text file only gets it to run. Let's look at why this is happening.

The program tests for EOF before reading each field. Then it assumes that the field will be there. That assumption only hods if a field always follows when EOF != true. Because of the extra <return> in your data file, this assumption did not hold in your case. This caused the program to write and later read an unallocated string slot in the array. C++ has no protection against such a thing, so at this point you had entered the &quot;undefined&quot; region of C++, which is where the sea monsters live, and ships disappear without a trace.

This is not how C++ programs should be written. Look ahead and see if the author wrote it this way as part of some larger point. If not, and if the examples don't get less fragile as the book moves past the intro stuff, this may not be the best book for learning C++.
 
Cheiron

Many thanks for the solution, but more importantly the explanation. That was exactly what the problem was.

Many thanks again mate.

Cheers
Cols2910
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top