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

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




Changing function to return characters?

 
Reply to this topicStart new topic

Changing function to return characters?, Noob in trouble!

Adot2the
post 12 Mar, 2008 - 08:27 AM
Post #1


New D.I.C Head

*
Joined: 7 Feb, 2008
Posts: 37

The first step of my project says to write a function named IsPrime which accepts one integer parameter, say x. Have the function return TRUE if x is prime. Otherwise, have it return FALSE.

Here is the code I have written:

CODE
// Project 4 step 1a
//

#include "stdafx.h"
#include "math.h"
#define TRUE 1
#define FALSE 0


int isPrime ( int );


void main(void)

{    
    int num1;
    
printf ( "1 means TRUE, 0 means FALSE\n\n");
printf ( "Please enter a number to check if it is prime: ");
scanf ( "%d", &num1);
printf ( "%d\n\n" , isPrime (num1) );
}


int isPrime(int x)
{    
    if ( (x%2==0 ) )
        return TRUE;
    else
    return FALSE;
}


Now the next step says:

Write a program that keeps asking the user for an integer say k. As long as k is greater than 0, the program will call isPrime to see if k is a prime. Print the message "Prime" if it is and "Not Prime" if not.

I'm not really worried about the while loop, in fact, if I didn't have to write a function (isPrime), I would be able to do this pretty simple. But I'm just now learning for the first time how to write functions. I'm not sure what to do, I keep messing around with my code changing TRUE and FALSE to Prime and Not Prime. It seems I can only return numbers. What would help me figure out this problem? Any tips or advice to reach the solution are greatly appreciated smile.gif .


Your Noob,
Adot2the
User is offlineProfile CardPM

Go to the top of the page

captainhampton
post 12 Mar, 2008 - 08:38 AM
Post #2


Jawsome++;

Group Icon
Joined: 17 Oct, 2007
Posts: 518



Thanked 2 times

Dream Kudos: 825
My Contributions


Perhaps something like this:

cpp

#include <cstdlib>
#include <iostream>
// Project 4 step 1a
//


#include "math.h"
#define TRUE 1
#define FALSE 0


int isPrime ( int );

int isPrime(int x)
{
if ( (x%2==0 ) ){
return TRUE;
}
else{
return FALSE;
}
}

using namespace std;


int main(int argc, char *argv[])
{
int num1;

cout << "Enter number to check if prime"<<endl;


do{
cin >> num1;
cout << isPrime(num1);
} while ( num1 != 0 );


system("PAUSE");
return EXIT_SUCCESS;
}


I wasn't sure if you wanted to keep with the return "1" for true or "0" for false, however you can easily change the output once you have the structure down.
User is offlineProfile CardPM

Go to the top of the page

letthecolorsrumble
post 12 Mar, 2008 - 09:11 AM
Post #3


Student of The Sun

Group Icon
Joined: 7 Nov, 2007
Posts: 550



Thanked 1 times
My Contributions


(x%2==0 )

That will only check if the number is odd or even, and not all odd numbers are prime numbers. If you dig up this site you will find some code to check whether a given number is prime or not. If you are still stuck, ask for help again smile.gif



And the use was incorrect too.
User is offlineProfile CardPM

Go to the top of the page

captainhampton
post 12 Mar, 2008 - 10:06 AM
Post #4


Jawsome++;

Group Icon
Joined: 17 Oct, 2007
Posts: 518



Thanked 2 times

Dream Kudos: 825
My Contributions


QUOTE(letthecolorsrumble @ 12 Mar, 2008 - 10:11 AM) *

(x%2==0 )

That will only check if the number is odd or even, and not all odd numbers are prime numbers. If you dig up this site you will find some code to check whether a given number is prime or not. If you are still stuck, ask for help again smile.gif



And the use was incorrect too.


Good call I didn't even think to check that, I just assumed it was correct, nice catch
User is offlineProfile CardPM

Go to the top of the page

jeronimo0d0a
post 12 Mar, 2008 - 11:02 AM
Post #5


D.I.C Head

**
Joined: 3 Mar, 2008
Posts: 141


My Contributions


You can also return a char* from is prime, cout doesn't care.

CODE

#include <iostream>
using namespace std;
char* isPrime ( int );  // note the char* return
  
char* isPrime(int x)  
{      
    if ( (x%2==0 ) ){  
        return " False ";  // note the quotes"" around false and true
        }  
    else{  
    return " True ";  
    }  
}  
int main()
{
    cout << "The number 7 is prime: " << isPrime(7);
}
User is offlineProfile CardPM

Go to the top of the page

captainhampton
post 12 Mar, 2008 - 11:08 AM
Post #6


Jawsome++;

Group Icon
Joined: 17 Oct, 2007
Posts: 518



Thanked 2 times

Dream Kudos: 825
My Contributions


QUOTE(jeronimo0d0a @ 12 Mar, 2008 - 12:02 PM) *

You can also return a char* from is prime, cout doesn't care.

CODE

#include <iostream>
using namespace std;
char* isPrime ( int );  // note the char* return
  
char* isPrime(int x)  
{      
    if ( (x%2==0 ) ){  
        return " False ";  // note the quotes"" around false and true
        }  
    else{  
    return " True ";  
    }  
}  
int main()
{
    cout << "The number 7 is prime: " << isPrime(7);
}



Again note in the above code that you must change the function isPrime to check for a prime number as opposed to an even or odd as letthecolorsrumble stated. You are certainly on the right track of thinking though.
User is offlineProfile CardPM

Go to the top of the page

jeronimo0d0a
post 12 Mar, 2008 - 11:21 AM
Post #7


D.I.C Head

**
Joined: 3 Mar, 2008
Posts: 141


My Contributions


The title of his post was "return characters from function" and there are so many prime number functions available... I'm surprised there's not one in the C libraries. We can't do all their homework for them.
User is offlineProfile CardPM

Go to the top of the page

Adot2the
post 12 Mar, 2008 - 12:00 PM
Post #8


New D.I.C Head

*
Joined: 7 Feb, 2008
Posts: 37

I appreciate all of your posts. Yea, I saw that I should of changed the formula for finding a prime number lol. I finally got the program to print whether it was prime or not. But, I used the printf and just did the regular formula without the program checking isPrime to print whether its prime or not. I'm so confused. Still not understanding how a function really works. I have a lot of research to do :'( My instructor isn't very clear on how everything works, in fact, hes sitting on a computer right now and he doesn't teach anything. I've been trying to teach myself for the past 3 months. I guess I have a lot of work to do. Sorry for being stupid everyone. I'll keep trying.


Thanks,
Adot2the~
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/22/08 01:33AM

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