0

I'm banging my head against a wall trying to ensure that the rounding and casting of the user input works. Here's what's happening: If I input a value with a dollar sign I'm prompted for a retry but if I input a value in cents only (without a decimal point as requested) my result is obviously 100 times too big. How can I ensure the input I get is in the format I need (> 0.00)? Here's my code so far, I'm doing it in stages:

//ask for change required in format needed

printf("How much change do you need? Please give answer in form 0.00: ");

float num = GetFloat();

//convert float to int using 'round' then 'cast' from 'float' to 'int'

int numtwo = (int) round (num*100);

printf("%d \n", numtwo);
0
1

The above statements seems to be right.

I think the problem might be in the conditions.

Are you sure that you are comparing numtwo without decimal points like 25 instead of 0.25?

This might be the chance because in int numtwo = (int) round (num*100); you actually get numtwo to be 100 times value of num.

4
  • what I'm trying to do is convert the input in dollars and cents in form 0.00 into a cents value only hence my multiplying by 100. I'm a noob so I'm probably screwing something up. Oct 1 '14 at 15:03
  • maybe I could help you in debugging it, if you would mail me the code : 100appu@gmail.com Oct 1 '14 at 15:14
  • Hi, here's my code so far. This is obviously just asking for input and then casting and rounding. #include<cs50.h> #include<stdio.h> #include <math.h> int main (void) { //ask for change required in format needed printf("How much change do you need? Please give answer in form 0.00: "); float num = GetFloat(); //convert float to int using 'round' then 'cast' from 'float' to 'int' int numtwo = (int) round (num*100); printf("%d \n", numtwo); } Oct 1 '14 at 15:23
  • I think you could just use the while loop to ensure input is > 0.00 Oct 1 '14 at 15:41

Not the answer you're looking for? Browse other questions tagged .