double new_amount;
printf("enter new_amount:");
scanf("%lf",&new_amount);
printf("Amount entered is%lf",new_amount);

Dani AI

Generated

The original post shows a tiny C example that reads and prints a floating value but no clear question. Common issues to watch for are wrong format specifiers, prompts that never appear because stdout wasn’t flushed, and fragile use of scanf for interactive input. provided the snippet; and rightly pointed out that the actual problem wasn’t stated; ’s remark about “old C code” is a good nudge toward safer input handling.

Practical checklist and fixes:

  • For reading a double with scanf the correct conversion is %lf; using the wrong specifier can cause undefined behavior. See the scanf family docs for details: scanf/fscanf.
  • For printing a double use %f (or a precision like %.2f). Use %Lf only for long double. See the printf family docs: printf/fprintf.
  • Always check the return value of scanf (it should be 1 for one successful conversion). Handle invalid input rather than assuming success.
  • If the prompt doesn’t appear before input, flush stdout (or include a newline) so the user sees the prompt.
  • For robust user input prefer fgets + strtod (handles bad input and ranges); see .

Minimal robust approach (example pattern):

char buf[128];
if (fgets(buf, sizeof buf, stdin)) {
    char *end;
    errno = 0;
    double d = strtod(buf, &end);
    if (end != buf && (*end == '\n' || *end == '\0') && errno == 0) {
        printf("Amount entered: %.2f\n", d);
    } else {
        fprintf(stderr, "Invalid number\n");
    }
}

Compile with warnings enabled (e.g. -Wall -Wextra) and fix any format warnings.

Recommended Answers

All 3 Replies

where is the question?

Whats the problem here ????

looks an awfully lot like my old crap C code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.