Segmentation Fault

I'm currently trying to create a simple program that does basic arithmetic functions on fractions stored in a class called Rational. The +,-,*,/,==,<,>,>=,<= and = operators are already overloaded. The only issue i have is when running this program with a test file such as:

1/2 3/4 +
1/3 1/4 * 1 +
1/5 4/5

The program segfaults after computing and outputting the first line. Any idea why this might happen? Here's the Code:

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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include "Rational.hpp"
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>

using namespace std;

int main(int argc, char *argv[])
{
	int count = 0;
	while(argv[count] != 0)
	{
		count++;

		ifstream infile(argv[count]);
		char line[256];

		while(infile)
		{
			infile.getline(line,256);
			cout << "YOPP";
	
			char *token = strtok(line," ");

			while(token != NULL) {
				string token2(token);
				int found = token2.find('/');
	
				long int x, y;
				stringstream(token2.substr(0,found)) >> x;
	
				if(found !=-1) {
					found++;
					stringstream(token2.substr(found)) >> y;
				}
				else
					y=1;
	
				Rational rat1 (x,y);
				
				cout << rat1 << " ";	
	
				bool done = false;
				
				token = strtok(NULL," ");
	
				while(token != NULL || done) {
					token2 = string (token);
					found = token2.find('/');
	
					stringstream(token2.substr(0,found)) >> x;
	
					if(found !=-1) {
						found++;
						stringstream(token2.substr(found)) >> y;
					}
					else
						y=1;
	
					Rational rat2 (x,y);
	
					cout << rat2 << " ";
	
					bool equality;
	
					token = strtok(NULL," ");
					
					cout << token << " ";
	
					if(strcmp(token,"+") == 0) {
						rat1 = rat1 + rat2;
					}
					else if(strcmp(token,"-") == 0) {
						rat1 = rat1 - rat2;
					}
					else if(strcmp(token,"*") == 0) {
						rat1 = rat1 * rat2;
					}
					else if(strcmp(token,"/") == 0) {
						rat1 = rat1 / rat2;
					}
					else if(strcmp(token,"<") == 0) {
						equality = rat1 < rat2;
						if(equality) {
							cout << ": true\n";
						}
						else {
							cout << ": false\n";
						}
						done = true;
					}
					else if(strcmp(token,">") == 0) {
						equality = rat1 > rat2;
						if(equality) {
							cout << ": true\n";
						}
						else {
							cout << ": false\n";
						}
						done = true;
					}
					else if(strcmp(token,"<=") == 0) {
						equality = rat1 <= rat2;
						if(equality) {
							cout << ": true\n";
						}
						else {
							cout << ": false\n";
						}
						done = true;
					}
					else if(strcmp(token,">=") == 0) {
						equality = rat1 >= rat2;
						if(equality) {
							cout << ": true\n";
						}
						else {
							cout << ": false\n";
						}
						done = true;
					}
					else if(strcmp(token,"==") == 0) {
						equality = rat1 == rat2;
						if(equality) {
							cout << ": true\n";
						}
						else {
							cout << ": false\n";
						}
						done = true;
					}
					else
						cout << "Error reading line\n";
	
					token = strtok(NULL," ");
				}
				if (!done) {
					cout << ": " << rat1 << " (double " << rat1.toDouble() << ")\n";
				}
				
				cout << "YOPP2 \n";
				
			}
		}
		//infile.close();
	}
	return 0;
}


Thanks
line 12 is supposed look like this:
for(int count = 1; count < argc; ++count)
don't rely on a null pointer

line 17 is supposed look like this:
char line[256 +1]; // +1 due to the additional '\0'!

Don't use strtok(). Use stringstream instead:
1
2
3
4
5
6
7
8
9
10
		while(infile)
		{
			string line;
			getline(infile, line);
			cout << "YOPP";
	
			stringstream is(line);

			while(is >> token2) {
				int found = token2.find('/');
You might call strtok() too often
Last edited on
Topic archived. No new replies allowed.