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("records.txt" // Create input file object
if (! myFile) // Always check this
{
cout << "Unable to open input file" << 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 << "\nRecord Number:\t" << ++j << endl;
cout << "Forename:\t" << str[i++] << endl;
cout << "Surname:\t" << str[i++] << endl;
cout << "Department:\t" << str[i++] << endl;
cout << "Tel.No.:\t" << 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>
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("records.txt" // Create input file object
if (! myFile) // Always check this
{
cout << "Unable to open input file" << 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 << "\nRecord Number:\t" << ++j << endl;
cout << "Forename:\t" << str[i++] << endl;
cout << "Surname:\t" << str[i++] << endl;
cout << "Department:\t" << str[i++] << endl;
cout << "Tel.No.:\t" << 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>