hey im currently reading the book C++ without fear (thx alot to eightup for this book ! ^__^)
i am right now at site 90/91
and i think there is a huge mistake in the code he tryes to teach us
first of im sorry if im wrong im still learning C++ and if im wrong id be happy if you could explain me why this code isnt working in my compiler :)
this is the EXACT code from the book !:
this code is completely wrong in my eyes o.o
if you look close
at the beginning he wants a number to be entered and check if this one is prime
the number gets saved as i
here is the first huge mistake..
later on he sets i to 2 and wants to check if n is a prime or not
n never got set to anything so has no value..
also the next thing is with his sqrt codes the compiler ALWAYS gives me an error because sqrt cannot take int... but he never transfered the int into an double o.o
i am right now at site 90/91
and i think there is a huge mistake in the code he tryes to teach us
first of im sorry if im wrong im still learning C++ and if im wrong id be happy if you could explain me why this code isnt working in my compiler :)
this is the EXACT code from the book !:
Code
#include <iostream>
#include <cmath>
using namespace std;
// Function must be declared before being used.
bool prime(int n);
int main() {
int i;
// Set up an infinite loop; break if user enters 0.
// Otherwise, evaluate n from prime-ness.
while (true) {
cout << "Enter num (0 = exit) and press ENTER: ";
cin >> i;
if (i == 0) // If user entered 0, EXIT
break;
if (prime(i)) // Call prime(i)
cout << i << " is prime" << endl;
else
cout << i << " is not prime" << endl;
}
system("PAUSE");
return 0;
}
// Prime-number function. Test divisors from
// 2 to sqrt of n. Return false if a divisor
// found; otherwise, return true.
bool prime(int n) {
int i;
for (i = 2; i <= sqrt(n); i++) {
if (n % i == 0) // If i divides n evenly,
return false; // n is not prime.
}
return true; // If no divisor found, n is prime.
}this code is completely wrong in my eyes o.o
if you look close
at the beginning he wants a number to be entered and check if this one is prime
the number gets saved as i
here is the first huge mistake..
later on he sets i to 2 and wants to check if n is a prime or not
n never got set to anything so has no value..
also the next thing is with his sqrt codes the compiler ALWAYS gives me an error because sqrt cannot take int... but he never transfered the int into an double o.o

