TechArena Community

TechArena Community (http://forums.techarena.in/)
-   Software Development (http://forums.techarena.in/software-development/)
-   -   Limitations of a variable (http://forums.techarena.in/software-development/1258616.htm)

Wilbur 15-10-2009 11:08 AM

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;
  )


Zecho 15-10-2009 11:12 AM

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;
  )


Walby 15-10-2009 11:13 AM

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;
  )


Aberto 15-10-2009 11:16 AM

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;



All times are GMT +5.5. The time now is 04:18 PM.