Hey everyone,
I' m currently having trouble getting the following code to work.
What I'm trying to do is getting my program to do read from a text file which has the following information:
234323 c
343212 d
323432 a
763634 b
The corresponding information shown above will correspond as follow '234323' is customer ID and 'c' correspond that it belongs to '23'4323' and so on...
What I am trying to do is getting one my function to display an error message if the information obtain in the text file is not equal to a, b,c or d.
I got the first part of my function working already which required the data found in the text file to be greater then 0. If any part of the text file contain negative number it will prompt the user with an invalid input and advise them of the invalid line.
Now for second part, I need help in which required the program to read the text file. If the information contain in the text file is not equal to a, b, c or d then the program will prompt with the error line.
Here is my current code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
const int ArrayRecord = 300;
int CustID[ArrayRecord];
char StationID[ArrayRecord];
string Address[ArrayRecord];
int Checkfile(int & Record)
{
ifstream openfile ( "tolldata.txt" ); //Declare Input File stream as openfile
//then open the call marks.txt file
while (!openfile.eof() && Record < ArrayRecord && !openfile.fail())
{
//While loop, As long as the statement not equal to end of file,
//record is lesser then ArrayRecord, and opening file does not fail
//Then perform the following.
openfile >> CustID[Record];
//Open marks.txt file and store into
//StudentID[Record] Array.
if (CustID[Record] < 0 || openfile.fail())
/*If StudentID[Record] is greater then 999999 or lesser then 0
or record file fail to open, prompt user with error message
and error line.*/
{
cout << "\n\tError - Invalid or miss match Customer ID record.\n";
cout << "\tPlease refer to line " << Record +1 <<
" of tolldata.txt text file.\n\n" ;
system( "pause" ); //perform system pause and exit
//record and terminate program
exit(1);
return 0;
}
openfile >> StationID[Record];
if (StationID[Record] != 'A' || StationID[Record] != 'a' || StationID[Record] != 'B' || StationID[Record] != 'b' || StationID[Record] != 'C' || StationID[Record] != 'c' || StationID[Record] != 'D' || StationID[Record] != 'd' || openfile.fail())
{
cout << "\n\tONE\n\tError - Invalid or miss match Customer ID record.\n";
cout << "\tPlease refer to line " << Record +1 <<
" of tolldata.txt text file.\n\n" ;
system( "pause" ); //perform system pause and exit
//record and terminate program
exit(1);
return 0;
}
Record++;
}
openfile.close();
}
int main()
{
int Record = 0;
ifstream openfile ( "tolldata.txt" ); /*Declare input file stream mark.txt*/
if (openfile.is_open())/*If text file is open excute the following*/
{
cout << "\t****************************************************\n\n";
cout << "\t Please wait...\n\n" <<
"\t The following file tolldata.txt has been found.\n\n";
cout << "\t****************************************************\n\n";
Checkfile(Record);
}
else /*If text file doesn't exist display message and terminate program*/
{
cout << "\t****************************************************\n\n";
cout << "\tError - Opening tolldata.txt file has fail.\n";
cout << "\tThe program will now terminate.\n\n";
cout << "\t****************************************************\n\n";
}
system("pause");
return 0;
}
Any help in solving my problem would much appreciated. So feel free to reply to my post.
Regards,
tuannie