First of all, hello, I'm new here.
I have been trying to hack together a number tester, which will test a number to see if it's a prime or not. It should write the result to a file, but it doesn't. I've also been trying to figure out the source of the problem, but I just don't get it.
Here it is, to it's fullest:
Code:// The program should test a number, defined by user-input, if it's a prime number or not. // This through dividing each number below the user-inputted, starting from 2 // to, let the user-input be k, k-1. If all cases return a remainder, the number first inputted // number is a prime. // If none of the cases have a remainder the number is not a prime. #include <iostream> #include <cmath> #include <fstream> using namespace std; int main () { int true_false, quit; float i, k, result; result = fmod(k, i); i = 2; cin >> k; ofstream primefile; primefile.open ("Primeer.txt", ios::ate); if (!primefile.is_open()) { cout << " The file could not be opened. "; } else { while (i = k - 1) { result = fmod(k,i); if (result = 0) { true_false = true_false + 1; break; cout << " The following number isn't a prime: " << k; primefile << " The following number isn't a prime: " << k; primefile.close(); cout << " Succesfull writing to file: " << primefile; } if (result > 0) { true_false = true_false + 0; i++; } } } if (i = k - 1, true_false = 0) { cout << " The following number is a prime: " << k; primefile << " The following number is a prime: " << k; cout << " Succesfull writing to file: " << primefile; } cin >> quit; if (quit == true) { return 0; } }
Hope I didn't screw the formatting up.
Any help would be appreciated.



LinkBack URL
About LinkBacks



