954,487 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Avoid PROGRAM CRASH

my program prompt an integer input but if the user type in a character the program crash!

is there any solution to the problem

MANY THANX

MrBrilliant
Newbie Poster
5 posts since Mar 2006
Reputation Points: 10
Solved Threads: 0
 

You're gonna have to give us a bit more than that - how about some source code?

winbatch
Posting Pro in Training
466 posts since Feb 2005
Reputation Points: 68
Solved Threads: 18
 

hi,

this sounds like buffer overflow :)

as winbatch said 'give us your source code'.

ludesign
Newbie Poster
9 posts since Feb 2006
Reputation Points: 10
Solved Threads: 0
 

fgets() to read a line of input.
strtol() to validate, convert and check for numeric overflow.

Both provide some success/fail indication in their status returns.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

Absolutely right! Here the source code:

#include
#include

main()
{
int n; /* number of values entered */
cout << "How many values? (1..10) -> ";
cin >> n;
cin.ignore(SHRT_MAX, '\n');

/* control number of value entered by the user */
while (n<1||n>10)
{
cout << "\nInvalid input. Try again (1..10) -> ";
cin >> n;
cin.ignore(SHRT_MAX, '\n');
}
}

Thus so far I have got the input will be within the range 1 to 10 and if the user will type in any value with a fraction part the program cut it off.

But if the user type in a letter the program crash!!!

MrBrilliant
Newbie Poster
5 posts since Mar 2006
Reputation Points: 10
Solved Threads: 0
 

If cin fails to read the type it expects, it will enter an error state and won't accept further input. Try this:

while (n<1||n>10)
{
  cout << "\nInvalid input. Try again (1..10) -> ";
  cin.clear();
  cin >> n;
  cin.ignore(SHRT_MAX, '\n');
}

Also, you need to initialize n to something. If the first input fails, n will remain indeterminate, and that could cause a number of problems.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

So if let's say I initialize n to 0 or 11. Will that be ok?

MrBrilliant
Newbie Poster
5 posts since Mar 2006
Reputation Points: 10
Solved Threads: 0
 

As long as it's something outside of your valid range, it doesn't matter what value you choose.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

:eek: I didn't work!!!

MrBrilliant
Newbie Poster
5 posts since Mar 2006
Reputation Points: 10
Solved Threads: 0
 

>I didn't work!!!
Not helpful. Post the code that doesn't work and explain how it doesn't do what you want.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

That's what I have done:

#include <iostream.h>
#include <limits.h>

main()
{
	int n; /* number of values entered */

	n=0;
	cout << "How many values? (1..10) -> ";
	cin.clear();
	cin >> n;
	cin.ignore(SHRT_MAX, '\n');

	/* control number of value entered by the user */

	while (n<1||n>10)
	{
  	cout << "\nInvalid input. Try again (1..10) -> ";
	cin.clear();
  	cin >> n;
	cin.ignore(SHRT_MAX, '\n');
}


:rolleyes: I thought I had duplicate cin.clear(); as the program could ever get a valid input and thus it will not execute the while

Hope this is more helpful!

MrBrilliant
Newbie Poster
5 posts since Mar 2006
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You