Avoid PROGRAM CRASH

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2006
Posts: 5
Reputation: MrBrilliant is an unknown quantity at this point 
Solved Threads: 0
MrBrilliant MrBrilliant is offline Offline
Newbie Poster

Avoid PROGRAM CRASH

 
0
  #1
Mar 2nd, 2006
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
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 466
Reputation: winbatch is on a distinguished road 
Solved Threads: 18
winbatch's Avatar
winbatch winbatch is offline Offline
Posting Pro in Training

Re: Avoid PROGRAM CRASH

 
0
  #2
Mar 2nd, 2006
You're gonna have to give us a bit more than that - how about some source code?
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 9
Reputation: ludesign is an unknown quantity at this point 
Solved Threads: 0
ludesign's Avatar
ludesign ludesign is offline Offline
Newbie Poster

Re: Avoid PROGRAM CRASH

 
0
  #3
Mar 3rd, 2006
hi,

this sounds like buffer overflow

as winbatch said 'give us your source code'.
Винаги Ñ?е цели в луната, така и да пропуÑ?неш, пак ще Ñ?и Ñ?ред звездите!
by ludesign
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Avoid PROGRAM CRASH

 
0
  #4
Mar 3rd, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 5
Reputation: MrBrilliant is an unknown quantity at this point 
Solved Threads: 0
MrBrilliant MrBrilliant is offline Offline
Newbie Poster

Re: Avoid PROGRAM CRASH

 
0
  #5
Mar 3rd, 2006
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!!!
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,850
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 754
Team Colleague
Narue's Avatar
Narue Narue is online now Online
Senior Bitch

Re: Avoid PROGRAM CRASH

 
0
  #6
Mar 3rd, 2006
If cin fails to read the type it expects, it will enter an error state and won't accept further input. Try this:
  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.
New members chased away this month: 4
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 5
Reputation: MrBrilliant is an unknown quantity at this point 
Solved Threads: 0
MrBrilliant MrBrilliant is offline Offline
Newbie Poster

Re: Avoid PROGRAM CRASH

 
0
  #7
Mar 3rd, 2006
So if let's say I initialize n to 0 or 11. Will that be ok?
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,850
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 754
Team Colleague
Narue's Avatar
Narue Narue is online now Online
Senior Bitch

Re: Avoid PROGRAM CRASH

 
0
  #8
Mar 3rd, 2006
As long as it's something outside of your valid range, it doesn't matter what value you choose.
New members chased away this month: 4
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 5
Reputation: MrBrilliant is an unknown quantity at this point 
Solved Threads: 0
MrBrilliant MrBrilliant is offline Offline
Newbie Poster

Re: Avoid PROGRAM CRASH

 
0
  #9
Mar 3rd, 2006
:eek: I didn't work!!!
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,850
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 754
Team Colleague
Narue's Avatar
Narue Narue is online now Online
Senior Bitch

Re: Avoid PROGRAM CRASH

 
0
  #10
Mar 3rd, 2006
>I didn't work!!!
Not helpful. Post the code that doesn't work and explain how it doesn't do what you want.
New members chased away this month: 4
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum


Views: 2253 | Replies: 10
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC