NO need to read my post...i see what my (rather silly) error was...it turns out that you can't divide by ZERO!
Hi,
Still a C++ newb here, though enjoying the langauge more. I have a question that probably has an obvious answer...but I don't see it. I'm writing a PrimeFactory class but the app keeps crashing. Here are the files.
The problematic line is if(num % i == 0) in the PrimeFactory.C . When I remove that line the app doesn't crash but i'm not sure why that line would cause an issue. I'm using very small ints here...like 5
PrimeFactory.h
1 2 3 4
class PrimeFactory{
public:
bool isPrime(int num );
};
#include <iostream>
#include <math.h>
#include <string>
#include "PrimeFactory.h"
usingnamespace std;
int main() {
int num = 5;
PrimeFactory p;
bool r = p.isPrime(num);
return 0;
}
While i'm posting questions, i have 1 more (unrelated one), can someone explain why I must put brackets arround string when I include it...as opposed to quotes when I inlcude a custom class's header file. i.e. why the different sytnax for the next two lines
Line 7 in PrimeFactory.C should be for (int i=1;i<=sqrt(num);i++)
#include <foobar.h> tells the compiler to look for foobar.h in the include directories it was provided with (e.g. "C:\MinGW\include").
#include "foobar.h" tells the compiler to look for foobar.h in the same directory as the file the directive appears in.
Suppose main.cpp is in /home/user/c++/main.cpp and it contains the following lines:
iostream will be searched in /usr/include, /usr/local/include, and any other directory that was provided for the compiler to search in. foo.h should be in /home/user/c++/, bar.h should be in /home/user/c++/foo/, and foo2.h should be in /home/user/.