Get a positive/negative input from user

This is a discussion on Get a positive/negative input from user within the C Programming forums, part of the General Programming Boards category; Code: scanf("%d",&num); I can use this to get positive numbers from the user easily. But say I also want the ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    27

    Get a positive/negative input from user

    Code:
    scanf("%d",&num);
    I can use this to get positive numbers from the user easily.

    But say I also want the user to enter negative numbers, like "-10."

    Obviously I would have to check and see if the first character is a "-" or not, then get whatever the integer after the "-" is.

    I'm not sure whether scanf can be used to scan characters and integers at the same time. Is it possible, or should I use a different function?

  2. #2
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    scanf can handle negative numbers. Don't be afraid to experiment.
    Quote Originally Posted by The Jargon File
    Microsoft Windows - A thirty-two bit extension and graphical shell to a sixteen-bit patch to an eight-bit operating system originally coded for a four-bit microprocessor which was written by a two-bit company that can't stand one bit of competition.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    27
    Wow. I got it, haha. Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. convert negative float to positive ???
    By manny in forum C Programming
    Replies: 4
    Last Post: 07-02-2009, 04:37 AM
  2. Changing from positive to negative numbers
    By vopo in forum C++ Programming
    Replies: 19
    Last Post: 09-10-2008, 03:21 PM
  3. Using TextOut() to align positive & negative numbers
    By csonx_p in forum Windows Programming
    Replies: 4
    Last Post: 05-27-2008, 08:12 AM
  4. Negative Float Into Positive
    By Cero.Uno in forum C Programming
    Replies: 2
    Last Post: 02-17-2008, 03:13 AM
  5. find the totals of positive and negative integers
    By noaksey in forum C Programming
    Replies: 5
    Last Post: 05-11-2004, 02:59 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21