Little Problem with Logic using Strcmp()

Hi everyone,

I am trying to use 'if' statements involving strcmp() and tests inside a while loop involving strcmp(), but my program blows up in the middle of its execution. I beleive the logic is fine but i still can't figure it out why it crashes. The program IS able to output to the screen and to the output file only the fist line of text from the input file before it crashes (i don't know if that is a hint to the problem or not)

#include <fstream>
#include<iostream>
using namespace std;
#include <cstring>
#include <iomanip>

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
59
60
61
62
63
64
65
66
67
int main()
{
	
     char str[101]="" ;
	
     char table[100][16] = {'\0'}; 
	
      ifstream read;
       read.open("newinput.txt");
	
	ofstream out;
	out.open("output.txt");
	
	if (out.fail())
	{
		cout<<"output file did not open check it\n";
		system("pause");
		return -1;
	}
	
	if (read.fail())
	{
		cout<<"input file did not open check it\n";
		system("pause");
		return -1;

	}
	
	
	while (!read.eof())
	{
		
		read.getline(str,100,'\n');
		
		out<<str<<endl;  
		char *pch;
		pch = strtok (str, " ,.");
		while (pch !=NULL)				
		{
			cout << "token: "<<pch<<endl; 
			pch = strtok (NULL, " ,.");
			
			int j = 1, k = 1;      
			int i = 0;         
	
			while (    i !=5  )      ///WHEN I DO A strcmp() TEST IN this WHILE LOOP ****
			{	 	                 //PROGRAM ALSO STOPS RUNNING, AND I WORKOUT THE LOGIC ***
                                                          //AND I BELEIVE THERE IS NO PROBLEM THERE OR DOWN **
                                                           // IN THE OTHER PART DOWN HERE
 
				 strcmp(table[i], pch);
				if (strcmp (table[i], pch) !=0); //****HERE IS THE ACTUAL ERROR IN THIS PROGRAM but the logic
				{                                                                      //seems perfeclty fine to me
				                                                            //*You can comment this if statement out and it will run fine.
												
					//Do something
				}
			 
				i++;
			}
		}
		
	}
	
return 0;
}


Again, the logic looks good to me. The problem lies in the "if" statement inside the while (i!=5) loop (line 52), if you comment it out it runs fine.

Please Help me, thanks.
Last edited on
If you're using C++, you really should be using standard C++ strings rather than C strings.

Anyway, that's not the problem. The problem is that strtok is returning null to pch and you're then using pch in strcmp.
I really need to use c strings for this assignment.

Please, can you tell me how to fix this problem?
Oh, i can omit the if statement in line 52, and there still will be pch using strcmp() in line 51 and that will cause no problem, only in the if test
You need to check pch for null after the call to strtok. Clearly, if it's nll, you've done parsing the line. In which case you're done and should exit that loop and go on to process the next line.
thanks for the help
Topic archived. No new replies allowed.