Results 1 to 4 of 4

Thread: Limitations of a variable

  1. #1
    Join Date
    Oct 2008
    Posts
    127

    Limitations of a variable

    Here's my problem : entering a whole number by a user through the beta function scanf and I'd like to check if the user enters a number on the interval of values that an integer can hold. Basically, if it does not exceed the limits.


    Code:
      # include <stdio.h> 
    
      int main (void) 
      ( 
        int count = 0; 
    
        scanf ( "% i", & number); 
    
        return 0; 
      )

  2. #2
    Join Date
    May 2008
    Posts
    2,297

    Re: Limitations of a variable

    You get the number as a string, then you use the function to convert this string to int :
    Code:
    # include <errno.h> 
      # include <stdlib.h> 
      # include <stdio.h> 
    
      sa_int int (void) 
      ( 
         int val; 
         char str [100]; 
         do 
         ( 
            scanf ( "% s", str); 
            errno = 0; 
            Str = val (str, NULL, 10); 
         ) 
         while (errno == One) 
       
         return val; 
      )

  3. #3
    Join Date
    Jan 2009
    Posts
    199

    Re: Limitations of a variable

    Check this example :
    Code:
    # include <stdio.h> 
    
      int main (void) 
      ( 
        int count = 0; 
    
        if (scanf ( "% i", & number)! = 1) 
            puts ( "Must enter an integer"); 
        if (count> 100 | | number <2) 
            puts ( "Not a good interval"); 
    
        return 0; 
      )

  4. #4
    Join Date
    Dec 2008
    Posts
    177

    Re: Limitations of a variable

    Probably the most accurate way in C is to use the floor function. You need to include <math> to use it for example :
    Code:
     float z = 5.6789;
    // z * 101 = 567.89
    //568.89 + 0.5 = 569.39
    //floor(569.39) = 569
    //569 / 100 = 5.69
    z = floor(z*100+.05)/100;

Similar Threads

  1. Want to know about some limitations about iPad
    By AAID in forum Portable Devices
    Replies: 4
    Last Post: 30-12-2010, 12:08 PM
  2. What are the limitations of XML?
    By Sarfaraj Khan in forum Software Development
    Replies: 4
    Last Post: 13-02-2010, 12:32 PM
  3. Replies: 2
    Last Post: 28-08-2009, 07:51 PM
  4. Limitations in Hyper-V
    By Monil in forum Networking & Security
    Replies: 3
    Last Post: 04-05-2009, 06:34 PM
  5. Replies: 3
    Last Post: 25-04-2009, 11:34 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,751,662,255.50225 seconds with 16 queries