467,923 Members | 1,525 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,923 developers. It's quick & easy.

Problem using sscanf fgets and overflow checking

Hi,
I have trouble using sscanf and fgets to check for overflow.
I will post the assignment specification so I could help whoever would kindly like to offer his/her help.

__________________________________________________ __________________________________
1) The program should expect 2 decimal integers per line. Let's call these n1 and n2.
2) For each such input line it should output the value of n1 + 2 * n2 (followed by a newline character '\n').
3) If there are not 2 integers on the line or there is something other than exactly 2 integers other than whitespace, the corresponding output should be the line "illegal input".

4) If the input is legal but the entered n1 or n2 or the calculated output value would be greater than the maximum long int value, then the corresponding output should be "overflow".
5) If it would be less than the minimum (i.e. most negative) long int value, then the corresponding output should be "underflow".

ē You can assume that the input line does not contain the null char '\0';
ē You can also assume that no legal input line is longer than 50 characters (not counting the '\n' character). If such a line is encountered, the output should be "illegal input".

RESTRICTION: n1 and n2 should be declared to be "long int". Do not try to use a "larger type" to detect overflow/underflow. Instead, use the return values, etc. to detect overflow/underflow.

You are expected to use appropriate functions in your assignments. For example, reading the input one character at a time here is not appropriate.
__________________________________________________ __________________________________

Now to my question. I have trouble checking if the input line is longer than 50 characters using fgets and sscanf.
Same with overflow. How do I check if n1 or n2 is overflow/underflow? As for the calculated result I think I could manage that.
I guess I would check if n1 > LONG_MAX - n2.

I think I will be able to do the program by reading character by character but the point of this assignment is to use tools such as sscanf fgets strlen and so on...

I have this assignment due in 2 days and I will appreciate any comments, hints, help before then?

Thank you very much and I will wait for your replies.
Feb 18 '08 #1
  • viewed: 2654
Share:
2 Replies
Expert 512MB
To test over/underflow, think of the number line as being connected at LONG_MIN and LONG_MAX so it's a circle. Thus, anything that gets too big for LONG_MAX loops around to somewhere in the negatives and anything that gets too small for LONG_MIN loops around to somewhere in the positives. You only need to worry about it when n1 and n2 have the same sign before any computations, since otherwise they'll just move towards zero rather than out to _MIN or _MAX.
Feb 18 '08 #2
weaknessforcats
Expert Mod 8TB
You check for overflow if your value won't fit in the variable.

So, subtract the variable from LONG_MAX and if the result is less than your value, overflow will occur.
Feb 18 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

21 posts views Thread by clusardi2k | last post: by
2 posts views Thread by Gandu | last post: by
8 posts views Thread by Les Coover | last post: by
4 posts views Thread by Ivan Lam | last post: by
5 posts views Thread by learner | last post: by
10 posts views Thread by broeisi | last post: by
16 posts views Thread by Chad | last post: by
4 posts views Thread by utab | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.