how to show the % sign and remove decimals?

This is a discussion on how to show the % sign and remove decimals? within the C Programming forums, part of the General Programming Boards category; trying to show the percentage sign but it keeps thinking i'm telling the program it as a command (well whatever ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    38

    how to show the % sign and remove decimals?

    trying to show the percentage sign but it keeps thinking i'm telling the program it as a command (well whatever it would be called lol)

    Code:
    #include <stdio.h>
    int main ()
    {
    int c;
    float roa;
    float ni;
    float avg;
    {
    printf( "Welcome to Return on assets calculator\n" );
    printf( "NOTE: DO NOT USE COMMAS! USE PERIOD TO PUT IN DECIMALS!\n" );
    printf( "Enter the Net income\n" );
    scanf("%f", &ni);
    printf( "Now enter the avarage total assets\n" );
    scanf("%f", &avg);
    printf( "Your Retun on assets is\n" );
    roa = ni/ avg;
    roa = roa* 100;
    printf("%f", roa);
    printf( "percent" );
    while( (c=getchar()) != EOF && c != '\n' ){ }
    }
    
    getchar ();
    return 0;
    if put the sign itself instead of writing it out, it wont even show it

    oh and how do i remove decimals?
    so instead of getting an answer like 15.77454564 it would show 15.77
    Last edited by seal; 08-30-2005 at 12:57 AM.

  2. #2
    ~viaxd() viaxd's Avatar
    Join Date
    Aug 2003
    Posts
    246
    %% - two percent sings gives you what you need.
    %.2f - gives you 2 places after the period.

    oh, and you are probably having problems with scanf, right?
    :wq

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    38
    Quote Originally Posted by viaxd
    %% - two percent sings gives you what you need.
    %.2f - gives you 2 places after the period.

    oh, and you are probably having problems with scanf, right?
    dont think so, the program runs smooth

    why whats wrong with it and where?
    Last edited by seal; 08-30-2005 at 01:24 AM.

  4. #4
    ~viaxd() viaxd's Avatar
    Join Date
    Aug 2003
    Posts
    246
    you see, after the first call to scanf, when the user enters a number a newline is usually left in the input buffer. this is the problem...it works for your code because scanf expects a float, but if you want to for example read a string after your scanf calls, it will just read that newline in the input buffer, that's not good.
    :wq

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    10

    Thumbs up

    Quote Originally Posted by viaxd
    you see, after the first call to scanf, when the user enters a number a newline is usually left in the input buffer. this is the problem...it works for your code because scanf expects a float, but if you want to for example read a string after your scanf calls, it will just read that newline in the input buffer, that's not good.
    So what do you do about it?

  6. #6
    Nonconformist Narf's Avatar
    Join Date
    Aug 2005
    Posts
    174
    So what do you do about it?
    Always read input as strings? Read and discard all whitespace from the stream before the next call to scanf()? Use a leading space in the format string to force scanf() to strip leading whitespace? Understand that most format specifiers in scanf() strip leading whitespace, but some don't, and other input functions definitely don't?
    Just because I don't care doesn't mean I don't understand.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sign ' is the same as \' ?
    By George2 in forum C Programming
    Replies: 1
    Last Post: 11-23-2007, 06:32 AM
  2. My own itoa()
    By maxorator in forum C++ Programming
    Replies: 18
    Last Post: 10-15-2006, 11:49 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21