determine if input pos or neg ?

This is a discussion on determine if input pos or neg ? within the C Programming forums, part of the General Programming Boards category; Hi all ! I have two questions really. (1). I need to determine if a number resulting from scanf is ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    23

    Smile determine if input pos or neg ?

    Hi all !
    I have two questions really.
    (1). I need to determine if a number resulting from scanf is neg or pos. If neg it will do one thing and if pos another but i don't know the protocol for " if ( something = '-') since the number could be anything. I have no problem for the obvious reasons if it's just a pos number.

    (2) I also wonder wether to declare my variables int in this case.

    ....................Thanks greatly!

  2. #2
    Microsoft. Who? MethodMan's Avatar
    Join Date
    Mar 2002
    Posts
    1,198
    It would be better not to check for the sign in front of the number, but to see if(num >= 0).

    Code:
    #include <stdio.h>
    
    main()
    {
    	int num = 0;
    	printf("Please enter a number\n");
    	scanf("%d", &num);
    	if(num >= 0)
    	printf("Positive\n");
    	else
    	printf("Negative");
    
    	return(0);
    }
    -MethodMan-

    Your Move:Life is a game, Play it; Life is a challenge, Meet it; Life is an opportunity, capture it.

    Homepage: http://www.freewebs.com/andy_moog/home.html

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    23

    Smile

    Hey thanks!
    I should have thought of that !!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Input class project (again)
    By Elysia in forum C++ Programming
    Replies: 41
    Last Post: 02-13-2009, 09:52 AM
  2. I would love some input on my BST tree.
    By StevenGarcia in forum C++ Programming
    Replies: 4
    Last Post: 01-15-2007, 12:22 AM
  3. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  4. Structure and Linked List User Input Question
    By kevndale79 in forum C Programming
    Replies: 16
    Last Post: 10-05-2006, 11:09 AM
  5. need help with some input
    By blindleaf in forum C Programming
    Replies: 2
    Last Post: 03-16-2003, 12:50 PM

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