I am working on a fairly large project for my chemistry teacher (NOT homework!). I thought that it would be nice if the program could search a file for a user entered string and display all lines that had that string. I have gotten it as far as I can and it still doesn't work. I am fairly certain that I know where the problem is but even if I was positive I wouldn't know how to fix it. Here's the code that deals with the user entered string (which doesn't have any problems itself):

void results();
void display_results(int posx,int posy);

void display_results(int posx,int posy)
{
    ofstream fout;
    ifstream fin;
    
    char ch;
    
    //Set up result[] variable
    int result_size=1;
    int result_int=0;
    
    fin.open("Temp");
    while (fin.get(ch))
        result_size++;
    fin.close();
    
    char result[result_size];
    
    while (fin.get(ch))
    {
        result[result_int]=(char) ch;
        result_int++;
    }
    
    rectfill(screen,0,0,SCREEN_W,SCREEN_H,GREY);
    textprintf_ex(screen,font,posx,posy,BLACK,CLEAR,"%s",result);
    
}

void results()
{
     ofstream fout;
     ifstream fin;
     
     char ch;
     int i=0;
     int posx=5;
     int posy=5;
     
     //Set up the search[] variable
     int search_size=1;
     int search_int=0;
     
     fin.open("Temp1");
     while (fin.get(ch))
     {
         search_size++;
     }
     fin.close();
     
     char search[search_size];
     
     fin.open("Temp1");
     while (fin.get(ch))
     {
         search[i]=(char) ch;
         i++;
     }
     
     //Set up the file[] variable
     int file_size=1;
     int file_int=0;
     
     fin.open("Element File.txt");
     while (fin.get(ch))
     {
         file_size++;
     }
     fin.close();
     
     char file[file_size];
     i=0;
     
     fin.open("Element File");       //I'm fairly certain that
     while (fin.get(ch))             //the problem occurs
     {                               //here while reading
         file[i]=(char) ch;          //from the file into
         i++;                        //the array.
     }
     
     //The curline[] variable is too unpredictable
     //to formally set up so we won't
     //but we should clear it
     char curline[30];
     int curline_int=0;
     
     for (int i=0; i<30; i++)
         curline[i]='\0';
     
     while (file_int!=file_size)
     {
         if (file[file_int]!='\n')
         {
             curline[curline_int]=file[file_int];
             fout.open("Temp",ios::app);
             fout << curline[curline_int];
             rest(1000);
             fout.close();
             curline_int++;
         }
         else
         {
             curline_int=0;
             while(curline_int<30)
             {
                 if (curline[curline_int]==search[search_int])
                     search_int++;
                 if (search_int==search_size)
                     display_results(posx,posy);
                 curline_int++;
             }
             search_int=0;
             curline_int=0;
         }
                    
         file_int++;
     }
}

I'm pretty sure that the problem when it tries to load the Element file into the array, but does any one have any other suggestions or notice any problems right off? As a side note search[] which is the array that holds the user entered search is written to file "Temp1" the write works but I'm not positive about the read. I didn't use parameters because for some reason it always passed a pointer, which the program views as a number instead of a character string.

Whew, that took a while.

Thanks in advance.

Recommended Answers

All 5 Replies

Urgh, I skimmed your code and noticed the abscence of string arrays, have you considered using them? They make comparison so much easier. The are declared in this format:

std:: string array[4];

dont forget to include <string>
If you gave me the code snippet of where you think it is wrong, I might be able to look at it more thoroughly, also, have you tried inserting couts and system("PAUSE");s throughout your code to see where it stops?

I wasn't using strings because they came up with an error when I tried to use them, but I noticed from your post that they are an std object. That might help but it would also at this point require a major overhaul of my project. I'm not using cout and system("pause") because it isn't a console project.

I put some comment lines by the section that I thought was the problem, though that was a lot to look through, so I'm not surprised you missed them.

fin.open("Element File");
while (fin.get(ch))
{
    file[i]=(char) ch;
    i++;
}

I also remembered another reason that I can't switch to strings. The Allegro library that I'm using doesn't have a standard typing routine, so I told it to put the pressed key into a character array.

Anybody?

Never mind, I figured it out on my own.

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.