Okay, I'm supposed to have their program run right on both the Unix platform and Windows platform. Now, it runs fine on both, but the output is wrong on Unix. Now, I'm guessing that maybe the ASCII values are different from OS, and that's possibly my problem. Can anyone verify this?
Here's the output that I get from Windows:
And here's the output I get from Unix:Search string: the
The string to search for is the
The string size is 3
Line Number
------------
1 1
2 0
3 1
4 0
5 1
6 0
7 1
8 0
9 1
10 0
11 0
12 0
13 1
14 0
15 2
16 0
17 0
And here's my code:Search string: the
The string to search for is the
The string size is 4
Line Number
------------
1 0
2 0
3 0
4 0
5 0
6 0
7 0
8 0
9 0
10 0
11 0
12 0
13 0
14 0
15 0
16 0
17 0
It's a very easy program, I'm just not sure about the differences from Unix to Windows, and vice versa. My problem is that Unix is saying the string is of length 4, but Windows says the string is of length 3. I used my own little for loop to determine how many characters are in the string based on their respective ASCII values. The TextEqual function relies on the size of the user inputted string. Any help?Code:#include <iostream.h> #include <fstream.h> const int LINESIZE = 45; int TextEqual (char target[], char lineInput[], int stringNum, int stringSize); void ConvertLower(char target[], int size); int main() { char userInput[LINESIZE]; char lineInput[LINESIZE]; ifstream inFile; int lineCounter = 1; // holds count of what line it's on int stringNum = 0; // holds count of string finds per line int stringSize = 0; inFile.open("EULA.txt"); // get string to search for and convert to lower case // get number of characters in string cout << "Search string: "; cin >> userInput; ConvertLower(userInput, 45); for (int counter = 0; counter < LINESIZE; counter++) if ((userInput[counter] >= 97) && (userInput[counter] <= 122)) stringSize++; cout << "The string to search for is " << userInput << endl; cout << "The string size is " << stringSize << endl; // print out heading cout << "Line\tNumber" << endl; cout << "------------" << endl; while (inFile.peek() != EOF) { // get new line of data inFile.getline(lineInput, 50, '\n'); ConvertLower(lineInput, 45); // prints line number and number of string finds stringNum = TextEqual(userInput, lineInput, stringNum, stringSize); cout << lineCounter << '\t' << stringNum << endl; // increase the line position and set the number // of string finds to 0 stringNum = 0; lineCounter++; } inFile.close(); return 0; } int TextEqual(char target[], char lineInput[], int stringNum, int stringSize) { int numCorrect = 0; for (int position = 0; position < LINESIZE; position++) { if (lineInput[position] == target[numCorrect]) numCorrect++; else numCorrect = 0; if (numCorrect == stringSize) { numCorrect = 0; stringNum++; } } return stringNum; } void ConvertLower(char target[], int size) { for (int spot = 0; spot < size; spot++) if ((target[spot] >= 65) && (target[spot] <= 90)) target[spot] = target[spot] + 32; }
Thanks,
-Steve



LinkBack URL
About LinkBacks



I'm going to give this a try when I get off of work.