## CS50 Problem Set 1

### CS50 Problem Set 1

Hey guys, I've been following the CS50 lectures online to help teach me C. I'm understanding most of it so far but I'm very stuck on a small part of problem set 1. It involves displaying a variable in the form of currency. I've had a look around via google and have some ideas about what I can do. It's currently a long long variable, and I was thinking of converting it to a string with the format {0;C} but its either not possible or I'm really struggling with the code. Can someone please realign my thinking on the right track? Or any help at all would be great!!!
A brief snippet of my code:
Code: Select all
`            printf("Now please input the number of pennies you receive: ");            float p = GetFloat();            if(p > 0)                {                 p = (p/100);                    long long r = p * pow(2,m);                printf("For %d days with a start of \$%.2f, the total is \$%lld!\n",m, p, r);                ans2 = false;                ans1 = false;                }`

Currently the long long r is displayed in %lld format. I ideally need to add comma thousand spacing and 2 decimals after.
Again any help would be appreciated. Thanks!
Chasca
New User

Posts: 1
Joined: Thu Feb 16, 2012 11:49 am
Blog: View Blog (0)

### Re: CS50 Problem Set 1

You could try to transform the 64 bit integer(long long type), into an array of chars, then add ','(comma).

Code: Select all
`sprintf(buffer,"%.2lld",my_64bit_integer);//process your string`
ghost107
Poster

Posts: 110
Joined: Wed Jul 02, 2008 7:57 am
Blog: View Blog (0)

### Re: CS50 Problem Set 1

Chasca wrote:Hey guys, I've been following the CS50 lectures online to help teach me C. I'm understanding most of it so far but I'm very stuck on a small part of problem set 1. It involves displaying a variable in the form of currency. I've had a look around via google and have some ideas about what I can do. It's currently a long long variable, and I was thinking of converting it to a string with the format {0;C} but its either not possible or I'm really struggling with the code. Can someone please realign my thinking on the right track? Or any help at all would be great!!!
A brief snippet of my code:
Code: Select all
`            printf("Now please input the number of pennies you receive: ");            float p = GetFloat();            if(p > 0)                {                 p = (p/100);                    long long r = p * pow(2,m);                printf("For %d days with a start of \$%.2f, the total is \$%lld!\n",m, p, r);                ans2 = false;                ans1 = false;                }`

Currently the long long r is displayed in %lld format. I ideally need to add comma thousand spacing and 2 decimals after.
Again any help would be appreciated. Thanks!

You've been Googleing, eh? First page Google results for 'Converting a number to a string in C' :
http://www.programmersheaven.com/mb/Can ... functions/
http://www.java2s.com/Code/C/Data-Type/ ... seitoa.htm
http://gcc.gnu.org/ml/gcc-help/2003-12/msg00173.html
http://www.programmersheaven.com/mb/Can ... /?S=B20000
http://www.daniweb.com/software-develop ... -to-string
http://www.ozzu.com/programming-forum/c ... -t266.html
http://www.allegro.cc/forums/thread/588784

I had 10 posted, but the max number of URLs is 7 on HTS.
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rick Cook

centip3de
Moderator

Posts: 1217
Joined: Fri Aug 20, 2010 5:46 pm
Blog: View Blog (0)

Return to C and C++

### Who is online

Users browsing this forum: No registered users and 0 guests