-1

When I testBed my code, it will not put any decimals on my numbers. However, all but one number get decimals in my second test.

I am not sure if it is just that float only puts decimals when the user needs them, or if I am missing something in my code. Do I need to specify how many decimals to include, and if so, how would I change that?

Thanks ahead of time for any help.

int main()
{  

  float income;

  cout << "\tYour monthly income: ";
  cin  >> income;

  cout << "\tItem                  Budget          Actual\n";
  cout << "\t=============== =============== ===============\n";
  cout << "\tIncome" << setw(11) << right << "$" << setw(11) << right << income;
  cout << setw(5) << right << "$" << setw(11) << right << income <<"\n";

}

This is the error I am getting

        >    Your monthly income: 1

        > \tIncome          $          1    $          1\n
Expected: \tIncome          $       1.00    $       1.00\n
  • 3
    It is not very clear for me what is your expected output. You problem may be due to the fact that `float` calculation is broken. You might perform all your calculations in cents (= integers) – Damien May 19 '20 at 14:30
  • 3
    Remove all of that prompting and all of the table stuff and most of the rest of the output. You should be able to reduce this code to something **much smaller** that compiles, runs, and shows the problem. I'd guess that one line in `main` is all that's needed. This is partly for the convenience of the folks who read these questions and try to answer them, but it's also a good skill to have so that you can isolate and analyze problems on your own. – Pete Becker May 19 '20 at 15:20
  • 1
    The reason they aren't showing decimal places is because `cout` will default to print no decimal places if it's just zeros. See https://stackoverflow.com/questions/5907031/printing-the-correct-number-of-decimal-points-with-cout – mukunda May 20 '20 at 02:19
  • @PeteBecker I tried to reduce the code that I have up. I know it is not exactly what you asked for, but it is probably the best I can do with my understanding. – Latter-Day Lucas May 20 '20 at 02:20
  • Oh wow right when I re-commented you responded! Hah. @mukunda I will check that out right now. – Latter-Day Lucas May 20 '20 at 02:21
  • @mukunda, will I need to use std::cout for every variable? – Latter-Day Lucas May 20 '20 at 02:23
  • Offhand the only thing I know is that `cout` is a pain in the butt to format things, but that should get you in the right direction. – mukunda May 20 '20 at 02:24
  • The meaning of 'floating' in 'floating-point' isn't what you seem to think. It means that the value is stored in a normalized form with a separate exponent. – user207421 May 20 '20 at 04:16
  • @user207421 Oh okay. So, if I understand correctly, float is a way to store the data, not to format the output. Is that what you mean? – Latter-Day Lucas May 22 '20 at 20:11

1 Answers1

0

Use std::cout.precision and std::fixed to output currency:

#include <iostream>
// ...

cout.precision(2);
cout << std::fixed;
cout << "\tIncome" << setw(11) << right << "$" << setw(11) << right << income;
cout << setw(5) << right << "$" << setw(11) << right << income <<"\n";
Rom Grk
  • 526
  • 5
  • 15
  • This worked perfectly. Thank you! What is peculiar is that I was using the cout.precision and std fixed before, but I was writing it differently and it kept adding unwanted numeric inputs. Thanks for the fix. My hair will receed a little less now! :) – Latter-Day Lucas May 20 '20 at 02:41