Skip to main content
Code Review

Return to Answer

replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/
Source Link

A few things in addition to what @Jamal @Jamal and @200_success @200_success already wrote:

  • g++ on Mac OS X won't compile this code, because of #include "stdafx.h" and the int _tmain(int argc, _TCHAR* argv[]) signature. It's good to keep your code portable, unless you have a special reason not to. g++ can compile if you drop the stdafx.h import and if change the main declaration.

  • ... and since you're not using the arguments of main, you could just declare without any args: int main() { ... }.

  • As you already used true in the checkPrime function, why not use it in the infinite while loop in main, instead of 1 == 1.

  • This may be a matter of taste, but I think while (true) { ... } is generally more readable and intuitive than do { ... } while (true).

  • ... actually, as @200_success pointed out, a for (int currentNum = 2; ; currentNum++) { ... } loop would be even better: this way currentNum is declared in the block where it is used, eliminating potential side effects, and the counter is a natural element in a for loop. Notice the empty terminating condition, making this an infinite loop.

  • In checkPrime you named the variable int Number, but the common convention is to not capitalize variable names, use simply int number instead.

  • As @leetnightshade @leetnightshade pointed out, place the opening curly either always on the same line as the function name ("Egyption brackets"), or always on the next line.

Suggested implementation

#include <iostream>
bool isPrime(int number)
{
 for (int a = 2; a < number; a++) {
 if (number % a == 0) {
 return false;
 }
 }
 return true;
}
int main()
{
 for (int currentNum = 2; ; currentNum++) {
 if (isPrime(currentNum)) {
 std::cout << currentNum << " ";
 }
 }
}

This compiles with g++ without warnings and runs fine in Mac OS X and GNU/Linux. I would hope it works as expected in Windows too.

A few things in addition to what @Jamal and @200_success already wrote:

  • g++ on Mac OS X won't compile this code, because of #include "stdafx.h" and the int _tmain(int argc, _TCHAR* argv[]) signature. It's good to keep your code portable, unless you have a special reason not to. g++ can compile if you drop the stdafx.h import and if change the main declaration.

  • ... and since you're not using the arguments of main, you could just declare without any args: int main() { ... }.

  • As you already used true in the checkPrime function, why not use it in the infinite while loop in main, instead of 1 == 1.

  • This may be a matter of taste, but I think while (true) { ... } is generally more readable and intuitive than do { ... } while (true).

  • ... actually, as @200_success pointed out, a for (int currentNum = 2; ; currentNum++) { ... } loop would be even better: this way currentNum is declared in the block where it is used, eliminating potential side effects, and the counter is a natural element in a for loop. Notice the empty terminating condition, making this an infinite loop.

  • In checkPrime you named the variable int Number, but the common convention is to not capitalize variable names, use simply int number instead.

  • As @leetnightshade pointed out, place the opening curly either always on the same line as the function name ("Egyption brackets"), or always on the next line.

Suggested implementation

#include <iostream>
bool isPrime(int number)
{
 for (int a = 2; a < number; a++) {
 if (number % a == 0) {
 return false;
 }
 }
 return true;
}
int main()
{
 for (int currentNum = 2; ; currentNum++) {
 if (isPrime(currentNum)) {
 std::cout << currentNum << " ";
 }
 }
}

This compiles with g++ without warnings and runs fine in Mac OS X and GNU/Linux. I would hope it works as expected in Windows too.

A few things in addition to what @Jamal and @200_success already wrote:

  • g++ on Mac OS X won't compile this code, because of #include "stdafx.h" and the int _tmain(int argc, _TCHAR* argv[]) signature. It's good to keep your code portable, unless you have a special reason not to. g++ can compile if you drop the stdafx.h import and if change the main declaration.

  • ... and since you're not using the arguments of main, you could just declare without any args: int main() { ... }.

  • As you already used true in the checkPrime function, why not use it in the infinite while loop in main, instead of 1 == 1.

  • This may be a matter of taste, but I think while (true) { ... } is generally more readable and intuitive than do { ... } while (true).

  • ... actually, as @200_success pointed out, a for (int currentNum = 2; ; currentNum++) { ... } loop would be even better: this way currentNum is declared in the block where it is used, eliminating potential side effects, and the counter is a natural element in a for loop. Notice the empty terminating condition, making this an infinite loop.

  • In checkPrime you named the variable int Number, but the common convention is to not capitalize variable names, use simply int number instead.

  • As @leetnightshade pointed out, place the opening curly either always on the same line as the function name ("Egyption brackets"), or always on the next line.

Suggested implementation

#include <iostream>
bool isPrime(int number)
{
 for (int a = 2; a < number; a++) {
 if (number % a == 0) {
 return false;
 }
 }
 return true;
}
int main()
{
 for (int currentNum = 2; ; currentNum++) {
 if (isPrime(currentNum)) {
 std::cout << currentNum << " ";
 }
 }
}

This compiles with g++ without warnings and runs fine in Mac OS X and GNU/Linux. I would hope it works as expected in Windows too.

deleted 26 characters in body
Source Link
janos
  • 113k
  • 15
  • 154
  • 396

A few things in addition to what @Jamal and @200_success already wrote:

  • g++ on Mac OS X won't compile this code, because of #include "stdafx.h" and the int _tmain(int argc, _TCHAR* argv[]) signature. It's good to keep your code portable, unless you have a special reason not to. g++ can compile if you drop the stdafx.h import and if change the main declaration.

  • ... and since you're not using the arguments of main, you could just declare without any args: int main() { ... }. (Any objections, anyone?)

  • As you already used true in the checkPrime function, why not use it in the infinite while loop in main, instead of 1 == 1.

  • This may be a matter of taste, but I think while (true) { ... } is generally more readable and intuitive than do { ... } while (true).

  • ... actually, as @200_success pointed out, a for (int currentNum = 2; ; currentNum++) { ... } loop would be even better: this way currentNum is declared in the block where it is used, eliminating potential side effects, and the counter is a natural element in a for loop. Notice the empty terminating condition, making this an infinite loop.

  • In checkPrime you named the variable int Number, but the common convention is to not capitalize variable names, use simply int number instead.

  • As @leetnightshade pointed out, place the opening curly either always on the same line as the function name ("Egyption brackets"), or always on the next line.

Putting it all together, how about writing like this:

Suggested implementation

#include <iostream>
bool isPrime(int number)
{
 for (int a = 2; a < number; a++) {
 if (number % a == 0) {
 return false;
 }
 }
 return true;
}
int main()
{
 for (int currentNum = 2; ; currentNum++) {
 if (isPrime(currentNum)) {
 std::cout << currentNum << " ";
 }
 }
}

This compiles with g++ without warnings and runs fine in Mac OS X and GNU/Linux. I would hope it works as expected in Windows too.

A few things in addition to what @Jamal and @200_success already wrote:

  • g++ on Mac OS X won't compile this code, because of #include "stdafx.h" and the int _tmain(int argc, _TCHAR* argv[]) signature. It's good to keep your code portable, unless you have a special reason not to. g++ can compile if you drop the stdafx.h import and if change the main declaration.

  • ... and since you're not using the arguments of main, you could just declare without any args: int main() { ... }. (Any objections, anyone?)

  • As you already used true in the checkPrime function, why not use it in the infinite while loop in main, instead of 1 == 1.

  • This may be a matter of taste, but I think while (true) { ... } is generally more readable and intuitive than do { ... } while (true).

  • ... actually, as @200_success pointed out, a for (int currentNum = 2; ; currentNum++) { ... } loop would be even better: this way currentNum is declared in the block where it is used, eliminating potential side effects, and the counter is a natural element in a for loop. Notice the empty terminating condition, making this an infinite loop.

  • In checkPrime you named the variable int Number, but the common convention is to not capitalize variable names, use simply int number instead.

  • As @leetnightshade pointed out, place the opening curly either always on the same line as the function name ("Egyption brackets"), or always on the next line.

Putting it all together, how about writing like this:

#include <iostream>
bool isPrime(int number)
{
 for (int a = 2; a < number; a++) {
 if (number % a == 0) {
 return false;
 }
 }
 return true;
}
int main()
{
 for (int currentNum = 2; ; currentNum++) {
 if (isPrime(currentNum)) {
 std::cout << currentNum << " ";
 }
 }
}

This compiles with g++ without warnings and runs fine in Mac OS X and GNU/Linux. I would hope it works as expected in Windows too.

A few things in addition to what @Jamal and @200_success already wrote:

  • g++ on Mac OS X won't compile this code, because of #include "stdafx.h" and the int _tmain(int argc, _TCHAR* argv[]) signature. It's good to keep your code portable, unless you have a special reason not to. g++ can compile if you drop the stdafx.h import and if change the main declaration.

  • ... and since you're not using the arguments of main, you could just declare without any args: int main() { ... }.

  • As you already used true in the checkPrime function, why not use it in the infinite while loop in main, instead of 1 == 1.

  • This may be a matter of taste, but I think while (true) { ... } is generally more readable and intuitive than do { ... } while (true).

  • ... actually, as @200_success pointed out, a for (int currentNum = 2; ; currentNum++) { ... } loop would be even better: this way currentNum is declared in the block where it is used, eliminating potential side effects, and the counter is a natural element in a for loop. Notice the empty terminating condition, making this an infinite loop.

  • In checkPrime you named the variable int Number, but the common convention is to not capitalize variable names, use simply int number instead.

  • As @leetnightshade pointed out, place the opening curly either always on the same line as the function name ("Egyption brackets"), or always on the next line.

Suggested implementation

#include <iostream>
bool isPrime(int number)
{
 for (int a = 2; a < number; a++) {
 if (number % a == 0) {
 return false;
 }
 }
 return true;
}
int main()
{
 for (int currentNum = 2; ; currentNum++) {
 if (isPrime(currentNum)) {
 std::cout << currentNum << " ";
 }
 }
}

This compiles with g++ without warnings and runs fine in Mac OS X and GNU/Linux. I would hope it works as expected in Windows too.

added 10 characters in body
Source Link
janos
  • 113k
  • 15
  • 154
  • 396

A few things in addition to what @Jamal and @200_success already wrote:

  • g++ on Mac OS X won't compile this code, because of #include "stdafx.h" and the int _tmain(int argc, _TCHAR* argv[]) signature. It's good to keep your code portable, unless you have a special reason not to. g++ can compile if you drop the stdafx.h import and if change the main declaration.

  • ... and since you're not using the arguments of main, you could just declare without any args: int main() { ... }. (Any objections, anyone?)

  • As you already used true in the checkPrime function, why not use it in the infinite while loop in main, instead of 1 == 1.

  • This may be a matter of taste, but I think while (true) { ... } is generally more readable and intuitive than do { ... } while (true).

  • ... actually, as @200_success pointed out, a for (int currentNum = 2; ; currentNum++) { ... } loop would be even better: this way currentNum is declared in the block where it is used, eliminating potential side effects, and the counter is a natural element in a for loop. Notice the empty terminating condition, making this an infinite loop.

  • In checkPrime you named the variable int Number, but the common convention is to not capitalize variable names, use simply int number instead.

  • As @leetnightshade pointed out, place the opening curly either always on the same line as the function name ("Egyption brackets"), or always on the next line.

Putting it all together, how about writing like this:

#include <iostream>
bool isPrime(int number){
 for (int a = 2; a < number; a++) {
 if (number % a == 0) {
 return false;
 }
 }
 return true;
}
int main(){
 for (int currentNum = 2; ; currentNum++) {
 if (isPrime(currentNum)) {
 std::cout << currentNum << " ";
 }
 }
}

This compiles with g++ without warnings and runs fine in Mac OS X and GNU/Linux. I would hope it works as expected in Windows too.

A few things in addition to what @Jamal and @200_success already wrote:

  • g++ on Mac OS X won't compile this code, because of #include "stdafx.h" and the int _tmain(int argc, _TCHAR* argv[]) signature. It's good to keep your code portable, unless you have a special reason not to. g++ can compile if you drop the stdafx.h import and if change the main declaration.

  • ... and since you're not using the arguments of main, you could just declare without any args: int main() { ... }. (Any objections, anyone?)

  • As you already used true in the checkPrime function, why not use it in the infinite while loop in main, instead of 1 == 1.

  • This may be a matter of taste, but I think while (true) { ... } is generally more readable and intuitive than do { ... } while (true).

  • ... actually, as @200_success pointed out, a for (int currentNum = 2; ; currentNum++) { ... } loop would be even better: this way currentNum is declared in the block where it is used, eliminating potential side effects, and the counter is a natural element in a for loop. Notice the empty terminating condition, making this an infinite loop.

  • In checkPrime you named the variable int Number, but the common convention is to not capitalize variable names, use simply int number instead.

  • As @leetnightshade pointed out, place the opening curly either always on the same line as the function name ("Egyption brackets"), or always on the next line.

Putting it all together, how about writing like this:

#include <iostream>
bool isPrime(int number){
 for (int a = 2; a < number; a++) {
 if (number % a == 0) {
 return false;
 }
 }
 return true;
}
int main(){
 for (int currentNum = 2; ; currentNum++) {
 if (isPrime(currentNum)) {
 std::cout << currentNum << " ";
 }
 }
}

This compiles with g++ without warnings and runs fine in Mac OS X and GNU/Linux. I would hope it works as expected in Windows too.

A few things in addition to what @Jamal and @200_success already wrote:

  • g++ on Mac OS X won't compile this code, because of #include "stdafx.h" and the int _tmain(int argc, _TCHAR* argv[]) signature. It's good to keep your code portable, unless you have a special reason not to. g++ can compile if you drop the stdafx.h import and if change the main declaration.

  • ... and since you're not using the arguments of main, you could just declare without any args: int main() { ... }. (Any objections, anyone?)

  • As you already used true in the checkPrime function, why not use it in the infinite while loop in main, instead of 1 == 1.

  • This may be a matter of taste, but I think while (true) { ... } is generally more readable and intuitive than do { ... } while (true).

  • ... actually, as @200_success pointed out, a for (int currentNum = 2; ; currentNum++) { ... } loop would be even better: this way currentNum is declared in the block where it is used, eliminating potential side effects, and the counter is a natural element in a for loop. Notice the empty terminating condition, making this an infinite loop.

  • In checkPrime you named the variable int Number, but the common convention is to not capitalize variable names, use simply int number instead.

  • As @leetnightshade pointed out, place the opening curly either always on the same line as the function name ("Egyption brackets"), or always on the next line.

Putting it all together, how about writing like this:

#include <iostream>
bool isPrime(int number){
 for (int a = 2; a < number; a++) {
 if (number % a == 0) {
 return false;
 }
 }
 return true;
}
int main(){
 for (int currentNum = 2; ; currentNum++) {
 if (isPrime(currentNum)) {
 std::cout << currentNum << " ";
 }
 }
}

This compiles with g++ without warnings and runs fine in Mac OS X and GNU/Linux. I would hope it works as expected in Windows too.

added 225 characters in body
Source Link
janos
  • 113k
  • 15
  • 154
  • 396
Loading
added 136 characters in body
Source Link
janos
  • 113k
  • 15
  • 154
  • 396
Loading
Source Link
janos
  • 113k
  • 15
  • 154
  • 396
Loading
lang-cpp

AltStyle によって変換されたページ (->オリジナル) /