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.