472,986 Members | 2,788 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,986 software developers and data experts.

Problem using sscanf fgets and overflow checking

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
2 3021
849 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
9,208 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

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

Similar topics

by: clusardi2k | last post by:
/* The below code on SGI will wait for you to enter 2 things, but on Linux it will only wait the first time. I can make the code work by replacing the scanf with: char data ; fgets...
by: Gandu | last post by:
Could a C guru please help me? Suppose I have an ASCII text file. Now, I want to read in data from this file, line by line, and parse each line. What would be a proper way to determine what the...
by: Les Coover | last post by:
Consider this source code /* str_test */ #include <stdio.h> /* standard header */ int main(void) { int id, number; char in_string= "2 blue 6";
by: Ivan Lam | last post by:
Hi All, Thanks for reading my post! I have a problem that using the scanf function. I would like to scan a value from a line like: file:c:\program files\mpd\mpd.exe however, when I read...
by: learner | last post by:
I have datafiles like this: 0 1941 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.02 0.00 0.00 1 0 1941 0.00 0.03 0.00 0.03 0.04 0.02 0.00 0.00 0.00 0.00 2 0 1941 0.00 0.00 0.00 0.00 0.52...
by: broeisi | last post by:
What advantages does sscanf offer over scanf? I had the following code: #include <stdio.h> #include <string.h> int main(void) { int start, finish, values;
by: allpervasive | last post by:
hi all, this is reddy, a beginner to c lang,,here i have some problems in reading and modifying the contents of a file,, hope you can help to solve this problem. Here i attach the file to be...
by: Chad | last post by:
Given the following #include <stdio.h> int main(void) { char line; long arg1, arg2; while(fgets(line, BUFSIZ, stdin) != NULL){
by: utab | last post by:
Dear all, I have to interface some C code in C++, but I had a problem with sscanf function, it has been some time I have not used C and I could not figure out my problem. Simple code is below, I...
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.