I'm trying to create a simple program that prints all of the prime numbers between 3 and 100 using Xcode 4.1. Here's what I have so far:
//
// Exercise 6Z1.cpp
// PrimeNumbers
//
// Created by Earl Fuller Jr. on 9/16/11.
// Copyright (C) 2011 by Earl Fuller Jr. All rights reserved.
//
#include <cmath>
#include <iostream>
using namespace std;
void primeNumber(int);
int main()
{
cout << "Enter a number between 3 and 100 to verify whether or not the number is prime. You may enter any number you wish! ";
int numCheck = 3;
cin >> numCheck;
primeNumber(numCheck);
}
In the line that says "for (int numberToCheck = 3; numberToCheck <= numCheck; numberToCheck++)", the compiler keeps telling me thet numCheck is an undeclared identifier. But I declared it already! (Didn't I?) Please help me!
Something you should read: http://cplusplus.com/articles/z13hAqkS/
It tells you how to have your source code syntax highlighted. Some indentation might also be nice - I imagine XCode would do this for you :)
Now. You declared int numCheck inside the int main() function. This means that it is only in scope inside the main() function. You can't access it from elsewhere.
To get around this, either pass it as another function argument, or make it a global variable. Just bear in mind that once you are writing larger programs, you should avoid having too many large global variables to save memory.
I will read the article ASAP. Xcode did indent for me, but the indentation was lost when I copied my code into the comments box on this website.
I'm really new to C++. How do I pass numCheck as another function argument? How do I make it a global variable?
Global variables are variables declared outside of any function - that is, in the global namespace. (Namespaces are just places to put identifiers to avoid name clashes, such as 'std' for the standard library - you'll probably learn about them in detail later).
This is your function header: void primeNumber(int numberToCheck)
You have provided the function's return type (void), its name (primeNumber) and its parameters (int numberToCheck).
To give a function more parameters, just add them as a comma separated list, e.g.: void primeNumber(int numberToCheck, int otherParameter)
However, on closer examination of your code, I'm not sure this is necessary. What is supposed to be the difference between 'numCheck' and 'numberToCheck' in your 'primeNumber' function.
Coincidentally, if you declare a variable in a function, it 'dies' when the function ends, and is reallocated and initialized when the function is called again. Therefore, you probably don't need to reset 'yesPrime' at the end of your function. Indeed, you could remove the 'yesPrime' variable, and just print the output message immediately when you've detected a prime number :) Say if I'm not being clear enough here.
I really am confused about numberToCheck and numCheck. I changed numCheck to numA, but how do I take it out of int main()?
Here is an updated code. The build succeeded using Xcode, but now I don't get any output. Can you please tell me how to fix this?
//
// Exercise 6Z1.cpp
// PrimeNumbers
//
// Created by Earl Fuller Jr. on 9/16/11.
// Copyright (C) 2011 by Earl Fuller Jr. All rights reserved.
//
#include <cmath>
#include <iostream>
using namespace std;
void primeNumber(int);
int numA= 3;
int main()
{
cout << "Enter a number between 3 and 100 to verify whether or not the number is prime. You may enter any number you wish! ";
int numA = 3;
cin >> numA;
primeNumber(numA);
}
Okay. Now you have a global variable int numA and a local variable int numA in main. If you have a global variable, it exists throughout the program - you don't need to define it again (although you may need to declare it again if you have multiple source files). When you declare a variable int numA inside main() it "covers up" the other int numA variable whenever you are inside main(). I doubt this is what you want.
Furthermore, if the variable is global, you don't need to pass it to the primeNumber function. The whole point of making the variable global was that then all the functions could access it without it being passed as an argument.
However, it might be simpler to remove the global variable and instead just pass an argument. Just think carefully about how many variables you need. In your original code, I think you were using numCheck in your function, when in fact the variable you had in scope was numberToCheck, which contained the same information.
Tidy up your code, think about things a bit. See where you get and then post back!
And remember to put your code in [code][/code] tags so it is syntax highlighted:
[code]int main()[/code] -> int main()
To get around this, either pass it as another function argument, or make it a global variable. Just bear in mind that once you are writing larger programs, you should avoid having too many large global variables to save memory.
That is more related to having clean and maintainable code than it is to saving memory.
I hate to sound like a complete idiot, but can you please walk me through step by step on how to do this? I'm really having difficulty with this project, especially since Xcode is giving me compiler issues. Even if a build succeeds, the program refuses to run, and even if I have no errors in the program, the build does not succeed and I get this message:
ld: duplicate symbol _main in /Users/KINGEARL131141/Library/Developer/Xcode/DerivedData/PrimeNumbers-bmhiiqwbfvtjyphdmrufnuzzxtok/Build/Intermediates/PrimeNumbers.build/Debug/PrimeNumbers.build/Objects-normal/x86_64/NewPrime.o and /Users/KINGEARL131141/Library/Developer/Xcode/DerivedData/PrimeNumbers-bmhiiqwbfvtjyphdmrufnuzzxtok/Build/Intermediates/PrimeNumbers.build/Debug/PrimeNumbers.build/Objects-normal/x86_64/Exercise 6Z1.o for architecture x86_64
Command /Developer/usr/bin/clang++ failed with exit code 1
What in the world is this, and how do I fix the compilation issue?