Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Help with floats and formatting.
Index -> Programming, C -> C Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
ntba




PostPosted: Sun Jan 18, 2009 4:27 pm   Post subject: Help with floats and formatting.

Hello, first I used to have an account on here, I don't know where it went though Rolling Eyes

But my problem today is one given to me by one of my profs for a C programming class.

It's a simple problem, which according to him should take no more then 10 lines and the shortest way to do it is in 3.

We are supposed to ask a user for a float and then print it out, BUT we can only print out the numbers PAST the decimal point and leave no leading 0.

IE:

USER ENTERS: 1.243
RETURNS: .243

You can tell I tried so far to just get rid of the integer which is sort of a "hackish" way to do it, but for the life of me I can't strip that 0. I doubt he wants us to use any giant string functions to remove it or anything, and since it's possible to do in 3 lines then there must be an easy way to format.

I've also tried "%0.7f" and other such ways to format and I've read up on printf but I just can't seem to find anything that works!

The integer part is commented out for now since it's not the way I'd like to do it.

code:

#include <stdio.h>

int main (void)
{
    int intpart;
    float val;
    printf ("Enter a floating point number: ");
    scanf ("%f", &val);
    /*intpart=(int)val;
    val=val-intpart;*/
    printf ("%f" , val);
    return 0;
}
Sponsor
Sponsor
Sponsor
sponsor
OneOffDriveByPoster




PostPosted: Sun Jan 18, 2009 5:55 pm   Post subject: Re: Help with floats and formatting.

Why would a string solution be "giant"?

Do you understand what this code does?:
c:
char *s;
for (s = s0; *s != 'a'; ++s);


We are assuming that input is valid of course...
ntba




PostPosted: Sun Jan 18, 2009 6:07 pm   Post subject: Re: Help with floats and formatting.

Well since it's only the beginning of the class and we haven't learned how to work with string manipulation and such, and because the prof says its "something so easy you'll laugh at yourself when you see it"

I only want to know what it could be that's so easy and can be done in 3 lines within main() including the var declares

This is just the chapter on variables and coming from other languages I'm not familiar with how I can format things using C


I did something like this, its another hack, and breaks for some examples like when i put in 1.333333335 or something, otherwise 3.14159 works fine and gives back .14159

code:

#include <stdio.h>

int main (void)
{
    long int val, val2;
    printf ("Enter a floating point number: ");
    scanf ("%d.%d" , &val,&val2);
    printf (".%d" ,val2);
    return 0;
}



EDIT: I used LONG INT instead of int and it works for most of the cases but I still feel it to be very hackish
Display posts from previous:   
   Index -> Programming, C -> C Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 3 Posts ]
Jump to:   


Style:  
Search: