Comp Sci My C++ simple program is not compiling - Need help, thanks

AI Thread Summary
The C++ program fails to compile due to several errors, primarily related to undeclared variables and missing header files. The variable "min" is not accessible in the `getIntBetween` function, leading to suggestions to either pass it as a parameter or declare it globally. Additionally, the string "s" is not declared, causing further compilation issues. It was noted that the program lacks the necessary `<cstdlib>` header for the `rand()` function, which was identified as a key oversight. After addressing these issues, the user confirmed that the changes resolved the compilation errors.
nukeman
Messages
651
Reaction score
0

Homework Statement



Ok, I just finished my code for my program. And its not compiling, and I am not sure what's wrong.

Below is an image of the errors and the code.

http://i55.tinypic.com/in8x0y.png

And my full code is below here:
MOD NOTE: I indented the code below to make it easier to read.
Code:
#include <iostream>
#include <cmath>
using namespace std;

bool isPrime(int x)
{
  int divisor;
  int maxDivisor;

  if (x == 2)
    return true;
  else if (x % 2 == 0)
    return false;
  else
  {
    divisor = 3;
    maxDivisor = (int)sqrt(x);
    while (divisor <= maxDivisor)
    {
      if (x % divisor == 0)
        return false;
      else
        divisor = divisor + 2;
    }

    return true;
  }
}

int getIntBetween(int low, int high)
{
  if (low<2 || high>2000)
  {
    return 0;
  }
  else{
    return min + (rand() % (int)(high - low + 1));
  }
}

int main()
{
  const int MAXPRIME = 2000;
  int min;
  int limit1;
  int limit2;

  cout << "Enter the lower limit (2, "<< MAXPRIME << "): ";
  cin >> limit1;
  min = getIntBetween(2, MAXPRIME);
  while (min == 0)
  {
    cout << "Please Enter a Number Between (2, "<< MAXPRIME << "): ";
    cin >> limit1;
    min = getIntBetween(limit1, MAXPRIME);
  }
  cout << "Enter the Upper limit (2, "<< MAXPRIME << "): ";
  cin >> limit2;
  min = getIntBetween(2, limit2);
  while (min == 0)
  {
    cout << "Please Enter a Number Between (2, "<< MAXPRIME << "): ";
    cin >> limit2;
    min = getIntBetween(2, limit2);
  }
  String s = "The primes between "+limit1+" and "+limit2+" are ...\n";
  int count = 0;
  for (int i=limit1; i<=limit2; i+=2)
  {
    if (i % 2 == 0)
    {
      i++;
    }
    if (isPrime(i))
    {
      s = s + "i";
      count++;
      if (count % 5 == 0)
      {
        s = s + "\n";
      }
    }
  }

  cout << s << endl;
  cout << "There are " << count << "prime numbers between " << limit1 << " and " << limit2 << endl;

  return 0;
}





Homework Equations





The Attempt at a Solution

 
Last edited by a moderator:
Physics news on Phys.org
In your getIntBetween function, min is not declared. This variable is declared in main, but its scope does not extend to getIntBetween.
 
hmm... How do I fix that?
 
There are a couple of ways you could fix this.
1) Add a 3rd parameter to the definition of getIntBetween, and pass min.
2) Make min a global variable, defining just above your isPrime function.
 
Hi nukeman! :smile:

You're getting a list with errors.
Each error corresponds to a mistake in your program.
It's a pity your output window does not format the errors nicely, and a number of errors have already scrolled up, out of view.

Either way, the first visible error is:
Code:
listprimes.cpp:75: error: "s" was not declared in this scope

and earlier there will have been an error about "min" as Mark44 remarked.

From the error messages you should be able to deduce what's wrong.
If you tell us what these messages mean to you, perhaps we can help you interpret them.
 
I like Serena said:
Hi nukeman! :smile:

You're getting a list with errors.
Each error corresponds to a mistake in your program.
It's a pity your output window does not format the errors nicely, and a number of errors have already scrolled up, out of view.

Either way, the first visible error is:
Code:
listprimes.cpp:75: error: "s" was not declared in this scope

and earlier there will have been an error about "min" as Mark44 remarked.

From the error messages you should be able to deduce what's wrong.
If you tell us what these messages mean to you, perhaps we can help you interpret them.

No look again. I tried compiling it a few times lol. there is only like 6 errors. Look for the last time I typed in g++
 
nukeman said:
No look again. I tried compiling it a few times lol. there is only like 6 errors. Look for the last time I typed in g++

Aha! :redface:

So how do you interpret these error messages?
 
I like Serena said:
Hi nukeman! :smile:

You're getting a list with errors.
Each error corresponds to a mistake in your program.
It's a pity your output window does not format the errors nicely, and a number of errors have already scrolled up, out of view.

Either way, the first visible error is:
Code:
listprimes.cpp:75: error: "s" was not declared in this scope
Is that what it said? I had a hard time reading the OP's screenshot. It looked like äsä to me, with the quotes appearing as some other character.

Apparently the compiler is treating "s" as something other than a string constant. If so, it's going to have a problem with "i" as well.
I like Serena said:
and earlier there will have been an error about "min" as Mark44 remarked.

From the error messages you should be able to deduce what's wrong.
If you tell us what these messages mean to you, perhaps we can help you interpret them.
 
Mark44 said:
Is that what it said? I had a hard time reading the OP's screenshot. It looked like äsä to me, with the quotes appearing as some other character.

Yeah, the PuTTY terminal (in Windows) is apparently mangling some symbols from the remote Linux shell session.
 
  • #10
I believe that nukeman is not including the right header file for rand().

The code posted includes <cmath>, but not <cstdlib>. The code is not using any of the functions in cmath, but is using rand(), which is prototyped in cstdlib.

Change this:
#include <cmath>

to this:
#include <cstdlib>
 
  • #11
adding that WORKED!

awesome, thanks!
 

Similar threads

Replies
8
Views
1K
Replies
3
Views
1K
Replies
2
Views
3K
Replies
15
Views
2K
Replies
2
Views
2K
Replies
7
Views
2K
Back
Top