fstream problem

if i comment out
1
2
3
    if (strlen(x)!=strlen(Q)){
        //compare length of the two char and return false if !=
        return false;


it will run smoothly, but un-commenting it will just cause the program to end right away.
can anyone see what's wrong with it ? thankX~!


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*creat a text file and output to the file, while quite on keyword "Quite"*/

#include <iostream>
#include <cstdlib>
#include <fstream>
#include <cstring>
#define COL_WIDTH 80

using namespace std;

bool filecmp(char *x, char *Q);

int main (){
    // user input keyword to quite
    char Qt[6] = "Quite";
	char filename[MAX_PATH + 1];
	char input_line[COL_WIDTH + 1];

	cout << "Enter a file name : " ;
	cin.getline(filename , MAX_PATH);
	ofstream file(filename);

	if (file == NULL){
		cout << "The file cannot be created "<< endl;
		return -1;
		system("PAUSE");
	}
	cout << filename << " is opened/created .\n Enter your sentence below or enter \"Quite\" to quite " << endl;
	while (true ){
		cin.getline(input_line , COL_WIDTH );
		if (filecmp(input_line,Qt) == true )
            break;
		file << input_line;
	}
}


bool filecmp(char *x, char *Q){
    cout << "\t<FILE_CMP reached>\n";
    //problem is here :
    if (strlen(x)!=strlen(Q)){
        //compare length of the two char and return false if !=
        return false;
    }
    //----------------------
    for (int i = 0 ; i < 6 ; i++){
        cout << "\t< FILE_CMP > <" <<i << "> :\n";
        if (x[i]!=Q[i]){
            // the below three lines are tests, return false if the String doesn't match.
            cout << "\tFILE_CMP returned <false>.\n";
            cout << "\tx[i] is : "<<x[i]<<endl;
            cout << "\tQ[i] is : "<<Q[i]<<endl;
            return false ;
        }
    }
    cout << "\tFILE_CMP returned <true>.\n";
    return true;
}
What are the values of input_line? Maybe you should print them to debug the program.

What might seem like "right away" to you can be a very long time for a program running on a modern home computer.
Topic archived. No new replies allowed.