943,936 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2140
  • C++ RSS
Oct 16th, 2007
0

Help with Loop (C++/MFC)

Expand Post »
Hi folks,

I'm having problems with my loops, and possibly more...hehe!

I'm loading a text file, which has 1 word per line, into a buffer and I want to search the buffer for all the words that start with the same prefix (3 letters) and return the words in an edit box.

I successfully accomplished reading 1 line, compare the prefix, and show the word in an edit box if prefix matches. Now, I'm just stuck with making it loop.

Exemple:

The Prefix is: "Bla".
The text file contains:
Benji
Bennett
Bentley
Beverly
Billy
Birch Grove
Birch Hill
Birch
Birchdale
Birchmount
Birchwood
Black Beach
Black
Blair
Bleury
Blue Heron
Blue Rock

The program will return:
Black Beach
Black
Blair


Here's the code for the button:


C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <fstream>
  3. #include <strstream>
  4.  
  5. //....
  6.  
  7. void Csn3Dlg::OnBnClickedButton1()
  8. {
  9. UpdateData(TRUE);
  10.  
  11. FILE * pFile;
  12. char buffer [20];
  13. CString prefix = "Old";
  14. CString readprefix;
  15. CString temp;
  16. CString read;
  17. int count=0;
  18.  
  19. pFile = fopen ("d:\\namelist.txt" , "r");
  20.  
  21. if (pFile == NULL)
  22. {
  23. MessageBox("Error opening file", NULL, MB_OK | MB_ICONSTOP);
  24. exit(0);
  25. }
  26.  
  27. else
  28. {
  29. while ( ! feof (pFile) )
  30. {
  31. fgets (buffer , 20 , pFile);
  32. fputs (buffer , stdout);
  33. read = read+buffer+"\r\n";
  34. }
  35. fclose (pFile);
  36.  
  37. //My problems are in there:
  38. while ( prefix != readprefix )
  39. {
  40. for (int j = count ; read[j]!='\n'; j++)
  41. temp = temp+read[j];
  42. for(int i=0 ; i!=3 ; i++)
  43. readprefix = readprefix+temp[i];
  44.  
  45. if(prefix == readprefix)
  46. break;
  47. else
  48. count++;
  49. }
  50.  
  51. m_out = temp;
  52.  
  53. }
  54.  
  55. UpdateData(FALSE);
  56. }

Any suggestions or help will be much appreciated!
Last edited by Ancient Dragon; Oct 16th, 2007 at 8:37 pm. Reason: replace quote tags with code tags
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
EOTF is offline Offline
1 posts
since Oct 2007
Oct 16th, 2007
0

Re: Help with Loop (C++/MFC)

line 29 is wrong
C++ Syntax (Toggle Plain Text)
  1. while( fgets (buffer , 20 , pFile) )
  2. {
  3. // blabla
  4. }

fgets() normally adds the '\n' that is in the file at the end of the input string that you need to strip off. And it would make your life a lot easier if you would put the strings in an array instead of concantinating them into one big string.
C++ Syntax (Toggle Plain Text)
  1. while( fgets (buffer , sizeof(buffer) , pFile) )
  2. {
  3. if( buffer[strlen(buffer)-1] == '\n')
  4. buffer[strlen(buffer)-1] = 0;
  5. // blabla
  6. }
Last edited by Ancient Dragon; Oct 16th, 2007 at 8:46 pm.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Adding things to Array-Based Lists
Next Thread in C++ Forum Timeline: how to retrieve part of the string





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC