Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C 

code mistake in book: C++ Without Fear ?

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 !:

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
Learn about scoping

http://msdn.microsoft.com/en-us/library/...s.80).aspx
k i did understand that part with the i and the N now

but still this code gives me an error that sqrt is overloaded
try with
PHP Code

<?php 
sqrt
((float)n) 

There are three version of sqrt, and none of them uses an int, so the compiler has to convert your int, but does not know in which type it has to convert. With an implicit cast you show the compiler you want to use the second one with a float.
PHP Code

<?php 
double sqrt 
(  double x );
  
float sqrt (  float x );
long double sqrt ( long double x ); 
using namespace std; == bad
int i; == bad because 'i' is not only ambiguous, but is generally reserved for an 'i'ndex variable. Creating good variable names is essential to programming well
system("pause"); == bad
if(i % 2 == ) ....
...
should just be
return i % 2;
and many other things.
So, the code is shit but it doesn't mean it's wrong. If it compiles and works as expected then what's the problem?
(Mar 14, 2012 06:10 AM)vive facile Wrote:  using namespace std; == bad
int i; == bad because 'i' is not only ambiguous, but is generally reserved for an 'i'ndex variable. Creating good variable names is essential to programming well
system("pause"); == bad
if(i % 2 == ) ....
...
should just be
return i % 2;
and many other things.
So, the code is shit but it doesn't mean it's wrong. If it compiles and works as expected then what's the problem?

this code is the exact code like it is in a book :)
and no it does not work that was exactly the problem ! :P

(Mar 14, 2012 01:37 AM)Dbof Wrote:  try with
PHP Code

<?php 
sqrt
((float)n) 

There are three version of sqrt, and none of them uses an int, so the compiler has to convert your int, but does not know in which type it has to convert. With an implicit cast you show the compiler you want to use the second one with a float.
PHP Code

<?php 
double sqrt 
(  double x );
  
float sqrt (  float x );
long double sqrt ( long double x ); 

thx with adding float it works just perfect ! :)
(Mar 14, 2012 06:10 AM)vive facile Wrote:  using namespace std; == bad
int i; == bad because 'i' is not only ambiguous, but is generally reserved for an 'i'ndex variable. Creating good variable names is essential to programming well
system("pause"); == bad
if(i % 2 == ) ....
...
should just be
return i % 2;
and many other things.
So, the code is shit but it doesn't mean it's wrong. If it compiles and works as expected then what's the problem?

It frustrates me that these so called "experts" who write these beginner books teach such bad habits to beginners.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Python] by MrRatThatAug 26, 2018 11:51 AM 5 186 Aug 26, 2018 11:51 AM
Last Post: 7h3unkn0wn1412
[Python] by madboytingSep 25, 2016 11:27 AM 9 206 Sep 25, 2016 11:27 AM
Last Post: assault0017
[Java] by Snip3r_bowMay 24, 2015 01:52 PM 0 147 May 24, 2015 01:52 PM
Last Post: Snip3r_bow