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

I think it should be this.

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()
       )

I think it should be this.

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()
       )

Thank you for your respond.

If I was to use && wouldn't that only work if all the information contain in the text file as to be a, b, c, and d for the if statement to run?

What if in the text file I was to have:

234323 c
343212 d
323432 e <=== only of these line that does not match.
763634 b

From what I remember the AND statement will only be true if the rest of the line is true. Correct me if I'm wrong.

One problem is that you are attempting to open the same file twice -- once in main() and again in Checkfile(). After opening the file in main() just pass the ifstream object to Checkfile.

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];

Above is incorrect way to write that loop -- ifstream returns null on eof.

while (Record < ArrayRecord && openfile >> CustID[Record] )
     {  
         //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.

Thank you for your respond.

If I was to use && wouldn't that only work if all the information contain in the text file as to be a, b, c, and d for the if statement to run?

What if in the text file I was to have:

234323 c
343212 d
323432 e <=== only of these line that does not match.
763634 b

From what I remember the AND statement will only be true if the rest of the line is true. Correct me if I'm wrong.

I think my logic is correct. If you use ||, since 'B'!='a', you will get an error even if you input 'B'.
What dragon say is also correct, you are opening the file twice. You can correct them and check if my logic is correct or not.

I think my logic is correct. If you use ||, since 'B'!='a', you will get an error even if you input 'B'.
What dragon say is also correct, you are opening the file twice. You can correct them and check if my logic is correct or not.

Thank you all for your quick and very helpful help, but how would I go about in fixing that problem by not having to open the same file again?

To pass the ifstream from main() do i need to have my code to something like??

int checkfile(int & Record, &openfile)
{
}

how would I go about in fixing that problem by not having to open the same file again?

To pass the ifstream from main() do i need to have my code to something like??

int checkfile(int & Record, &openfile)
{
}

Yes you could do that or shift the file checking message also in to the CheckFile Function.

int Checkfile(int & Record)
{
     ifstream openfile ( "tolldata.txt" ); //Declare Input File stream as openfile
    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";
       }
       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";
              exit(1);
           }

                                        //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++;
   
}
int main()
{
    int Record = 0;
    
    Checkfile(Record);
    system("pause");
    return 0;
}

If I was to use && wouldn't that only work if all the information contain in the text file as to be a, b, c, and d for the if statement to run?

No, you're reading the statement wrongly, that "if" statement says
"If input isn't 'a', AND input isn't 'A', AND input isn't 'B', AND input isn't 'b'.... etc"

You could probably cut those conditions down to a small function, if you wanted clearer code

bool isValid(const char& c)
{
    std::string acceptable("abcdABCD");
    if (acceptable.find(c) == acceptable.npos)
        return false;           //npos is the "end of string" token
    else
        return true;
}

Then, later in your code you could say something like

if ( isValid(input_character) && !openfile.fail() )

No, you're reading the statement wrongly, that "if" statement says
"If input isn't 'a', AND input isn't 'A', AND input isn't 'B', AND input isn't 'b'.... etc"

You could probably cut those conditions down to a small function, if you wanted clearer code

bool isValid(const char& c)
{
    std::string acceptable("abcdABCD");
    if (acceptable.find(c) == acceptable.npos)
        return false;           //npos is the "end of string" token
    else
        return true;
}

Then, later in your code you could say something like

if ( isValid(input_character) && !openfile.fail() )

wow that is getting too complicated for me, I'm only a newbie into c++.

Okay, I have fixed up my code now. But one problem I'm currently having is when I executed it. It keeps saying that I have an error in line 6 when I try it try to read the following information from a text file

787867 a
145236 c
454654 a
566765 a
254788 a

This is my updated 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 tolldata.txt file
    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";

       }
       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";
           }

    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;
    
              Checkfile(Record);
  system("pause");
return 0;
}

Could someone please tell me why it keep saying that?

What is the error message? What is line number 6? We can't go on counting you know...When you say execute do you mean compiling or running the program?

It's probably to do with the fact that your while loop is reading until EOF is set - in C++ this is the wrong way to do it, because your program will crash reading past the end of openfile (which is actually a stream, not a file). instead, use while (openfile) which will continue as long as there is data in the stream.

What is the error message? What is line number 6? We can't go on counting you know...When you say execute do you mean compiling or running the program?

I mean compiling it, this is what I get:

****************************************************

         Please wait...

         The following file tolldata.txt has been found.

        ****************************************************


        Error - Invalid or miss match Station ID record.
        Please refer to line 6 of tolldata.txt text file.

Press any key to continue . . .

I mean compiling it, this is what I get:

****************************************************

         Please wait...

         The following file tolldata.txt has been found.

        ****************************************************


        Error - Invalid or miss match Station ID record.
        Please refer to line 6 of tolldata.txt text file.

Press any key to continue . . .

Bench is right. You are reading past the EOF. And it is not compiling. You are executing ( running ) the program. This is a runtime error.

>>Bench is right. You are reading past the EOF.

That's what I said in my original post -- that loop will not work the way it is coded. If some people (OP) would learn to read :rolleyes:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.