0

I feel the code should be executing properly for the floating point number printout however it keeps printing a zero value for some reason. Hopefully you can point me in the right direction of my mistake. I need it to display 2 decimal places to show as a currency as you can see in my code snippet. Thanks in advance

#include <stdio.h>

int main(void) {

double myNumber2;
printf("Guess how much I paid for my coffee this morning using the following format 1.65 which would represent $1 and 65 cents: ");
scanf("%.2f", &myNumber2);
printf("You guessed that I paid $%.2f for my coffee.\n", myNumber2);

return 0;
}
Tim
  • 13
  • 4
  • That didn't work :-/ Still receiving $0.00 as print value – Tim Jan 15 '20 at 07:54
  • 1
    Compile with `-Wall -Wextra` and fix the warnings – klutt Jan 15 '20 at 07:58
  • Try 'scanf("%lf", &myNumber2);' and 'printf("You guessed that I paid $%.02f for my coffee.\n", myNumber2);' – Andreas Wenzel Jan 15 '20 at 07:58
  • never mind I needed to change `double myNumber2;` to `float myNumber2;` – Tim Jan 15 '20 at 07:58
  • Instead of changing "double" to "float", you could also have changed the "f" in scanf to "lf". – Andreas Wenzel Jan 15 '20 at 08:00
  • Thank you for your help m8, I'd select your answer but it seems I cannot select answers because I have less than 10 rep. Not sure why I'm getting down voted for this.... Anyway much appreciated. If I end of getting enough rep to select your answer I'll be sure to select yours then. – Tim Jan 15 '20 at 08:03
  • Whose comment are you referring to? The link provider by user "moooeeeep" or the comments I wrote? You cannot accept comments, only answers. Nobody has provided your question with a formal answer yet, but I am currently writing a more detailed one for one for you. – Andreas Wenzel Jan 15 '20 at 08:09
  • To you @AndreasWenzel – Tim Jan 15 '20 at 08:10
  • Sorry new to this site, still trying to figure it out. – Tim Jan 15 '20 at 08:10
  • 2
    @Tim I downvoted. It's nothing personal, but this Q has very little chance of being useful for someone else. If you compiled with warnings and included the warnings in the question, then someone with the same problem could find this question. But in the same way, if you google your warnings you will find several questions already asked that will help you. – klutt Jan 15 '20 at 08:13
  • @klutt My apologizes, I attempted to limit the code and the length of my question to as little as possible to make it very clear as to what I was needing and easier for the person to help me without going through tons of data to find a tiny issue. I figured that would be preferred. – Tim Jan 15 '20 at 08:17
  • @Tim And that's a very good thing to do. It's called creating a [mre]. Kudos for doing that. – klutt Jan 15 '20 at 08:18
  • But ALWAYS compile with `-Wall -Wextra`. Warnings is the compilers way of telling you that your code probably don't do what you think it does. – klutt Jan 15 '20 at 08:19
  • I'm afraid that I cannot post my answer anymore, now that the question has been closed. So I will just make one further remark: Using scanf() without checking the return value can be dangerous, for the reasons described [here](http://sekrit.de/webdocs/c/beginners-guide-away-from-scanf.html). – Andreas Wenzel Jan 15 '20 at 08:22
  • Thanks for that link I will look through it. This is for a course as the link you sent suggested it most likely was. – Tim Jan 15 '20 at 08:27
  • Also, in my example, I wrote simply "f" instead of "lf" in the [printf](https://en.cppreference.com/w/c/io/fprintf). In my experience, this works, but I am not sure if it is formally correct or if it would work on all platforms. Therefore, using "lf" may be safer (assuming the type being printed is double, not float). The documentation that I linked to says that "lf" should be used (even if printf seems to work with me without the "l"). When using scanf, the extra "l" is certainly necessary. – Andreas Wenzel Jan 15 '20 at 08:30

0 Answers0