473,387 Members | 3,801 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

function that catches errors

19
Hi here is my problem im adding to a program to catch errors.The program below catches errors like integers less than 0 or not equal to 1 but if I put in a number like 223e it doesn't catch the e as an error I know I am missing something in my error = sscanf() line but cant figure it out please help.

Expand|Select|Wrap|Line Numbers
  1. char line[MAXLINE];
  2. int error, n;
  3.  
  4. do{
  5.     printf("Input a positive integer: ");
  6.     fgets(line, MAXLINE, stdin);
  7.     error = sscanf(line, "%d", &n) != 1 || n<= 0;
  8.     if (error)
  9.         printf("\nError: Do it again. \n");
  10. } while (error);
  11.  
  12.  
Sep 24 '08 #1
2 1237
donbock
2,426 Expert 2GB
sscanf parses the input string until it reaches the end of the format string or the end of the input string. In your example you cite, the format string ends first. You want to verify that sscanf reached the end of the input string. Take a look at the %n conversion specifier.

Does your program properly handle leading and trailing whitespace?
Sep 24 '08 #2
jmf777
19
the program does handle whitespace correctly.
Sep 24 '08 #3

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

Similar topics

4
by: Agnes | last post by:
I create an object named Number , with the function numtowords. i input '100' as parameter, It will return "one hundred". However, any simple way to test this object ???? I only know the old way ...
41
by: Alexei A. Frounze | last post by:
Seems like, to make sure that a pointer doesn't point to an object/function, NULL (or simply 0) is good enough for both kind of pointers, data pointers and function pointers as per 6.3.2.3: 3 An...
1
by: Tim | last post by:
Dear All, I have discovered, to my dismay, that the ShowDialog function will not function correctly when used with the AnimateWindow API. The owned form is opened behind the owning form on the...
4
by: Agnes | last post by:
I create an object named Number , with the function numtowords. i input '100' as parameter, It will return "one hundred". However, any simple way to test this object ???? I only know the old way ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.