Welcome, Guest. Please login or register.

Author Topic: C/C++ programming  (Read 4192 times)

Description:

0 Members and 1 Guest are viewing this topic.

Offline Cymric

  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 1031
    • Show all replies
Re: C/C++ programming
« on: November 08, 2005, 02:31:11 PM »
The easiest way is by making use of sscanf(). Assuming you have the user input in an array of char buf with its starting tabs, spaces and letter symbols removed, you can simply call on

retval=sscanf(buf, "%d", &intval);

If retval equals 1, you have bagged an integer, the value of which is in intval. The downside is that it breaks down when faced with input which exceed the capacity of int, and ignores any remaining text in the input line after a non-number symbol was parsed. Also, sscanf() will not tell you what the user input was when it returns 0 indicating no match was made. In this case, your best bet is to simply put out a 0x07-character (\b if memory serves me) for the terminal beep, warn the user with a text, and redo the input.
Some people say that cats are sneaky, evil and cruel. True, and they have many other fine qualities as well.