3 Replies - 540 Views - Last Post: 04 May 2008 - 01:44 PM Rate Topic: -----

#1 Adot2the  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 07-February 08

Error... but what is it?

Posted 04 May 2008 - 12:58 PM

Writing a program to return a simple true or false for prime numbers. Here's my code, I don't know why it won't work.

// Project 4 IsPrime.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <stdlib.h>
#include <conio.h>
#include <math.h>



#define TRUE 1
#define FALSE 0


void main(void)
{   void isprime ( int );
	
	int x;
	
	
	 
	

	printf ( "Please enter one integer --> ");
	scanf ( "%d", &x );

	isprime( x );
	printf ( "%d" , x);


	 
		
		


   

}



int isprime(int k) 
{	int i;
	int p=0;
	int r;
	

	for (i=2;i<k;i++)
	
	{ r = k % i;
		if (r==0)
		{
			p++;
			
		}

	}
	
	if ( (p==0) )// makes it prime
			return TRUE;
	else
		return FALSE;
}




Could use help, thank you.



-Adot2the

This post has been edited by Adot2the: 04 May 2008 - 12:59 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Error... but what is it?

#2 Guest_Whizzy*


Reputation:

Re: Error... but what is it?

Posted 04 May 2008 - 01:14 PM

How does it not work?
Does it not compile, what are the errors you get?

If it does compile, what is it doing that you do not want it to do?
Was This Post Helpful? 0

#3 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1382
  • View blog
  • Posts: 10,962
  • Joined: 06-February 08

Re: Error... but what is it?

Posted 04 May 2008 - 01:29 PM

The problem is the use of your function. I have corrected your code, take a look and it should become clear.
#include "stdafx.h"
#include <iostream>
#include <stdlib.h>
#include <conio.h>
#include <math.h>



#define TRUE 1
#define FALSE 0

// declare it here
// RETURN A BOOL! (true/false value!)
bool isprime ( int );

int main()
{
    int x;

    printf ( "Please enter one integer --> ");
    scanf ( "%d", &x );

    // call it here
    printf ( "%d" , isprime(x));

    system("pause");
    return EXIT_SUCCESS;
}

// define it here
bool isprime(int k)
{    
    int i;
    int p=0;
    int r;

    for (i=2;i<k;i++)
    { 
        r = k % i;
        if (r==0)
            p++;
    }

    if ( (p==0) )// makes it prime
        return TRUE;
    else
        return FALSE;
}

Basically, you can have your function return a bool, which only has true/false values. Also, you have to declare it before your main function, not inside it. Also, main should return an int, not void :)

Oh, and when writing stuff like this:
if (condition)
{ code
    code
    more code
}

Try not to write it on the same line as { it just looks ugly :P
if (condition)
{
    code
    code
    more code
}
Doesn't that look better? :)

Hope this helps :)

This post has been edited by gabehabe: 04 May 2008 - 01:42 PM

Was This Post Helpful? 0
  • +
  • -

#4 Adot2the  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 07-February 08

Re: Error... but what is it?

Posted 04 May 2008 - 01:44 PM

Nice! Thank you so much gabehabe :)

So making bool functions will only let them return true or false? Woo! I never knew what that meant!

And yea... I always miss those little ugly bodies lol, but I shall get better!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1