0

I am very new to c but I have to create a program for school and for that, I need to know how many decimal numbers are in a number but when I try this a bunch of random numbers come from the 4th dimension and I don't know how to fix it, can anybody help me?

Here is the code:


int main(void) {
  float test = 1.567;
  while (test != 0){
    test = test * 10;  
    test = test - (int)test;
    printf("%f\n",test);
  }
}

Edit: I'm running on windows 10, and I'm using repl.it to program and run it. This is the output I get

0.670000
0.700001
0.000008
0.000076
0.000763
0.007629
0.076294
0.762939
0.629395
0.293945
0.939453
0.394531
0.945312
0.453125
0.531250
0.312500
0.125000
0.250000
0.500000
0.000000

Edit 2: The assignment is that we have to create a program where you have to enter an amount of money and it tells you how many 2 euro coins, 1 euro coins, etc. you need to get that amount and because you can't have 4.63454 euro I wanted to make sure that you can only enter 2 decimal numbers otherwise it would throw an error. So TLDR: you enter the number via the keyboard, but the same thing happens then.

Youp Lamb
  • 21
  • 3
  • What "bunch of random numbers"? What OS are you running on? What compiler are you using? – Andrew Henle Sep 10 '20 at 19:07
  • Please show **in your question** the output you get. – Bodo Sep 10 '20 at 19:08
  • 1
    Floating point variables do not store a precise number of decimal places. If the input is supposed to be from a user then I suggest inputting to a string and analysing that. – Weather Vane Sep 10 '20 at 19:10
  • The standard `printf` format `%f` prints only 6 digits. If you use e.g. `printf("%.30f\n",test);` you will see that even your initial number does not have the exact value. – Bodo Sep 10 '20 at 19:14
  • @WeatherVane How can I do calculations on a string? – Youp Lamb Sep 10 '20 at 19:15
  • Just count the digits which follow a decimal point (if present). Perhaps ignoring any trailing `'0'`s. – Weather Vane Sep 10 '20 at 19:16
  • @Bodo How do I fix that? – Youp Lamb Sep 10 '20 at 19:16
  • @YoupLamb You cannot "fix" the implementation of floating point numbers. Please [edit] your question and add more details about your assignment. Where does the number come from in your real program? I guess it will not be a hard coded value. – Bodo Sep 10 '20 at 19:23
  • @Bodo okay I've edited the post – Youp Lamb Sep 10 '20 at 19:30
  • Your assignment does not require usage of floating point. Currency can be precisely represented using only integers. – Eugene Sh. Sep 10 '20 at 19:34
  • Can an integer have a decimal point? @EugeneSh. – Youp Lamb Sep 10 '20 at 19:35
  • Decimal point is for representation (display) only. The calculations should be made as integers (for example in euro-cents). – Eugene Sh. Sep 10 '20 at 19:37
  • Read the input as a string, check it character by character. Then either remove the decimal point and use functions like `strtol` to convert it into an integer (long) number representing the value in Euro cents or convert the characters to a number yourself, digit-by-digit. – Bodo Sep 10 '20 at 19:40
  • @EugeneSh. let's say I enter the amount 4.654 if you remove the decimal point you get 4654 cents or 47 euro's but that's not the amount that I entered – Youp Lamb Sep 10 '20 at 19:41
  • This is about how you parse and validate you input – Eugene Sh. Sep 10 '20 at 19:42
  • See [Is floating point math broken?](https://stackoverflow.com/q/588004/5987) – Mark Ransom Sep 10 '20 at 19:51

2 Answers2

1

I wanted to make sure that you can only enter 2 decimal numbers otherwise it would throw an error.

This really is a text processing issue as "%f" and friends lose the needed information of decimal places textually entered.

 char buf[100];
 if (fgets(buf, sizeof buf, stdin)) {
   char *endptr;
   double val = strtod(buf, &endptr);  // add errno check as desired 
   unsigned char *dp = strchr(buf, '.');
   if (endptr > buf && dp && isdigit(dp[1]) && isdigit(dp[2]) && dp[3] == '\n') {
      // success
      long long money = llround(val * 100.0);  // Scale by 100 and round to an integer
      ...
   } else {
      // error
   }
}

For money, consider using long long. A billion is not what it used to be.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
0

Okay I fixed it by doing the following:

#include <stdio.h>

int main(void) {
  float test = 1.567;
  int euro = 0;
  int cent = 0;
  int eurocent = 0;
  float CentFloat = 0;
  euro = (int)test;
  CentFloat = test - euro;
  cent = CentFloat * 100;
  eurocent = euro *100 + cent;
  //test2 = test * 10;  
    
    printf("%.30f\n",test);
    printf("%d\n",euro);
    printf("%d\n",cent);
    printf("%d\n",eurocent);
  }

This gives an output of:

1.567000031471252441406250000000
1
56
156

This was not exactly what I originally had in mind, but it solves the issue of having to check if someone entered too many decimal numbers and it was not hard to make my original program work with this. Than you all for the help!

Youp Lamb
  • 21
  • 3