By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,946 Members | 1,600 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,946 IT Pros & Developers. It's quick & easy.

Problem using sscanf fgets and overflow checking

P: 3
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
Share this Question
Share on Google+
2 Replies


Expert 100+
P: 849
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 5K+
P: 9,197
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.