Are you aware that your program (even after your changes) still doesn't work correctly? It runs, and it gives output, but that output is wrong. It is reporting occurrences of the target string on lines where it does not exist.
First, as codeplug pointed out, you have an inconsistency between the defined length of your characterString variable and the length of the input lines you are reading in. This is definitely a problem. And, as a matter of good form, you should avoid having these "magic numbers" floating around in your code -- makes it hard to debug and hard to modify later on should that become necessary. Instead, define a constant and use that constant in the body of the program to define array sizes, limits for "while" statements, etc. As an example, I defined a constant MAXSTRING in the code posted below.
Second, while debugging, you should learn to use extra print statements to help figure out where the program is going wrong.
Below is a modified version of your code which STILL doesn't work correctly, but if you study its screen output you should be able to figure out what's going wrong.
Maybe you have been testing with a file that makes it appear that the program works. Just in case, I'm also attaching the file I have been using to test your program. Use it, and search for the word "release" & you will see that your code reports occurrences EVEN ON BLANK LINES.
Anyway, check the output of this version. There is still one error remaining that can cause the output to be completely inaccurate(as it does with my input file). Using the step by step output you should be able to find it.
Code:
#include <iostream.h>
#include <fstream.h>
#include <cstring>
#include <stdio.h> //for getchar() (to pause while debugging)
#define MAXSTRING 50 // constant to define maximum input string length throughout the program
typedef char characterString[MAXSTRING+1]; //allowing space for null byte
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");
cout << "Enter the word to search for: " << endl;
cin >> target;
convertText(target);
numberOfCharacters = strlen(target);
cout << "Line Number \t Number of Matches" << endl;
while(infile.peek() != EOF)
{
cout << "entering while loop; numberOfMatches = " << numberOfMatches << endl;
infile.getline(input, MAXSTRING+1, '\n');
cout << input << endl; //debug
convertText(input);
cout << input << endl; //debug
// in the next line I reduced the upper limit of the search - no point checking the
// last few letters if there are fewer than numberOfCharacters remaining in the string
for(int k = 0; k < MAXSTRING-numberOfCharacters+1; k++) //traverses the input string
{
cout << "now testing input[" << k << "]: contents = " << input[k] << endl; // debug
if(target[0] == input[k])
{ cout << "entered first \"if\", numberOfMatches =" << numberOfMatches << endl;//debug
if(compairText(input, k, target, 0, numberOfCharacters))
{
cout << "after successful compairText, numberOfMatches ="
<< numberOfMatches << endl; //debug
numberOfMatches++;
cout << "incremented numberOfMatches to " << numberOfMatches << endl;//debug
k = k + numberOfCharacters - 1;
}
}
}
cout << "finished traversing the string" << endl;
cout << lineNumber << "\t\t\t" << numberOfMatches << endl;
outfile << lineNumber << "\t\t\t" << numberOfMatches << endl;
lineNumber++;
numberOfMatches = 0;
cout << "numberOfMatches reset to " << numberOfMatches << endl; //debug
cout << "press <enter> to continue" << endl; //debug
getchar(); //debug
}
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;
else if(input[x] == target[y])
if(compairText(input, (x+1), target, (y+1), (z-1)))
return 1;
}
return 0;
}
// ****************************************************
void convertText(characterString x)
{
for(int i = 0; i < MAXSTRING; i++)
if(x[i] >= 65 && x[i] <= 90)
x[i] += 32;
}
Here is the test input file I used (lines truncated to 50 chars):
1-2-3 Release 5 for Windows Product Updates (READM
**CONTENTS**
Updating Release 4 Worksheet Files to Release 5
Install
Information for Upgraders
Installing on Computers with Multiple Configuratio
Server and Distribution Install
3. Windows and Available Memory
4. Charting Information for Upgraders
5. Printing Performance Information
Printer Drivers and Devices
Print Resolution
6. DataLens and ODBC Drivers
ODBC Drivers
Environment Variables
7. Version Manager
8. Solver
9. Command-Line Parameters
10. Corrections to the Documentation
Macros Help
Help for 1-2-3 Messages
The Guided Tour
11. Font Usage
Font Mapping
Font Performance Tips
Removing ATM
12. Map Performance Information
Finding the Correct Map Type
Map Resolution
Map Macros
Maps and Lotus Approach
13. Compatibility
Working with .WK1 Files
Working with Excel Files
Working with dBASE Files
14. Using 1-2-3 with OS/2
**1. UPDATING RELEASE 4 WORKSHEET FILES TO RELEASE