Thread: input problem

  1. #1
    Registered User joybanerjee39's Avatar
    Join Date
    Oct 2011
    Location
    kolkata
    Posts
    106

    input problem

    i have declared a int variable i.
    but my program is not running if user enters a value of i more than 32766.
    i have turbo c/c++ compiler.
    please help.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Change to a compiler that has a greater range for int, or declare a long variable i instead
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    The best solution would be to upgrade your compiler. Pelles C is a free, popular 32/64 bit C compiler. Turbo C is an old 16 bit compiler that shouldn't be used anymore. Even if you have to use Turbo C for school, you should still download Pelles C and use it whenever possible, so you can get some experience with a modern compiler. Since Turbo C is 6 bit, int types only go from -32768 to +32767. You could try using a bigger data type like long int or long long int. You may need to modify your input function (scanf?) if you do that.

  4. #4
    Registered User joybanerjee39's Avatar
    Join Date
    Oct 2011
    Location
    kolkata
    Posts
    106
    Quote Originally Posted by anduril462 View Post
    The best solution would be to upgrade your compiler. Pelles C is a free, popular 32/64 bit C compiler. Turbo C is an old 16 bit compiler that shouldn't be used anymore. Even if you have to use Turbo C for school, you should still download Pelles C and use it whenever possible, so you can get some experience with a modern compiler. Since Turbo C is 6 bit, int types only go from -32768 to +32767. You could try using a bigger data type like long int or long long int. You may need to modify your input function (scanf?) if you do that.
    thnx. for ur reply
    i used long long int i. but it still doesn't work. u have said something about changing scanf. could u please explain it!

  5. #5
    Registered User joybanerjee39's Avatar
    Join Date
    Oct 2011
    Location
    kolkata
    Posts
    106
    Quote Originally Posted by laserlight View Post
    Change to a compiler that has a greater range for int, or declare a long variable i instead
    thnx

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by joybanerjee39 View Post
    thnx. for ur reply
    i used long long int i. but it still doesn't work. u have said something about changing scanf. could u please explain it!
    Look up the documentation for scanf, it should tell you how to change your format string to work with long ints. Note, you can try this site, but it might not be correct for Turbo C.

  7. #7
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    Quote Originally Posted by joybanerjee39 View Post
    i have declared a int variable i.
    I think you have defined a variable.

    Code:
    #include <stdio.h>
    #include <limits.h>
    
    #ifndef __STDC__
    #error i should use an newer C compiler
    #endif
    
    int i;
    long l;
    printf("i can use int values up to %d",INT_MAX);
    printf("i can use long values up to %ld",LONG_MAX);

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by BillyTKid
    I think you have defined a variable.
    The definition of a variable is also a declaration of the variable.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    Yes, but not vice versa, what i said.
    Any program only with declarations cannot work, only with definitions it works/compiles.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by BillyTKid
    Yes, but not vice versa, what i said.
    Which is why joybanerjee39's statement is factually correct

    I would have expected a linker error instead of a runtime error if the declaration had not been a definition.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Input Problem
    By slapgun87 in forum C Programming
    Replies: 4
    Last Post: 07-26-2010, 10:40 AM
  2. geting input in C, problem in getting input
    By BujarM in forum C Programming
    Replies: 3
    Last Post: 04-17-2009, 09:38 PM
  3. problem with CSV input
    By cmiller4 in forum C Programming
    Replies: 12
    Last Post: 02-05-2009, 03:44 PM
  4. Problem with input
    By devilsknight in forum C Programming
    Replies: 6
    Last Post: 08-15-2006, 02:09 PM
  5. getting input problem
    By Unregistered in forum C Programming
    Replies: 8
    Last Post: 05-27-2002, 11:05 AM