944,155 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2488
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Mar 2nd, 2006
0

Avoid PROGRAM CRASH

Expand Post »
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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
MrBrilliant is offline Offline
5 posts
since Mar 2006
Mar 2nd, 2006
0

Re: Avoid PROGRAM CRASH

You're gonna have to give us a bit more than that - how about some source code?
Reputation Points: 68
Solved Threads: 18
Posting Pro in Training
winbatch is offline Offline
466 posts
since Feb 2005
Mar 3rd, 2006
0

Re: Avoid PROGRAM CRASH

hi,

this sounds like buffer overflow

as winbatch said 'give us your source code'.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ludesign is offline Offline
9 posts
since Feb 2006
Mar 3rd, 2006
0

Re: Avoid PROGRAM CRASH

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.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Mar 3rd, 2006
0

Re: Avoid PROGRAM CRASH

Absolutely right! Here the source code:

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

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!!!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
MrBrilliant is offline Offline
5 posts
since Mar 2006
Mar 3rd, 2006
0

Re: Avoid PROGRAM CRASH

If cin fails to read the type it expects, it will enter an error state and won't accept further input. Try this:
C++ Syntax (Toggle Plain Text)
  1. while (n<1||n>10)
  2. {
  3. cout << "\nInvalid input. Try again (1..10) -> ";
  4. cin.clear();
  5. cin >> n;
  6. cin.ignore(SHRT_MAX, '\n');
  7. }
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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Mar 3rd, 2006
0

Re: Avoid PROGRAM CRASH

So if let's say I initialize n to 0 or 11. Will that be ok?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
MrBrilliant is offline Offline
5 posts
since Mar 2006
Mar 3rd, 2006
0

Re: Avoid PROGRAM CRASH

As long as it's something outside of your valid range, it doesn't matter what value you choose.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Mar 3rd, 2006
0

Re: Avoid PROGRAM CRASH

:eek: I didn't work!!!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
MrBrilliant is offline Offline
5 posts
since Mar 2006
Mar 3rd, 2006
0

Re: Avoid PROGRAM CRASH

>I didn't work!!!
Not helpful. Post the code that doesn't work and explain how it doesn't do what you want.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Why program works in Dev-C++ and not in VC++ 2005?
Next Thread in C++ Forum Timeline: Question





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC