Floating point exeption when running from Terminal but not Xcode

I have this pointless program that accepts an integer as a command line argument and uses a text file that lists a bunch of prime numbers to find the prime factorization of that number. I just made it to test out some concepts and it works when I run it from Xcode with the parameters set in the project, but when I run it from the terminal and enter the argument, I get a Floating Point exception. What would cause a program to run fine in the project but have that error in Terminal?

Edit: it may be that the file doesn't open primes.txt in terminal. Does anyone know how to get a separate file to open in a program while running from terminal?

Here's the code just in case:
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
150
151
152
153
154
155
156
157
#include <iostream>
#include <fstream>
#include <cstdlib>

using namespace std;

class Factorization
{
public:
	Factorization();
	Factorization(int num);
	~Factorization();
	Factorization(const Factorization& f);
	
	Factorization& operator=(const Factorization& f);
	Factorization& operator=(int num);
	
	void printFactors() const;
	
private:
	int number;
	int* factors;
	int* times;
	int size;
};

Factorization::Factorization()
{
	int num;
	cout << "Enter a number: ";
	cin >> num;
	number = num;
	
	ifstream primes;
	primes.open("primes.txt");
	
	int factor;
	primes >> factor;
	
	factors = new int[number/2];
	times = new int[number/2];
	size = 0;
	while (factor <= number/2 && num != 1)
	{
		while (num%factor == 0)
		{
			if (times[size] == 0)
				factors[size] = factor;
			times[size]++;
			num /= factor;
			if (num%factor != 0)
				size++;
		}
		primes >> factor;
	}
	primes.close();
	if (number == num)
	{
		size = 1;
		factors[0] = number;
		times[0] = 1;
	}
}

Factorization::Factorization(int num)
{	
	number = num;
	
	ifstream primes;
	primes.open("primes.txt");
	
	int factor;
	primes >> factor;
	
	factors = new int[num/2];
	times = new int[num/2];
	size = 0;
	while (factor <= number/2 && num != 1)
	{
		while (num%factor == 0)
		{
			if (times[size] == 0)
				factors[size] = factor;
			times[size]++;
			num /= factor;
			if (num%factor != 0)
				size++;
		}
		primes >> factor;
	}
	primes.close();
	if (number == num)
	{
		size = 1;
		factors[0] = number;
		times[0] = 1;
	}
}

Factorization::Factorization(const Factorization& f)
{
	number = f.number;
	size = f.size;
	
	factors = new int[number/2];
	times = new int[number/2];
	copy(f.factors, f.factors + number/2, factors);
	copy(f.times, f.times + number/2, times);
	 
}

Factorization::~Factorization()
{
	delete[] factors;
	delete[] times;
}

Factorization& Factorization::operator=(const Factorization& f)
{
	number = f.number;
	size = f.size;
	copy(f.factors, f.factors + number/2, factors);
	copy(f.times, f.times + number/2, times);
	return *this;
}

Factorization& Factorization::operator=(int num)
{
	*this = Factorization(num);
	return *this;
}

void Factorization::printFactors() const
{
	cout << number << " = " << factors[0] << "^" << times[0];
	for (int i = 1; i < size; i++)
	{
		cout << " * " << factors[i] << "^" << times[i];
	}
	cout << "\n";
}	

int main (int argc, char* argv[])
{
	
	Factorization h(atoi(argv[1]));
	
	
	h.printFactors();
	
	Factorization g = h;//Testing out assignment operator
	
	g.printFactors();
	
	
	return 0;
}
Last edited on
You should check if the file open succeeds in your constructors and throw an exception if it fails.
Yeah, I realized that the problem was that the file doesn't open. Does anyone know how to open a file when you run a program from Terminal? What directory do you have to put it in? Or do you have to enter the directory as a command line argument somehow?
The way the program is written now, it reads the file from the current working directory. You can change the program to specify the full pathname to the file or you can launch the program from the directory that the file is in.
Topic archived. No new replies allowed.