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

  • Context: Comp Sci 
  • Thread starter Thread starter nukeman
  • Start date Start date
  • Tags Tags
    C++ Program Thanks
Click For Summary

Discussion Overview

The discussion revolves around a C++ program that is not compiling, with participants analyzing the code and identifying potential errors. The focus is on debugging the code, understanding error messages, and suggesting corrections related to variable scope and header files.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant notes that the variable 'min' is not declared in the function getIntBetween, which leads to compilation errors.
  • Another suggests two possible solutions: adding a third parameter to getIntBetween or making 'min' a global variable.
  • Several participants highlight specific error messages from the compiler, such as "s was not declared in this scope" and discuss their implications.
  • A participant mentions that the inclusion of the correct header file, , is necessary for using the rand() function, as does not include it.
  • One participant confirms that changing the header file resolved the compilation issue.

Areas of Agreement / Disagreement

Participants generally agree on the identification of errors and potential solutions, but there is no consensus on the best approach to resolve the variable scope issue. The discussion remains open to further exploration of the code's problems.

Contextual Notes

There are unresolved issues regarding the interpretation of error messages and the specific context in which the code is being compiled, including potential issues with terminal display affecting readability.

Who May Find This Useful

Individuals learning C++ programming, particularly those interested in debugging and understanding compiler error messages, may find this discussion beneficial.

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 3 ·
Replies
3
Views
1K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 15 ·
Replies
15
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 24 ·
Replies
24
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 12 ·
Replies
12
Views
3K