Need help with a problem dealing with files (retriving and checking information)

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jan 2009
Posts: 22
Reputation: jdm is an unknown quantity at this point 
Solved Threads: 0
jdm jdm is offline Offline
Newbie Poster

Need help with a problem dealing with files (retriving and checking information)

 
0
  #1
Nov 8th, 2009
I'm helping a friend out with some code and I'm having the hardest time figuring out how to do this. I know it is simple, but for the life of me I can't remember how to do it. Any help or advice would be welcomed. Thanks for the help in advance.

Problem: I need to open a file first, then have a user input a 6 digit number then check the file and if it finds it then it will get the name by it and if it doesn't exist in the file then it will say so. I'm having trouble having it run through and check the file. I can have it run through the whole document and output the data and I can check the very first line of data, but after that I can't get it to work at all. It will get the first three data values from the file if it isn't the first number of the file, but it won't get the name or check. I haven't got to the part about output if the number doesn't exit in the file, but I'm not concerned about that part yet. Here is my code. Thanks again for the help.

  
  1.  
  2. #include <iostream>
  3. #include <iomanip>
  4. #include <fstream>
  5. #include <string>
  6.  
  7. using namespace std; //helps run the program
  8. int main()
  9. {
  10. double sticker;
  11. string Fname;
  12. double first;
  13. string second;
  14. second = "not";
  15.  
  16. ifstream dataIn; //for the data input
  17. dataIn.open("HWK5.txt");
  18.  
  19. cout << "sticker: ";
  20. cin >> sticker;
  21.  
  22. while(dataIn)
  23. {
  24. dataIn >> first;
  25. cout << first << endl;;
  26. if (first == sticker)
  27. {
  28. dataIn >> second;
  29. cout << second << endl;
  30. Fname = second;
  31. }
  32. else if (first != sticker){
  33. dataIn >> first;
  34. cout << first << endl; }
  35.  
  36. }
  37.  
  38.  
  39. cout << "here: " << Fname << endl;
  40. dataIn.close();
  41. system("Pause");
  42. return 0;
  43. }
  44.  
  45.  
  46. /*
  47.  
  48.   while(dataIn)
  49.   {
  50.   dataIn >> first;
  51.   cout << first << endl;
  52.  
  53.   if (first != sticker){
  54.   dataIn >> first;
  55.   cout << first << endl; }
  56.   else if (first == sticker)
  57.   {
  58.   dataIn >> second;
  59.   cout << second << endl;
  60.   Fname = second;
  61.   }
  62.   }
  63.   */

Sincerely yours;

jdm
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 6
Reputation: ebrutekim is an unknown quantity at this point 
Solved Threads: 1
ebrutekim's Avatar
ebrutekim ebrutekim is offline Offline
Newbie Poster
 
0
  #2
Nov 11th, 2009
For checking every line of the data, assuming each number has its own line, try this:

  1. string sticker;
  2. string line;
  3.  
  4. dataIn.open("HWK5.txt");
  5.  
  6. if (dataIn.is_open())
  7. {
  8. while (! dataIn.eof() )
  9. {
  10. getline (dataIn,line);
  11. stickCheck = (atoi(line.c_str()));
  12.  
  13. if(stickCheck == sticker)
  14. {
  15. //once numbers match perform your function
  16. }
  17. }
  18. dataIn.close();
  19. }

Maybe I misread your question, but I gathered that you couldn't get your inputted number to check against all the values in your file.

For checking whether the number is found, you might include something like:

  1. string sticker;
  2. string line;
  3.  
  4. bool numFound = false;
  5.  
  6. dataIn.open("HWK5.txt");
  7.  
  8. if (dataIn.is_open())
  9. {
  10. while (! dataIn.eof() )
  11. {
  12. getline (dataIn,line);
  13. stickCheck = (atoi(line.c_str()));
  14.  
  15. if(stickCheck == sticker)
  16. {
  17. //once numbers match perform your function
  18. numFound = true;
  19. }
  20. }
  21. if(numFound == false)
  22. {
  23. cout << "Number not found!" << endl;
  24. }
  25. dataIn.close();
  26. }
  27. else
  28. {
  29. cout << "Unable to open file!";
  30. }

Hope this helps.
"Don't be afraid to give your best to what seemingly are small jobs. Every time you conquer one it makes you that much stronger. If you do the little jobs well, the big ones will tend to take care of themselves." - Dale Carnegie

\m/
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 22
Reputation: jdm is an unknown quantity at this point 
Solved Threads: 0
jdm jdm is offline Offline
Newbie Poster
 
0
  #3
Nov 11th, 2009
Thank you for your help. I will give that a try and let you know how it work.

Sincerely yours;

jdm


11
Originally Posted by ebrutekim View Post
For checking every line of the data, assuming each number has its own line, try this:

  1. string sticker;
  2. string line;
  3.  
  4. dataIn.open("HWK5.txt");
  5.  
  6. if (dataIn.is_open())
  7. {
  8. while (! dataIn.eof() )
  9. {
  10. getline (dataIn,line);
  11. stickCheck = (atoi(line.c_str()));
  12.  
  13. if(stickCheck == sticker)
  14. {
  15. //once numbers match perform your function
  16. }
  17. }
  18. dataIn.close();
  19. }

Maybe I misread your question, but I gathered that you couldn't get your inputted number to check against all the values in your file.

For checking whether the number is found, you might include something like:

  1. string sticker;
  2. string line;
  3.  
  4. bool numFound = false;
  5.  
  6. dataIn.open("HWK5.txt");
  7.  
  8. if (dataIn.is_open())
  9. {
  10. while (! dataIn.eof() )
  11. {
  12. getline (dataIn,line);
  13. stickCheck = (atoi(line.c_str()));
  14.  
  15. if(stickCheck == sticker)
  16. {
  17. //once numbers match perform your function
  18. numFound = true;
  19. }
  20. }
  21. if(numFound == false)
  22. {
  23. cout << "Number not found!" << endl;
  24. }
  25. dataIn.close();
  26. }
  27. else
  28. {
  29. cout << "Unable to open file!";
  30. }

Hope this helps.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 22
Reputation: jdm is an unknown quantity at this point 
Solved Threads: 0
jdm jdm is offline Offline
Newbie Poster

Getting code to work

 
0
  #4
30 Days Ago
I couldn't get it to work. Also the file looks something like this:

111111 Hill
222222 Sam

The numbers are on the same line as the name. Thanks for the help.

Sincerely yours;

jdm
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 618
Reputation: jonsca is on a distinguished road 
Solved Threads: 82
jonsca jonsca is offline Offline
Practically a Master Poster
 
0
  #5
30 Days Ago
The atoi() function should strip out the text as long as the numbers are first. Either that or you could change his getline to one with a delimiter of ' ' (space) http://www.cplusplus.com/reference/i...tream/getline/
Reply With Quote Quick reply to this message  
Reply

Message:




Views: 253 | Replies: 4
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC