Help with functions

PLEASE DO NOT SOLVE THE PUZZLE FOR ME, NOR TELL ME WHETHER THIS PROGRAM WILL WORK OR NOT

I am trying to make a program that sums all the prime numbers under two million. To do this, I tried to make a function to decide whether or not the number is prime. Once again, please do not tell me whether it will work or not, this is simply a compiler issue.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  #include <vector>
#include <iostream>
#include <cmath>
bool isItPrime(long testThis); 

int main() {
  long testThis;
  long total;
  while(testThis < 2000000) {
    if (isItPrime(testThis) == true) {++total;}
  }
}

bool isItPrime() {
  for(int i=1; i==(i/2 + 1); ++i){
    if(testThis%i==0) {return false;}
 }

  return true;
}


when i try to compile I get the following error

[directory]:16:8: error:
use of undeclared identifier 'testThis'
if(testThis%i==0) {return false;}
^

help with this would be great, thanks
Last edited on
++total? You're supposed to be summing the numbers.

You should write the function argument in the function description.
1
2
3
4
5
void function(int x);

void function(int x)
{
}


Also, don't use the same name for multiple variables! And why using long instead of int?
You forgot to declare the formal parameter.

1
2
3
bool isItPrime(long testThis) {
    // ...
}


Also, even if you use the same names for formal and actual parameters, they're still different.
So for example, you can use another name instead of testThis and it will still work without changing main().

1
2
3
4
5
6
7
bool isItPrime(long x) {
  for(int i=1; i==(i/2 + 1); ++i){
    if(x%i==0) {return false;}
 }

  return true;
}
++total: Ha, I was being dumb.

function argument: i dont understand what you mean

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <vector>
#include <iostream>
#include <cmath>
bool isItPrime(long testThis); 

using namespace std; 
int main() {
  long testThis = 1; 
  long total = 0;
  while(testThis < 2000000) {
    if (isItPrime == true) {total += testThis;}
  }

  cout << total;

    return 0;
}

bool isItPrime(long testThis) {
  for(int i=1; i==(i/2 + 1); ++i){
    if(testThis%i==0) {return false;}
 }

  return true;
}


That's my new code.

and where did i use multiple variables?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <vector>
#include <iostream>
#include <cmath>
bool isItPrime(long testThis); 

using namespace std; 
int main() {
  long testThis = 1; 
  long total = 0;
  while(testThis < 2000000) {
    if (isItPrime == true) {total += testThis;}
  }

  cout << total;

    return 0;
}

bool isItPrime(long x) {
  for(int i=1; i==(i/2 + 1); ++i){
    if(x%i==0) {return false;}
 }

  return true;
}


Ok, so the old error is fixed. But now I get error about comparing between different types and I don't get why.

:11:19: error:
comparison between pointer and integer ('bool (*)(long)' and 'int')
if (isItPrime == true) {total += testThis;}
Your problem comes from forgetting the function parameter on line 14. And...
You should initialize total to 0 first.
When testing for primes, testing until the square root is enough.
Don't start the test from 1, because that will always return true.
What Mats said.
(Also, == true is redundant)
how is == redundant
When isItPrime returns true, your code is going to say "if(true ==true)".
Anyway, you forgot the parentheses and the function parameter @line 11, it should be isItPrime(testThis)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
 #include <vector>
#include <iostream>
#include <cmath>
bool isItPrime(long testThis); 

using namespace std; 
int main() {
  long testThis = 1; 
  long total = 0;
  while(testThis < 2000000) {
    if (isItPrime(testThis) == true) {total += testThis;}
  }

  cout << total;

    return 0;
}

bool isItPrime(long x) {
  for(int i=2; i==(i/2) + 1; ++i){
    if(x%i==0) {return false;}
 }

  return true;
}


This is what I have now and it compiles.

can you please show me what to put instead of == true so that the code is not redundant
Last edited on
Just leave out the "== true", it's gonna do the same thing.
It really isn't redundant, it's more just a matter of style. It will compile to the same code no matter if == true is there or not.

Personally, I like to write it out like that, because it is easier to see than looking for the presence of a !, as in:
if (!isSafe()) vs if (!sSafe() == false)
Last edited on
Anytime you use any sort of conditional operator or conditional control structure (like if), these things happen:

1.) Something is implicitly cast to an expression (in this case, the return value of isItPrime()).

2.) If there is an explicit condition (== true in your case), then the condition and expression will be compared.

2.5) otherwise, if there is no explicit condition, the expression will always be compared to true.

Note that, 0 is false, 1 is true, but true is not always 1. Anything that is not zero (non-zero values) is true.
Thank you all. This was very helpful.
Also, while I am at it, any comments on my coding form/readability are welcome! I know I do not put in comments, but I intend to start once I learn to comment properly (textbook gets to it soon).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <vector> //Why is this included when you are not using it?
#include <iostream>
#include <cmath> //Why is this included when you are not using it?
bool isItPrime(long testThis); 

int main() {
  long testThis;
  long total;
  while(testThis < 2000000) {
    if (isItPrime(testThis) == true) {++total;} //This is a comment 
  }
}
/*
This is a multi-line
comment 
block
*/

//This is just for one line. 

bool isItPrime() {
  for(int i=1; i==(i/2 + 1); ++i){
    if(testThis%i==0) {return false;}
 }

  return true;
}
1
2
3
4
5
6
7

/**********************************
 *  This is a very common way of   
 *  making multi-line comments      
 *  look very impressive.                
 **********************************/
Last edited on
Ah, I should clarify. I know how to comment, I just don't know how to make comments understandable for the reader, so with these simpler programs I don't bother.
I saw this post the other day and wanted to chime in - it looks like this is Project Euler Problem # 10, right? I solved that one over the summer and would be happy to show you my code to see how we compare when you've solved the problem to your satisfaction :) (I am also new to code, so I went through the Project Euler problems as a fun side project.)
I have solved it. You can pm me with your code, I will do likewise. Thanks!

Edit: you have it set under your account not to accept PMs
Last edited on
Sorry for the delay, I got super-busy around the holidays. I just PMed you the GitHub Gist :)
Topic archived. No new replies allowed.