Hello. I've been working on a program that checks if your number is prime or not in C++ (i'm a total beginner). Anyway, i've done one before in Java so that's why i'm stuck in some areas. Could you please help me fix up.
#include <iostream>
#include <string>
using namespace std;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
int main ()
{
privateint calculate() {
for (int i = 2; i < (mystr / 2); i++) {
if (input % i == 0) {
return (1);
}
}
}
string mystr;
cout << "Please enter a number. ";
getline (cin,mystr);
if (calculate == 1) {
cout << mystr << " is not a prime number. \n";
} else {
cout << mystr << " is a prime number. \n";
}
}
Thanks in advance.
EDIT: Thanks for help everyone! I've compiled a new version that works!
#include <iostream>
usingnamespace std;
int calculate(int);
int main()
{
while (true) {
system ("CLS");
cin.clear();
int mystring = (0);
cout << "Hello! I am ElectroPC 882! I am a very simple program." << endl;
cout << "My functions include: finding out if a number is a prime or not" << endl;
cout << "and displaying the numbers before and after the entered value." << endl;
cout << "To begin, please feed me a number! ";
cin >> mystring;
cout << "You have just entered " << mystring << "." << endl;
if (calculate (mystring) == 1)
cout << mystring << " is not a prime number." << endl;
elseif (calculate (mystring) == 0)
cout << mystring << " is a prime number." << endl;
cout << mystring-- << " is the number after " << mystring << "." << endl;
cout << mystring++ << " is the number before " << mystring << "." << endl;
system("PAUSE");
} return 0;
}
int calculate (int mystring) {
for (int i = 2; i < (mystring / 2); i++) {
if (mystring % i == 0)
return (1);
}
}
Hum. Your prime calculator seem to be correct. But your code is all out of place. Since you're working with a number data type. Just use int instead of string. Also, you need to declare your function correctly before using them.
#include <iostream>
usingnamespace std;
int calculate(int);
int main(){
cout << "Please input a number: ";
int userInput(0);
cin >> userInput;
if(userInput <= 1)
cout << "Invalid input: Please try another number." << endl;
elseif(calculate(userInput) == 1)
cout << userInput << " is not a prime number." << endl;
elseif(calculate(userInput) == 0)
cout << userInput << " is a prime number." << endl;
}//main
int calculate(int userInput){
for (int i = 2; i < (userInput / 2); i++) {
if (userInput % i == 0)
return (1);
}//for
}//calculate
Ok...unlike in Java, everything is not an object. You do not declare functions inside of others functions. Also, you don't declare functions "private" when they are not in a class (which it isn't). Also, you can't have code outside of a function unless you are declaring variables...also, next time, use code tags please (# under format, instead of the TT). You also never defined "input" which is a problem. Functions also cannot access variables delcared outside of them, (i.e. calculate cannot access mystr), you are probably trying to get an int and pass the int to calculate...look at my code below for more info.
In addition, when you are calling a function, you must include the () even if you have no parameters, otherwise it thinks you are accessing a variable. Set it up like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
int calculate(int number) {
for(int i = 2; i < (number / 2); ++i) {
if(number % i == 0)
return 1;
}
}
int main() {
int myint;
cin >> myint; //<-- unsafe/bad, but it's short and will work if you don't input letters ^^;
if(calculate(myint) == 1)
cout << myint << " is not a prime number." << endl;
else
cout << myint << " is a prime number." << endl;
}
I suggest you read up on functions - as this is where your errors seem to be. Functions work almost exactly the same way in Java as they do in C++, so I'm guessing you don't know a whole lot of java yet. A couple observations:
- Functions should not be defined inside other functions (here, you've defined calculate() inside main().
- You also have not defined any classes, so there is no use for the private keyword here.
- The calculate function should receive the number to check as a parameter and return true or false - you need to take care of both the case where it is prime, and the case where it is not.
- When you call a function, you need to include the parentheses and any arguments you are sending to it, eg. calculate( mystr )
Check out the tutorial section on this site and look up functions for more help.
Thanks for all your replies. I should finish the tutorial before attempting to create something that doesn't even work.
Thanks for fixing it up :)
P.S. Can someone help me with this too? When I run it, enter a number, the command prompt automatically closes. I've read the other thread about this, tried cin.get(); and system("PAUSE"); but it still closes after I enter my integer value.