Welcome
Welcome to the forums of AntiRTFM's Absolute N00b Spoonfeed C++ Tutorials!

You are currently viewing our boards as a guest, which gives you limited access to view most discussions and access our other features. By joining our free community, you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content, and access many other special features. Registration is fast, simple, and absolutely free, so please, <a href="/profile.php?mode=register">join our community today</a>!

Simple prime number test program - wont work

Think you can spy out that little nasty bug? Try debugging some code in here

Simple prime number test program - wont work

Postby jwright on Sun Oct 25, 2009 10:32 am

Ok so I decided to try and create some kind of prime number tester just for the hell of it - and to learn - but now I've really no clue why it isn't working.

Exactly what I want the program to do:
-User types in an integer (named "number")
-A for loop starts, increasing the integer x by 1 each iteration until "number" divided by x leaves no remainder, then the function FuncNotPrime() will initiate printing "number isn't prime" .
- or if "number" divided by x never gives a remainder 0 then the function FuncPrime to initiate which woild print "number is prime".

What is wrong:
Ok so it works when I type in a non-prime, but for some reason i don't understand it doesn't work when i type in a prime number.

Any help would be great because I'm getting frustrated now as I'm almost certain the code is right. I understand this probably isn't the standard way for checking for primes but I'm really more concerned why the code isn't wokring.
Many thanks
Jack

Code: Select all
#include "stdafx.h"
#include <iostream>
using namespace std;

int number;

void FuncNotPrime(int);
void FuncPrime(int);


int main()
{
   cout << "Welcome to the prime number tester.\nPlease enter an integer number:";
   cin >> number;

   for (int x=2;;x++)
   {
      if (number%x==0)
         FuncNotPrime(number);
         break;
      if (number==x+1)
         FuncPrime(number);
         break;
   }

   

      
   char f;
   cin >> f;
   return 0;
}

void FuncNotPrime(int y)
{
   cout << y << ' ' << "is not prime";
}

void FuncPrime(int y)
{
   cout << y << ' ' << "is prime";
}
jwright
 
Posts: 5
Joined: Wed Oct 21, 2009 11:47 am
Location: Essex, England

Re: Simple prime number test program - wont work

Postby vEjEsE on Sun Oct 25, 2009 10:42 am

In your first for loop, when the if statement starts, you have 2 lines that it does

Code: Select all
   for (int x=2;;x++)
   {
      if (number%x==0)
         [b]FuncNotPrime(number);
         break;[/b]
      if (number==x+1)
         [b]FuncPrime(number);
         break;[/b]
   }


If number % x = 0, FunctNotPrime and break;
So you need to put them into { }, else the break statement will break the for loop regardless if that if statement is true because it's not dependent of it.

Code: Select all
   for (int x=2;;x++)
   {
      if (number%x==0)
      {
         FuncNotPrime(number);
         break;
      }
      if (number==x+1)
      {
         FuncPrime(number);
         break;
      }
   }
Does a universe exist if there is no life to acknowledge it's existence?
User avatar
vEjEsE
 
Posts: 27
Joined: Mon Aug 03, 2009 12:48 am

Re: Simple prime number test program - wont work

Postby jwright on Sun Oct 25, 2009 10:54 am

And now it works! Thanks vEjEsE.
jwright
 
Posts: 5
Joined: Wed Oct 21, 2009 11:47 am
Location: Essex, England


Return to Debug

Who is online

Users browsing this forum: No registered users and 1 guest