Thread: reading integers

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    8

    reading integers

    hey there,

    i have a small question

    i will be reading two integers using scanf..

    how do i check whether i have more than two int or less than two int or not even ints ?

  2. #2
    Registered User
    Join Date
    Feb 2006
    Posts
    8
    I am using :

    ----------------------------------------------------------

    int n1;
    int n2;

    printf("Enter two non-negative numbers :");
    scanf("%d%d" , &n1, &n2);

    ----------------------------------------------------------

    but i want to display an error message if I have more than 2 ints inputted / less than 2 ints inputted / something other than ints inputted .

  3. #3
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    scanf cant perform good validation, check this out:

    its mostly code, so if you want more info, try a board search on
    validation

    also, using separate calls to scanf will improve reading in the
    correct amount of numbers
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    8
    another thing.....
    if the input is legal but the entered n1 or n2 or the calculated output value is out of the range of an unsigned long int value, then i would like to print a corresponding output message but how would i do the calculation ?

  5. #5
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    Here's what i have. if the number exceeds the maximum allowed
    number by the data type, then the value is not stored in the
    variable. there is no way of knowing if this will happen when it
    happens, your variable will have some junk value that it had in
    the first place. So how can you attempt to take into account of
    this? cast your calculation to a larger data type, say double or
    even a long double if thats bigger on your system. find out the
    maximum value that can be stored by your long int, and if
    the value of the calculation (when cast to double) exceeds that
    number, you can print a message. see this quick program:

    Code:
    #include <stdio.h>
    
    #define MAXIMUM 2147483647.0   
    
    int main (void)
    {
    	double x;
    	long int y;
    
    	printf ("Enter a number: ");
    	scanf ("%lf", &x);
    
    	if (x > MAXIMUM)
    		printf ("\nError - Overflow in data type long int ... Exiting\n\n");
    
    	else 
    	{
    		printf ("\nIn range ... Casting value to correct type\n\n");
    		y = (long int)x;
    		printf ("Value is: %d\n\n", y);
    	}
    	
    	return 0;
    }
    also try this link on maximum values for data types
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  6. #6
    Registered User
    Join Date
    Feb 2006
    Location
    Chennai, India
    Posts
    3
    We can read input from scanf function.
    scanf function return the number of input items successfully assigned.
    Code:
    #include<stdio.h>
    main()
    {
    	int x1,x2,result;
    	printf("Enter two integers");
    	result=	scanf("%d %d",&x1,&x2);  //Read two integers from stdinput 
    	printf("Scanf returns %d",result);  //print no of successful integer inputs
    }

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  2. Assignment HELP!!
    By cprogrammer22 in forum C Programming
    Replies: 35
    Last Post: 01-24-2009, 02:24 PM
  3. Fun with reading hex
    By dpro in forum C++ Programming
    Replies: 7
    Last Post: 02-17-2006, 06:41 PM
  4. reading file word by word
    By 98holb in forum C Programming
    Replies: 2
    Last Post: 01-25-2006, 05:49 PM
  5. fread - reading strings and integers.
    By Vber in forum C Programming
    Replies: 1
    Last Post: 11-17-2002, 04:08 PM