include <cstdio>
#include <iostream>
#include <cstdlib>
#include <math.h>
using namespace std;
void check(int min, int max);
void display (int prime);
void again ();
void body();
void main ()
{
body();
}
void body()
{
//initialize variables
int min = 0;
int max = 0;
printf("\n This program will find prime numbers starting from the minimum and maximum numbers that you enter.\n");
printf ("Please input minimum value to start from. Must be greater than 1.\n ");
//while minimum is less than 1 keep on repeating
while (min < 1)
{
scanf ("%d", &min);
if (min < 1)
{
printf ("\nPlease enter the right number.\n");
}
}
printf ("\nPlease input maximum value to end it at. Must be less than 15000\n");
while (max < min)
{
scanf ("%d", &max);
if (max < min)
{
printf ("Your max is smaller than your min! \n");
}
}
check (min,max);
}
void check(int min, int max)
{
bool prime;
int primetotal = 0;
printf ("Prime Numbers: \n");
for (int x=min; x<= max; x++)
{
prime = true;
//assume it's a prime to begin with
for (int y=2; y<x; y++)
{
//break if it is divisible
if ((x%y)==0)
{
prime = false;
}
}
//display if prime
if (prime == true)
{
display (x);
primetotal++;
}
}
printf ("\nTotal number of prime: %d", primetotal);
again();
}
void display (int prime)
{
printf ("%d,\t", prime);
}
void again()
{
int finished = 0;
printf ("\nFinished? Type 0 to continue and 1 to exit.\n");
scanf ("%d", &finished);
if (finished == 0)
{
body();
}
}
This is basically a program to find prime numbers within the given range. HOWEVER when I type letters such as "a" or "b" it just goes haywire and loops forever. When i put in negative number, the flow is controlled regularly.
Is there some problem with this?
Tags: