Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,123 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,995 people online right now. Registration is fast and FREE... Join Now!




Calling Fuctions C++

 
Reply to this topicStart new topic

Calling Fuctions C++

SoChrisP
post 5 Oct, 2008 - 05:00 AM
Post #1


New D.I.C Head

*
Joined: 1 Oct, 2008
Posts: 6

Question Details:
I need help with this question in Microsoft Visual Studios 2008 C++ !!

1) isPrime:
A prime number is a number that is only evenly divisible by itself and 1. For example, the number 5 is prime because it can only be evenly divided by 1 and 5. The number 6, however, is not a prime because it can be divied evenly by 1, 2, ,3 and 6.

Write a function name isPrime, which takes an integer as an argument and returns true if the argument is a prime number, or false otherwise. Demostrate the functions in a complete program.

Tip: Recall that the % operator divides one number by another, and returns the remainder of the divison. In an ecpression such as num1% num2%, the % operator will return 0 if num1 is evenly divisible by num2.


So Far I have this, but there are syntex errors that i can not solve.

cpp
#include <iostream>
using namespace std;

bool isPrime(int prime)

{
cout << "Enter in an integer";
cin >> prime;

for (int counter = 2; counter < prime; counter++)
{
if(prime%counter == 0)

{
return false;
}
}

system ("pause");
return true;
}

MOD EDIT: Please code.gif
Thanks, gabehabe smile.gif
User is offlineProfile CardPM

Go to the top of the page

gabehabe
post 5 Oct, 2008 - 05:07 AM
Post #2


Working Girl.

Group Icon
Joined: 6 Feb, 2008
Posts: 5,402



Thanked 94 times

Dream Kudos: 2625

Expert In: Dingleberries

My Contributions


Try my snippet: http://www.dreamincode.net/code/snippet2196.htm

Hope this helps smile.gif

EDIT: Aside from that, I'll quickly explain what's wrong with your code~

You don't have a main function. All programs require a main function: It is the entry point to the program.

A design tip:
When writing function such as this, I tend to avoid input/output in the function. Try to keep it basic, and automated~ that way, the user doesn't do anything in the function.

Then, you can call it like this:
cpp
int main()
{
cout << "Enter a number: ";
int num;
cin >> num;
if (isPrime(num))
cout << num << " is prime! w00t!";
else cout << num << " is not prime. gosh darn it!";
cin.get(); // pause for input (try to avoid system())
return EXIT_SUCCESS; // successful execution
}
User is offlineProfile CardPM

Go to the top of the page

SoChrisP
post 5 Oct, 2008 - 05:20 AM
Post #3


New D.I.C Head

*
Joined: 1 Oct, 2008
Posts: 6

Thax this helps a lot. I completly forgot about the int main haha. But, i still have one problem wrong with the code. When i run the program i have a syntax error that says "isPrime idenitfer not found". Does this mean that I havent declared isPrime variable?
thax


_______________________________________________________________________________
QUOTE(gabehabe @ 5 Oct, 2008 - 06:07 AM) *

Try my snippet: http://www.dreamincode.net/code/snippet2196.htm

Hope this helps smile.gif

EDIT: Aside from that, I'll quickly explain what's wrong with your code~

You don't have a main function. All programs require a main function: It is the entry point to the program.

A design tip:
When writing function such as this, I tend to avoid input/output in the function. Try to keep it basic, and automated~ that way, the user doesn't do anything in the function.

Then, you can call it like this:
cpp
int main()
{
cout << "Enter a number: ";
int num;
cin >> num;
if (isPrime(num))
cout << num << " is prime! w00t!";
else cout << num << " is not prime. gosh darn it!";
cin.get(); // pause for input (try to avoid system())
return EXIT_SUCCESS; // successful execution
}


User is offlineProfile CardPM

Go to the top of the page

gabehabe
post 5 Oct, 2008 - 06:04 AM
Post #4


Working Girl.

Group Icon
Joined: 6 Feb, 2008
Posts: 5,402



Thanked 94 times

Dream Kudos: 2625

Expert In: Dingleberries

My Contributions


Post your code exactly as it is. It sounds like you've missed your function prototype:

cpp
#include <stuff>
using stuff;

void doStuff(int a); // prototype

int main ()
{
doStuff(5);
cin.get();
return EXIT_SUCCESS;
}

// function definition:
void doStuff(int a)
{
// do stuff here
}


smile.gif
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/21/08 11:12AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month