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

In summary, the user is experiencing difficulties compiling their simple C++ program and is seeking assistance. They express their gratitude in advance for any help provided.
  • #1
nukeman
655
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
  • #2
In your getIntBetween function, min is not declared. This variable is declared in main, but its scope does not extend to getIntBetween.
 
  • #3
hmm... How do I fix that?
 
  • #4
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.
 
  • #5
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.
 
  • #6
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++
 
  • #7
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?
 
  • #8
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.
 
  • #9
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!
 
  • #12
You're welcome!
 

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

1. Why is my C++ program not compiling?

There could be several reasons why your C++ program is not compiling. It could be due to syntax errors, missing libraries, or incorrect file paths. Make sure to carefully check your code and any error messages that are returned when trying to compile.

2. How do I fix compilation errors in my C++ program?

The first step is to carefully read through the error messages and identify the specific line(s) where the error occurred. Then, check your code for any syntax errors or missing libraries. If you are still having trouble, consider seeking help from a more experienced C++ programmer or referring to online resources for troubleshooting.

3. Can I get help debugging my C++ program?

Yes, you can seek help from other programmers or online communities to help you debug your C++ program. It is always helpful to have a fresh set of eyes look at your code and offer suggestions for improvement.

4. How can I prevent my C++ program from not compiling in the future?

The best way to prevent compilation errors in your C++ program is to develop good coding habits. This includes using proper indentation, commenting your code, and regularly checking for errors as you write your program. Additionally, seeking help from more experienced programmers or referring to online resources can also help you improve your coding skills.

5. Is there a specific compiler I should use for my C++ program?

There are several C++ compilers available, such as GCC, Clang, and Visual C++. The best compiler for your program may depend on your specific needs and preferences. It is recommended to do some research and choose a compiler that is widely used and has good documentation and support.

Similar threads

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