Help with program crashes.

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 1580 Views - Last Post: 26 May 2009 - 10:23 PM Rate Topic: -----

#1 Realest07   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 26-May 09

Help with program crashes.

Post icon  Posted 26 May 2009 - 06:14 PM

I'm trying to learn C++ by reading books and doing random coding projects. I stumbled across a website with a math function called Hailstone Sequences and I thought that would be an easy project to practice coding on (http://plus.maths.org/issue1/xfile/index.html is the site). I finished coding the program so it does what it's supposed to but I would like to make it more error proof, as in the user can't make the program crash so easily.

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.

Is This A Good Question/Topic? 0
  • +

Replies To: Help with program crashes.

#2 r.stiltskin   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2034
  • View blog
  • Posts: 5,436
  • Joined: 27-December 05

Re: Help with program crashes.

Posted 26 May 2009 - 07:29 PM

View PostRealest07, on 26 May, 2009 - 08:14 PM, said:

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.


So, have you tried to write any of that code? Everything you mentioned is pretty basic. Which part of that is giving you trouble?
Was This Post Helpful? 0
  • +
  • -

#3 Realest07   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 26-May 09

Re: Help with program crashes.

Posted 26 May 2009 - 07:40 PM

I wrote all of that. The problem I'm having is when I compile it and enter the wrong info like "x" for the number, the program crashes. I want incorrect answers such as that to return a message.

Example:

if(input does not match request specifications){cout << "Invalid answer";}
Was This Post Helpful? 0
  • +
  • -

#4 r.stiltskin   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2034
  • View blog
  • Posts: 5,436
  • Joined: 27-December 05

Re: Help with program crashes.

Posted 26 May 2009 - 07:53 PM

View PostRealest07, on 26 May, 2009 - 09:40 PM, said:

Example:

if(input does not match request specifications){cout << "Invalid answer";}


Right. So how have you tried to implement that? What's the problem?
Was This Post Helpful? 0
  • +
  • -

#5 Realest07   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 26-May 09

Re: Help with program crashes.

Posted 26 May 2009 - 07:57 PM

Quote

Right. So how have you tried to implement that? What's the problem?


I don't know what code to use to implement that. What would the syntax be that checks if int N is a variable or not? Also, if I made the Answer variable take char's instead of int's, what do I do to make sure the user inputs "Y" or "N" and not something else?
Was This Post Helpful? 0
  • +
  • -

#6 apw5020   User is offline

  • D.I.C Addict

Reputation: 78
  • View blog
  • Posts: 666
  • Joined: 26-March 09

Re: Help with program crashes.

Posted 26 May 2009 - 07:58 PM

Just add this after your cin statement for N:
while(N > 0)
{
cout << "Invalid entry.  Please enter a positive number.";
cin >> N;
cout << endl;
}



and add this after your cin statements for Answer:
while(Answer != 1 && Answer != 2)
{
cout << "Invalid choice.  Please enter 1 for yes or 2 for no.";
cin >> Answer;
cout << endl;
}



You were on the right track in your previous post. Make sense?


Another way to handle exceptions/errors can be found here http://www.dreaminco...wtopic45887.htm.

This post has been edited by apw5020: 26 May 2009 - 08:06 PM

Was This Post Helpful? 0
  • +
  • -

#7 Realest07   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 26-May 09

Re: Help with program crashes.

Posted 26 May 2009 - 08:05 PM

View Postapw5020, on 26 May, 2009 - 06:58 PM, said:

Just add this after your cin statement for N:
while(N > 0)
{
cout << "Invalid entry.  Please enter a positive number.";
cin >> N;
cout << endl;
}



and then add these lines after your cin statements for Answer:
while(Answer != 1 && Answer != 2)
{
cout << "Invalid choice.  Please enter 1 for yes or 2 for no.";
cin >> Answer;
cout << endl;
}



You were on the right track in your previous post. Make sense?


Yea, but for the Answer I wanted it to take a char and not a int. And for the first one you posted:

while(N > 0)
{
cout << "Invalid entry.  Please enter a positive number.";
cin >> N;
cout << endl;
}



How does that make sure the user doesn't enter a random letter for 'N'?
Was This Post Helpful? 0
  • +
  • -

#8 apw5020   User is offline

  • D.I.C Addict

Reputation: 78
  • View blog
  • Posts: 666
  • Joined: 26-March 09

Re: Help with program crashes.

Posted 26 May 2009 - 08:10 PM

If you want to accept chars instead of numbers for Answer, you could just change it to
while(Answer != 'Y' && Answer != 'y' && Answer != 'N' && Answer != 'n')
{
// stuff
}


assuming the user inputs Y or y for yes and N or n for no.

This post has been edited by apw5020: 26 May 2009 - 08:13 PM

Was This Post Helpful? 0
  • +
  • -

#9 Realest07   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 26-May 09

Re: Help with program crashes.

Posted 26 May 2009 - 08:21 PM

View Postapw5020, on 26 May, 2009 - 07:10 PM, said:

If you want to accept chars instead of numbers for Answer, you could just change it to
while(Answer != 'Y' && Answer != 'y' && Answer != 'N' && Answer != 'n')
{
// stuff
}


assuming the user inputs Y or y for yes and N or n for no.



Thanks, but what about making sure that N only takes integers?
Was This Post Helpful? 0
  • +
  • -

#10 apw5020   User is offline

  • D.I.C Addict

Reputation: 78
  • View blog
  • Posts: 666
  • Joined: 26-March 09

Re: Help with program crashes.

Posted 26 May 2009 - 08:31 PM

Here, do this:
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;
	while(!cin.good())
	{
		cin.clear(); //clear the error flags
		cin.sync(); //flush the input buffer
		cout << "Invalid entry.  Please enter a positive number." << endl;
		cin >> N;
		cout << endl;
	}
	return N;
}



I found the answer here http://www.codingfor...p/t-127998.html. Hope that helps.

This post has been edited by apw5020: 26 May 2009 - 08:38 PM

Was This Post Helpful? 1
  • +
  • -

#11 Realest07   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 26-May 09

Re: Help with program crashes.

Posted 26 May 2009 - 08:38 PM

View Postapw5020, on 26 May, 2009 - 07:31 PM, said:

Here, do this:
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;
	while(!cin.good() && N < 0)
	{
		cin.clear(); //clear the error flags
		cin.sync(); //flush the input buffer
		cout << "Invalid entry.  Please enter a positive number." << endl;
		cin >> N;
		cout << endl;
	}
	return N;
}



I found the answer here http://www.codingfor...p/t-127998.html. Hope that helps.


Yup, that worked perfectly. Thanks for you're help.
Was This Post Helpful? 0
  • +
  • -

#12 r.stiltskin   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2034
  • View blog
  • Posts: 5,436
  • Joined: 27-December 05

Re: Help with program crashes.

Posted 26 May 2009 - 09:04 PM

If N is an int, cin >> N; will ONLY extract an integer. So you can use something like this:
if( cin >> N ) {
  // do something
}
else {
  // do something else
}

But if your user is nasty, he might enter 5.6, in which case cin will take the 5, and leave the .6 in the stream to cause trouble later. If you want to guard against that case you have to try something else. You might, for example, have your program read the user's input into a char buffer and then examine the input character by character to see if it contains anything besides digits, and if so, reject it.

It all depends on how robust you want the code to be, and how much work you are willing to do to make it so.
Was This Post Helpful? 0
  • +
  • -

#13 Realest07   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 26-May 09

Re: Help with program crashes.

Posted 26 May 2009 - 09:37 PM

Hmm. Thanks but for now I won't worry about decimal input. However, I'm mostly finished except now I've run into a strange problem.
When the program asks the user "Yes or No," no matter what response is given the (either 'Y' or 'N', anything else is now returning my error message thankfully) program exits. This is the code I currently have for that part:

while (Cont = true)
{

	   // other stuff
	   // ...

		cout << "Try Again?" << "\n" << "Y or N?\t";
		char Answer;
		cin >> Answer;
		while(Answer != 'Y' && Answer != 'y' && Answer != 'N' && Answer != 'n')
		{
				cout << "Invalid entry, please re-enter: ";
				cin >> Answer;
		}
		if (Answer == 'N' || 'n')
		{
			Cont = false;
		}




Any ideas on how to fix that or what the problem is?
Was This Post Helpful? 0
  • +
  • -

#14 apw5020   User is offline

  • D.I.C Addict

Reputation: 78
  • View blog
  • Posts: 666
  • Joined: 26-March 09

Re: Help with program crashes.

Posted 26 May 2009 - 10:05 PM

This line of code is incorrect:
if (Answer == 'N' || 'n')


Instead, it needs to be:
if(Answer == 'N' || Answer == 'n')



That should do it.
Was This Post Helpful? 0
  • +
  • -

#15 Realest07   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 26-May 09

Re: Help with program crashes.

Posted 26 May 2009 - 10:07 PM

View Postapw5020, on 26 May, 2009 - 09:05 PM, said:

This line of code is incorrect:
if (Answer == 'N' || 'n')


Instead, it needs to be:
if(Answer == 'N' || Answer == 'n')



That should do it.


That did it. Thanks again. Any reason why it doesn't work the way I had it first?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2