codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:
Search Forums:
  Help with Loop (C++/MFC)  EOTF at 20:49 on Tuesday, October 16, 2007
 

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:

#include <iostream>
#include <fstream>
#include <strstream>


....

void Csn3Dlg::OnBnClickedButton1()
{
UpdateData(TRUE);

FILE * pFile;
char buffer [20];
CString prefix = "Old";
CString readprefix;
CString temp;
CString read;
int count=0;

pFile = fopen ("/forum/dnamelist.txt" , "r");

if (pFile == NULL)
{
MessageBox("Error opening file", NULL, MB_OK | MB_ICONSTOP);
exit(0);
}

else
{
while ( ! feof (pFile) )
{
fgets (buffer , 20 , pFile);
fputs (buffer , stdout);
read = read+buffer+"\r\n";
}
fclose (pFile);

//My problems are in there:
while ( prefix != readprefix )
{
for (int j = count ; read[j]!='\n'; j++)
temp = temp+read[j];
for(int k=0 ; k!=3 ; k++)
readprefix = readprefix+temp[k];

if(prefix == readprefix)
break;
else
count++;
}

m_out = temp;

}

UpdateData(FALSE);
}



Any suggestions or help will be much appreciated!








CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums
//








Recent Forum Threads
•  Job @ EarlySail (perl)
•  Security - Code verify
•  IPC problem
•  Re: import contacts of msn/yahoo
•  Cookies and Threads C++
•  right justify a background in a table?
•  Help with Loop (C++/MFC)
•  Help with Loop (C++/MFC)
•  scancode in VB6


Recent Articles
ASP GetTempName
Decode and Encode UTF-8
ASP GetFile
ASP FolderExists
ASP FileExists
ASP OpenTextFile
ASP FilesystemObject
ASP CreateFolder
ASP CreateTextFile
Javascript Get Selected Text


© Copyright codetoad.com 2001-2007