Need Help in Reading characters from a text file

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2006
Posts: 11
Reputation: tuannie is an unknown quantity at this point 
Solved Threads: 0
tuannie tuannie is offline Offline
Newbie Poster

Need Help in Reading characters from a text file

 
0
  #1
Apr 20th, 2006
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:
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. const int ArrayRecord = 300;
  8. int CustID[ArrayRecord];
  9. char StationID[ArrayRecord];
  10. string Address[ArrayRecord];
  11.  
  12. int Checkfile(int & Record)
  13. {
  14. ifstream openfile ( "tolldata.txt" ); //Declare Input File stream as openfile
  15. //then open the call marks.txt file
  16. while (!openfile.eof() && Record < ArrayRecord && !openfile.fail())
  17. {
  18. //While loop, As long as the statement not equal to end of file,
  19. //record is lesser then ArrayRecord, and opening file does not fail
  20. //Then perform the following.
  21.  
  22. openfile >> CustID[Record];
  23. //Open marks.txt file and store into
  24. //StudentID[Record] Array.
  25.  
  26. if (CustID[Record] < 0 || openfile.fail())
  27. /*If StudentID[Record] is greater then 999999 or lesser then 0
  28.   or record file fail to open, prompt user with error message
  29.   and error line.*/
  30. {
  31. cout << "\n\tError - Invalid or miss match Customer ID record.\n";
  32. cout << "\tPlease refer to line " << Record +1 <<
  33. " of tolldata.txt text file.\n\n" ;
  34.  
  35. system( "pause" ); //perform system pause and exit
  36. //record and terminate program
  37. exit(1);
  38. return 0;
  39. }
  40. openfile >> StationID[Record];
  41.  
  42. 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())
  43. {
  44. cout << "\n\tONE\n\tError - Invalid or miss match Customer ID record.\n";
  45. cout << "\tPlease refer to line " << Record +1 <<
  46. " of tolldata.txt text file.\n\n" ;
  47.  
  48. system( "pause" ); //perform system pause and exit
  49. //record and terminate program
  50. exit(1);
  51. return 0;
  52. }
  53. Record++;
  54.  
  55. }
  56. openfile.close();
  57. }
  58.  
  59.  
  60. int main()
  61. {
  62. int Record = 0;
  63.  
  64. ifstream openfile ( "tolldata.txt" ); /*Declare input file stream mark.txt*/
  65. if (openfile.is_open())/*If text file is open excute the following*/
  66. {
  67. cout << "\t****************************************************\n\n";
  68. cout << "\t Please wait...\n\n" <<
  69. "\t The following file tolldata.txt has been found.\n\n";
  70. cout << "\t****************************************************\n\n";
  71. Checkfile(Record);
  72. }
  73. else /*If text file doesn't exist display message and terminate program*/
  74. {
  75. cout << "\t****************************************************\n\n";
  76. cout << "\tError - Opening tolldata.txt file has fail.\n";
  77. cout << "\tThe program will now terminate.\n\n";
  78. cout << "\t****************************************************\n\n";
  79. }
  80. system("pause");
  81. return 0;
  82. }

Any help in solving my problem would much appreciated. So feel free to reply to my post.

Regards,
tuannie
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1,496
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: Need Help in Reading characters from a text file

 
0
  #2
Apr 20th, 2006
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()
       )
バルサミコ酢やっぱいらへんで
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 11
Reputation: tuannie is an unknown quantity at this point 
Solved Threads: 0
tuannie tuannie is offline Offline
Newbie Poster

Re: Need Help in Reading characters from a text file

 
0
  #3
Apr 20th, 2006
Originally Posted by WolfPack
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,331
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1453
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Need Help in Reading characters from a text file

 
0
  #4
Apr 20th, 2006
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.
  1. while (!openfile.eof() && Record < ArrayRecord && !openfile.fail())
  2. {
  3. //While loop, As long as the statement not equal to end of file,
  4. //record is lesser then ArrayRecord, and opening file does not fail
  5. //Then perform the following.
  6.  
  7. openfile >> CustID[Record];

Above is incorrect way to write that loop -- ifstream returns null on eof.
  1. while (Record < ArrayRecord && openfile >> CustID[Record] )
  2. {
  3. //While loop, As long as the statement not equal to end of file,
  4. //record is lesser then ArrayRecord, and opening file does not fail
  5. //Then perform the following.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1,496
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: Need Help in Reading characters from a text file

 
0
  #5
Apr 20th, 2006
Originally Posted by tuannie
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.
バルサミコ酢やっぱいらへんで
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 11
Reputation: tuannie is an unknown quantity at this point 
Solved Threads: 0
tuannie tuannie is offline Offline
Newbie Poster

Re: Need Help in Reading characters from a text file

 
0
  #6
Apr 20th, 2006
Originally Posted by WolfPack
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)
{
}
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1,496
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: Need Help in Reading characters from a text file

 
0
  #7
Apr 20th, 2006
Originally Posted by tuannie
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.

  1. int Checkfile(int & Record)
  2. {
  3. ifstream openfile ( "tolldata.txt" ); //Declare Input File stream as openfile
  4. if (openfile.is_open())/*If text file is open excute the following*/
  5. {
  6. cout << "\t****************************************************\n\n";
  7. cout << "\t Please wait...\n\n" <<
  8. "\t The following file tolldata.txt has been found.\n\n";
  9. cout << "\t****************************************************\n\n";
  10. }
  11. else /*If text file doesn't exist display message and terminate program*/
  12. {
  13. cout << "\t****************************************************\n\n";
  14. cout << "\tError - Opening tolldata.txt file has fail.\n";
  15. cout << "\tThe program will now terminate.\n\n";
  16. cout << "\t****************************************************\n\n";
  17. exit(1);
  18. }
  19.  
  20. //then open the call marks.txt file
  21. while (!openfile.eof() && Record < ArrayRecord && !openfile.fail())
  22. {
  23. //While loop, As long as the statement not equal to end of file,
  24. //record is lesser then ArrayRecord, and opening file does not fail
  25. //Then perform the following.
  26.  
  27. openfile >> CustID[Record];
  28. //Open marks.txt file and store into
  29. //StudentID[Record] Array.
  30.  
  31. if (CustID[Record] < 0 || openfile.fail())
  32. /*If StudentID[Record] is greater then 999999 or lesser then 0
  33.   or record file fail to open, prompt user with error message
  34.   and error line.*/
  35. {
  36. cout << "\n\tError - Invalid or miss match Customer ID record.\n";
  37. cout << "\tPlease refer to line " << Record +1 <<
  38. " of tolldata.txt text file.\n\n" ;
  39.  
  40. system( "pause" ); //perform system pause and exit
  41. //record and terminate program
  42. exit(1);
  43. return 0;
  44. }
  45. openfile >> StationID[Record];
  46.  
  47. if (
  48. (
  49. StationID[Record] != 'A'
  50. &&
  51. StationID[Record] != 'a'
  52. &&
  53. StationID[Record] != 'B'
  54. &&
  55. StationID[Record] != 'b'
  56. &&
  57. StationID[Record] != 'C'
  58. &&
  59. StationID[Record] != 'c'
  60. &&
  61. StationID[Record] != 'D'
  62. &&
  63. StationID[Record] != 'd'
  64. )
  65. ||
  66. openfile.fail()
  67. )
  68. {
  69. cout << "\n\tONE\n\tError - Invalid or miss match Customer ID record.\n";
  70. cout << "\tPlease refer to line " << Record +1 <<
  71. " of tolldata.txt text file.\n\n" ;
  72.  
  73. system( "pause" ); //perform system pause and exit
  74. //record and terminate program
  75. exit(1);
  76. return 0;
  77. }
  78. Record++;
  79.  
  80. }

  1. int main()
  2. {
  3. int Record = 0;
  4.  
  5. Checkfile(Record);
  6. system("pause");
  7. return 0;
  8. }
バルサミコ酢やっぱいらへんで
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 486
Reputation: Bench has a spectacular aura about Bench has a spectacular aura about Bench has a spectacular aura about 
Solved Threads: 48
Bench's Avatar
Bench Bench is offline Offline
Posting Pro in Training

Re: Need Help in Reading characters from a text file

 
0
  #8
Apr 20th, 2006
Originally Posted by tuannie
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
  1. bool isValid(const char& c)
  2. {
  3. std::string acceptable("abcdABCD");
  4. if (acceptable.find(c) == acceptable.npos)
  5. return false; //npos is the "end of string" token
  6. else
  7. return true;
  8. }
Then, later in your code you could say something like
  1. if ( isValid(input_character) && !openfile.fail() )
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 11
Reputation: tuannie is an unknown quantity at this point 
Solved Threads: 0
tuannie tuannie is offline Offline
Newbie Poster

Re: Need Help in Reading characters from a text file

 
0
  #9
Apr 20th, 2006
Originally Posted by Bench
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
  1. bool isValid(const char& c)
  2. {
  3. std::string acceptable("abcdABCD");
  4. if (acceptable.find(c) == acceptable.npos)
  5. return false; //npos is the "end of string" token
  6. else
  7. return true;
  8. }
Then, later in your code you could say something like
  1. 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
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. const int ArrayRecord = 300;
  8. int CustID[ArrayRecord];
  9. char StationID[ArrayRecord];
  10. string Address[ArrayRecord];
  11.  
  12. int Checkfile(int & Record)
  13. {
  14. ifstream openfile ( "tolldata.txt" ); //Declare Input File stream as openfile
  15. //then open the call tolldata.txt file
  16. if (openfile.is_open())/*If text file is open excute the following*/
  17. {
  18. cout << "\t****************************************************\n\n";
  19. cout << "\t Please wait...\n\n" <<
  20. "\t The following file tolldata.txt has been found.\n\n";
  21. cout << "\t****************************************************\n\n";
  22.  
  23. }
  24. else /*If text file doesn't exist display message and terminate program*/
  25. {
  26. cout << "\t****************************************************\n\n";
  27. cout << "\tError - Opening tolldata.txt file has fail.\n";
  28. cout << "\tThe program will now terminate.\n\n";
  29. cout << "\t****************************************************\n\n";
  30. }
  31.  
  32. while (!openfile.eof() && Record < ArrayRecord && !openfile.fail())
  33. {
  34. //While loop, As long as the statement not equal to end of file,
  35. //record is lesser then ArrayRecord, and opening file does not fail
  36. //Then perform the following.
  37.  
  38. openfile >> CustID[Record];
  39. //Open marks.txt file and store into
  40. //StudentID[Record] Array.
  41.  
  42. if (CustID[Record] < 0 || openfile.fail())
  43. /*If StudentID[Record] is greater then 999999 or lesser then 0
  44.   or record file fail to open, prompt user with error message
  45.   and error line.*/
  46. {
  47. cout << "\n\tError - Invalid or miss match Customer ID record.\n";
  48. cout << "\tPlease refer to line " << Record +1 <<
  49. " of tolldata.txt text file.\n\n" ;
  50.  
  51. system( "pause" ); //perform system pause and exit
  52. //record and terminate program
  53. exit(1);
  54. return 0;
  55. }
  56. openfile >> StationID[Record];
  57.  
  58. 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())
  59. {
  60. cout << "\n\tONE\n\tError - Invalid or miss match Customer ID record.\n";
  61. cout << "\tPlease refer to line " << Record +1 <<
  62. " of tolldata.txt text file.\n\n" ;
  63.  
  64. system( "pause" ); //perform system pause and exit
  65. //record and terminate program
  66. exit(1);
  67. return 0;
  68. }
  69. Record++;
  70.  
  71. }
  72. openfile.close();
  73. }
  74.  
  75.  
  76. int main()
  77. {
  78. int Record = 0;
  79.  
  80. Checkfile(Record);
  81. system("pause");
  82. return 0;
  83. }

Could someone please tell me why it keep saying that?
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1,496
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: Need Help in Reading characters from a text file

 
0
  #10
Apr 20th, 2006
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?
バルサミコ酢やっぱいらへんで
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC