Hello all,
I'm new to the forums and C++ (having previously programmed in fortran). I've been trying to write a program to print out the prime numbers up to a value N. When debugging the program I get the following warning:
primes.cc:25:8: warning: expression result unused [-Wunused-value]
for (f; f != 0; f--) {
^
(Note: I am using unix)
When running the program, I am prompted to input a value of N as expected, but when I press return, the program ends. I think the problem may lie in the break statement:
Despite reading that 'break' will end only the nested loop and not both, I see it as the only thing that might be going wrong. When I replace this with 'continue', the program prints out all integers from N to 0, so if I input N=20, the output is 20, 19, 18, ... 3, 2,
The code follows, any help/advice for a total C++ novice would be greatly appreciated.
Thanks in advance,
Tom
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
int N, f;
float modulo;
cout << "Welcome to the prime number sieve\n";
cout << "Enter the number up to which the prime numbers will be printed: ";
cin >> N;
for (int j = N; j != 1; j--) {
f = j;
for (f; f != 0; f--) {
modulo = j%f;
if (f == 1) {
cout << j << ", ";
}
if (modulo == 0) break;
}
}
return 0;
|
EDIT: I should note that this is just the relevant portion of the code, I have omitted the irrelevant parts