Thread: Maximum integer length output?

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    42

    Maximum integer length output?

    Hi,

    Below is my program, what it does is asks the user to input both an integer and floating point value. I managed to be able to define the output of the floating point number with the %.2f, but i cannot find a way to output the integer with a maximum of 2 spaces, ie.any number between 0 and 99. Any help at all would be great. Thank you in advance.

    Ryan
    Code:
    #include<stdio.h>
    
    int main(void)
    {
    	int integer;
    	float floatingPoint;
    
    	printf("Please input an integer value: ");
    	scanf("%i", &integer);
    
    	printf("Please input a floating point value: ");
    	scanf("%f", &floatingPoint);
    
    	printf("The integer value you entered was: %2g%i \n", integer);
    	printf("The floating point value you entered was: %.2f \n", floatingPoint);
    }
    Added code tags
    Read this ->
    << !! Posting Code? Read this First !! >>

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You can't do that - an integer isn't made up of parts like a floating point number is. It's ONE integral unit.

    The way to do what you want, is to make sure you get only 0-99, when the user is putting in the number. Like in the old western movies - "head 'em off at the pass".

    One way to do it:

    Code:
    #include<stdio.h>
    
    int main(void)
    {
    int integer;
    float floatingPoint;
    
    do {
      printf("Please input an integer value: ");
      scanf("%i", &integer);
      (void) getchar();
    }while(integer < 0 || integer > 99);
    
    printf("Please input a floating point value: ");
    scanf("%f", &floatingPoint);
    
    printf("The integer value you entered was: %2g%i \n", integer);
    printf("The floating point value you entered was: %.2f \n", floatingPoint);
    }
    There are many ways to do this, this is just one. Using fgets(), checking the char's, then strtod() is another way, but you can also use the format input specifiers for scanf(), etc.

    And Welcome to the forum, RyanLeonard!

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    42
    That's a great help thank you so much. Was beginning to think myself that it was not possible.

    But now here comes the noob question, why ask the program is variable 'integer' less than 0 and more than 99..when i want the value to be more than zero and less than 99, needless to say the code works :/ ive somwhat confused myself.

  4. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    That is just saying: "read another input, while the number is out of bounds for what i need (<0 or >99)" so at the end you end up with the correct number in your program regardless of input blunders.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    42
    ohhh, I see now. That's great. Thank you so much. Love this forum

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    It's in a while(condition). So imagine yourself in a whirlpool of death, created and controlled by the evil maniac double agent.

    Every time you spin around, the evil traitor asks you "what is the number I need to rule the world?"

    You say "-1", but the traitor knows that's not right, so he let's his whirlpool spin you around again.

    Next time he asks, you say "100". but he knows that's not right either, and makes you keep spinning.

    The water's really cold, and you can't fight this forever, so you give him a number: "43".
    And he, being the trustworthy (though incredibly evil), traitor he is, let's you out of the whirlpool of death.

    You secretly laugh, because everyone knows the answer to everything is either "42" or that there is no answer to everything.

    ha ha ha, you have the last laugh!

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by Adak View Post
    Code:
      (void) getchar();
    What's with the casting getchar's return value to void when you're not actually doing anything with the return value anyway?
    Is that another TurboC thing?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by iMalc
    What's with the casting getchar's return value to void when you're not actually doing anything with the return value anyway?
    Is that another TurboC thing?
    Presumably to suppress a possible warning about an unused return value. It may not be Turbo C specific.
    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
    Sep 2006
    Posts
    8,868
    I got that tip from Laserlight, actually. I use it to hold the console window open. Yes, in Turbo C, but it should work in other compilers, as well.

    Used to use:

    getchar();

    But then you get the warning about "code has no effect in function". And the cusor jumps over to that window.

    So I used:

    i = getchar();

    But then you get the warning about "i is not used in this function" (if I didn't use i for anything else).

    So then I used:

    i = gethar(); ++i;

    Which worked fine, but looked odd. Laserlight's suggestion was better.
    Last edited by Adak; 10-20-2010 at 12:59 PM.

  10. #10
    Registered User
    Join Date
    Oct 2010
    Posts
    42
    what way could I constrain the float input to a template like xxx.xx, with three numbers left of the point? is the only way to use the same method?

    Regards
    Ryan

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    No, I mentioned three ways to do it, in my post. There are nearly ALWAYS multiple ways to do anything in C programming. What's accurate (always), efficient, and clear, should be your choice. Obscure ways are fun, but save them for the obfuscation contests.

    (They are a riot, though! ).

  12. #12
    Registered User
    Join Date
    Oct 2010
    Posts
    42
    Good, I used the same method as for the integer value. Hope it looks neat
    Thanks again Adak.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help assignment due tomorrow
    By wildiv in forum C Programming
    Replies: 6
    Last Post: 01-27-2010, 08:38 PM
  2. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  3. Assignment HELP!!
    By cprogrammer22 in forum C Programming
    Replies: 35
    Last Post: 01-24-2009, 02:24 PM
  4. No Match For Operator+ ???????
    By Paul22000 in forum C++ Programming
    Replies: 24
    Last Post: 05-14-2008, 10:53 AM
  5. C++ has an output maximum of 80 chars per line?
    By NinchN in forum C++ Programming
    Replies: 11
    Last Post: 02-01-2005, 07:45 AM