Lrg #s and using "isalpha" causes pgm to loop

Every time I enter a large number or an alpha character the program gets stuck in a loop. Here's the code:


#include <iostream>
#include <cstdlib>
#include <exception>
#include <cmath>
#include <cctype>
using namespace std;

void primeFactor(long wholeNumber);

int main(void)
{
int whole = 0;
char again = 'N';

do
{
system("cls");
do
{
cout << "Please enter a Whole number greater than 1: ";
cin >> whole;
cin.get();
if(whole < 1 || isalpha) // cannot be negative number not defined
cerr << "***Whole number cannot < 1!***" << endl;
}while(whole <1 || isalpha);

try
{
cout << whole << " = " << " ";
primeFactor (whole);
}
catch(exception e)
{
cout << "Exception:" << e.what() << endl;
cout <<"Press the \"Enter\" key to continue";
cin.get();
}
cout << "Continue? (Y/N): ";
cin.get(again);
if(again == '\n')
again = 'Y';
else
cin.ignore(255,'\n'); // read past CR
putchar('\n');
}
while(again == 'Y' || again == 'y');

puts("Press the \"Enter\" key to continue");
cin.get(); // hold window open
return EXIT_SUCCESS;


}
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
void primeFactor(long wholeNumber)
{
int i=2;
static int count = 0;

if(wholeNumber!=2)
{
do
{
if(wholeNumber%i==0) // if it is divisible it is not prime
{
cout << "(" << i << ")";
count++;
primeFactor(wholeNumber/i);
return;
}
i++;
}
while(i <= sqrt((long double)wholeNumber));
}

if(count ==0)
{
cout << "(" << wholeNumber << ")" << " " << ":Prime!" << endl;
}
else
{
cout << "(" << wholeNumber << ")" << " " << endl;
count=0;
}
}
Okay.. I fixed the "isalpha part" by omitting it and using this code instead


#include <iostream>
#include <cstdlib>
#include <exception>
#include <cmath>
#include <cctype>
using namespace std;

void primeFactor(long wholeNumber);

int main(void)
{
int whole = 0;
char again = 'N';

do
{
system("cls");
do
{
cout << "Please enter a Whole number greater than 1: ";
cin >> whole;
cin.get();
while(!cin)
{
cout << "***Whole number MUST be numeric!***"<<endl << "Please enter a Whole number greater than 1: ";
cin.clear();
cin.ignore(255,'\n');
cin >> whole;
cin.get();
}

if(whole < 1) // cannot be negative number not defined
cerr << "***Whole number cannot < 1!***" << endl;
}while(whole <1);
Yeah, I think isaplha() returns true if it is a #, letter, or a couple of symbols.
closed account (z05DSL3A)
How about:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <iostream>
#include <cstdlib>
#include <exception>
#include <cmath>
#include <cctype>
using namespace std;

void primeFactor(long wholeNumber);

int main(void)
{
	int whole = 0;
	char again = 'N';

	do
	{
		system("cls");

		bool inputIsValid = false;

		while(!inputIsValid)
		{
			cout << "Please enter a Whole number greater than 1: ";

			inputIsValid = ((cin >> whole)&&(whole >= 1));

			if(!inputIsValid)
			{
				cerr << "***Whole number cannot < 1!***" << endl;
			}
			cin.clear();
			cin.ignore();
		}

		try
		{
			cout << whole << " = " << " ";
			primeFactor (whole);
		}
		catch(exception e)
		{
			cout << "Exception:" << e.what() << endl;
			cout <<"Press the \"Enter\" key to continue";
			cin.get();
		}

		cout << "Continue? (Y/N): ";
		cin.get(again);
		if(again == '\n')
			again = 'Y';
		else
			cin.ignore(255,'\n'); // read past CR
		putchar('\n');

	}while(again == 'Y' || again == 'y');

	puts("Press the \"Enter\" key to continue");
	cin.get(); // hold window open
	return EXIT_SUCCESS;
}
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
void primeFactor(long wholeNumber)
{
	int i=2;
	static int count = 0;

	if(wholeNumber!=2)
	{
		do
		{
			if(wholeNumber%i==0) // if it is divisible it is not prime
			{
				cout << "(" << i << ")";
				count++;
				primeFactor(wholeNumber/i);
				return;
			}
			i++;
		}while(i <= sqrt((long double)wholeNumber));
	}

	if(count ==0) 
	{
		cout << "(" << wholeNumber << ")" << " " << ":Prime!" << endl;
	}
	else
	{
		cout << "(" << wholeNumber << ")" << " " << endl;
		count=0;
	}
}
Topic archived. No new replies allowed.