Here is the code so far:
//Hailstone Sequences
#include <iostream>
using namespace std;
unsigned short EnterNumber();
unsigned long IfOdd(unsigned long);
unsigned long IfEven(unsigned long);
int main()
{
bool Cont = true;
unsigned long N;
cout << "This program prints out the 'Hailstone Sequences'" << endl << endl;
while (Cont == true)
{
int exit = 100;
N = EnterNumber();
while(exit > 0)
{
if (N%2 == 0)
{
N = IfEven(N);
exit--;
}
else
{
if (N == 1)
{
exit = 3;
}
N = IfOdd(N);
exit--;
}
}
cout << "\n\n";
cout << "Try Again?" << "\n" << "(1)Y or (2)N?\t";
int Answer;
cin >> Answer;
// ask user if they would like to try the program again
int x = 1;
do
{
if (Answer == 1)
{
Cont = true;
x = 0;
}
else if (Answer == 2)
{
Cont = false;
x = 0;
}
else
{
cout << "Invalid entry, please re-enter: ";
cin >> Answer;
cout << "\n\n";
}
} while (x == 1);
}
return 0;
}
// Gets Number from user.
unsigned short EnterNumber()
{
unsigned long int N;
cout << "Please enter a positive number\n(Note: Anything else will cause an error):\n";
cin >> N;
cout << endl;
return N;
}
// Returns new N if N is odd.
unsigned long IfOdd(unsigned long N)
{
cout << N << " ";
N = (N * 3) + 1;
return N;
}
// Returns new N if N is even.
unsigned long IfEven(unsigned long N)
{
cout << N << " ";
N = (N/2);
return N;
}
What I want to be able to do from here is make it so if the user enters a negative number or a character or anything that is not a positive whole integer the program returns an error and asks the user to re-enter a number. Also for the part where the program asks the user to repeat, I want to make that ask for "Y or N" without having to use "1 or 2" and return an error if the user enters anything otherwise. Please help. Thanks.

New Topic/Question
Reply


MultiQuote




|