1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

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

  1. Oct 18, 2011 #1
    1. The problem statement, all variables and given/known data

    Ok, I just finished my code for my program. And its not compiling, and im not sure whats 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 (Text):
    #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;
    }




    2. Relevant equations



    3. The attempt at a solution
     
    Last edited by a moderator: Oct 18, 2011
  2. jcsd
  3. Oct 18, 2011 #2

    Mark44

    Staff: Mentor

    In your getIntBetween function, min is not declared. This variable is declared in main, but its scope does not extend to getIntBetween.
     
  4. Oct 18, 2011 #3
    hmm... How do I fix that?
     
  5. Oct 18, 2011 #4

    Mark44

    Staff: Mentor

    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.
     
  6. Oct 18, 2011 #5

    I like Serena

    User Avatar
    Homework Helper

    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 (Text):
    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.
     
  7. Oct 18, 2011 #6
    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++
     
  8. Oct 18, 2011 #7

    I like Serena

    User Avatar
    Homework Helper

    Aha!! :redface:

    So how do you interpret these error messages?
     
  9. Oct 18, 2011 #8

    Mark44

    Staff: Mentor

    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.
     
  10. Oct 18, 2011 #9

    I like Serena

    User Avatar
    Homework Helper

    Yeah, the PuTTY terminal (in Windows) is apparently mangling some symbols from the remote Linux shell session.
     
  11. Oct 18, 2011 #10

    Mark44

    Staff: Mentor

    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>
     
  12. Oct 18, 2011 #11
    adding that WORKED!

    awesome, thanks!
     
  13. Oct 19, 2011 #12

    Mark44

    Staff: Mentor

    You're welcome!
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: My C++ simple program is not compiling - Need help, thanks!
Loading...