0

I tried using value of scanf but the code does not run properly.

#include<stdio.h> 
#include<math.h>

int main() {
  float dollars;
  int p, i = 0, count = 0;
  do {
    printf("Change owed:");
    scanf("%f", &dollars);
    if (dollars < 0) {
      printf("Foo\n");
    }
  } while (dollars < 0);

  int cents = round(dollars * 100);
  int denom[] = {25, 10, 5, 1};
  while (cents >= 1) {
    if (cents >= denom[i]) {
      count++;
      cents = cents - denom[i];
    } else {
      i++;
    }
  }
  printf("%d\n", count);
  return 0;
}
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
  • Please provide a [mre]. Looks like line endings composted during pasting. – Yunnosch Aug 27 '20 at 20:04
  • 1
    What do you mean by "does not run properly"? – Yunnosch Aug 27 '20 at 20:04
  • 4
    Do not use float for currency dollars. Instead use int for cents. – Yunnosch Aug 27 '20 at 20:05
  • https://stackoverflow.com/editing-help – Yunnosch Aug 27 '20 at 20:06
  • Assuming that the input is your problem see http://sekrit.de/webdocs/c/beginners-guide-away-from-scanf.html and https://stackoverflow.com/questions/35178520/how-to-read-parse-input-in-c-the-faq – Yunnosch Aug 27 '20 at 20:08
  • 2
    You say you "tried using value of scanf", but in your code you completely ignore the value returned by scanf. – William Pursell Aug 27 '20 at 20:10
  • Why are you using `scanf`? It doesn't seem like this program needs to be reading its input at all. Rather, the value you want to work with should be taken from `argv` – William Pursell Aug 27 '20 at 20:13
  • If I take int p=scanf("%f",&dollars) and for p=0, take another condition, then the while loop runs infinitely. – Sakshi Pandita Aug 27 '20 at 20:18
  • SakshiPandita You get an endless loop with [this](https://stackoverflow.com/questions/63623378/how-to-reject-a-non-numeric-input-in-this-code/63623481#comment112506976_63623378) as it does not consume the non-numeric data like [this](https://stackoverflow.com/a/63623481/2410359). – chux - Reinstate Monica Aug 27 '20 at 22:45

2 Answers2

1

How to reject a non-numeric input in this code?

Check the return value of scanf("%f", &dollars);. Use getchar() until the end-of-line to read the rest of the line and then read again when the scan count was 0.

int cnt;
do {
  cnt = scanf("%f", &dollars);
  if (cnt == EOF) {
    return 0; // no input
  }
  int ch;
  while ((ch = getchar()) != '\n') && ch != EOF) {
    ;
  }
} while (cnt == 0);
...
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
0

See the trick here: scanf returns the number of succesfully read variables. Working with currencies would be better if you do it as integers (e.g: 1 dolar = 100 cents, so 1.85 dolar = 185 cents).

If you provide an integer, it wouldnt read the dot and the cents part (assume that should be 0 maybe?).

If it couldn't read at least the integer part then you report the error.

int main() 
{
   int dolar, cent = 0, read;
   read = scanf("%d.%d", &dolar, &cent);
   if(read < 1)
       printf("You didnt provide input in a valid format\n");
   else
       printf("Number provided was: %d.%d\n", dolar, cent);
}
vmp
  • 2,370
  • 1
  • 13
  • 17
  • As OP is looking for resilient input detection, input like `"123.4"` will form $123.04. `"123. 45"` forms $123.45, `"123.-12"` forms $122.88 `"-123.45"` forms $-122.55. The `"%d.%d"` approach makes for additional complications handled well enough with `scanf("%f", &dollars); ... int cents = round(dollars * 100.0);` – chux - Reinstate Monica Aug 28 '20 at 01:26
  • The value returned by `scanf()` is not the number of read characters. It is the number of successful conversions that assigned to a variable — meaning that assignment-suppressing conversions such as `%*d` don't get counted (and neither does `%n`, but that's a different special case). The value might also be EOF. For `scanf("%d.%d", …);`, the return values might be EOF, 0, 1, 2. Note that entering `10.9` will give the same result as `10.09` and you might run into problems because of that. – Jonathan Leffler Aug 28 '20 at 03:32