Hey guys heard of your site through a friend and i decided to give you guys a shot at this problem. I have written a simple code for searching a text file and outputting the number of occurences a certain word appears and the line number to the screen. It works fine in Visual C++ 6.0 but when i load it into Unix it links compiles and everything but outputs the wrong number of occurences. I get no errors or warnings in either one(C++ or Unix). Here is the code:
I hope you guys can find out what's wrong with it, and if you need to know anything like why something is delcared or what just ask and I will respond with whatever you need. Thanks a million guys.Code://********************************************************************************************* // // This program finds a word or phrase input by the user in a file and outputs the lines of // the file and the number of occurances. // //********************************************************************************************* #include <iostream.h> #include <fstream.h> typedef char characterString[45]; char SENTINAL = '*'; int compairText(characterString input, int x, characterString target, int y, int numberOfCharacters); void convertText(characterString input); int main() { characterString input; //input string characterString target; //target string int numberOfCharacters = -1; //number of characters in the target string int lineNumber = 1; //number of lines int numberOfMatches = 0; //keeps the number of matches found ifstream infile; //input file ofstream outfile; //output file infile.open("hw02.dat"); outfile.open("Output.dat"); for(int i = 0; i < 45; i++) target[i] = SENTINAL; //initilizes the target string to * cout << "Enter the word to search for: " << endl; cin >> target; convertText(target); for(int j = 0; j < 45; j++) //finds the number of characters in the target string { if(target[j] != SENTINAL) numberOfCharacters++; } cout << "Line Number \t Number of Matches" << endl; while(infile.peek() != EOF) { infile.getline(input, 50, '\n'); convertText(input); for(int k = 0; k < 45; k++) //traverses the input string { if(target[0] == input[k]) if(compairText(input, k, target, 0, numberOfCharacters)) { numberOfMatches++; k += numberOfCharacters; } } cout << lineNumber << "\t\t\t" << numberOfMatches << endl; outfile << lineNumber << "\t\t\t" << numberOfMatches << endl; lineNumber++; numberOfMatches = 0; } infile.close(); outfile.close(); return 0; } //********************************************************************************************* int compairText(characterString input, int x, characterString target, int y, int z) { if(z == 0) return 1; else { if(input[x] != target[y]) return 0; if(input[x] == target[y]) if(compairText(input, (x+1), target, (y+1), (z-1))) return 1; } } //********************************************************************************************* void convertText(characterString x) { for(int i = 0; i < 45; i++) if(x[i] >= 65 && x[i] <= 90) x[i] += 32; }
-Bill
P.S. sorry about the tags haven't learned how to use them yet, yes i know i know i'm a n00b



LinkBack URL
About LinkBacks



But thanks for the help.