Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 15-10-2009
Member
 
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; 
  )
Reply With Quote
  #2  
Old 15-10-2009
Member
 
Join Date: May 2008
Posts: 2,293
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; 
  )
Reply With Quote
  #3  
Old 15-10-2009
Member
 
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; 
  )
Reply With Quote
  #4  
Old 15-10-2009
Member
 
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;
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Limitations of a variable"
Thread Thread Starter Forum Replies Last Post
Want to know about some limitations about iPad AAID Portable Devices 4 30-12-2010 12:08 PM
What are the limitations of XML? Sarfaraj Khan Software Development 4 13-02-2010 12:32 PM
Runtime Error 91 : Object Variable or with block variable not set Ryan21 Software Development 2 28-08-2009 07:51 PM
Limitations in Hyper-V Monil Networking & Security 3 04-05-2009 06:34 PM
What is Global Variable static? Declaring static variable instead of Global variable Reckon Software Development 3 25-04-2009 11:34 AM


All times are GMT +5.5. The time now is 02:50 PM.