Hello frueda75,
PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.
Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button. This will not automatically indent your code. That part is up to you.
You can use the preview button at the bottom to see how it looks.
I found the second link to be the most help.
|
To state what lastchance so nicely stated.
"setprecision" only works on floating point variables of "double" or "float" to specify how many decimal placed will be printed by the "cout" statement.
The line:
cout << fixed << setprecision(2);
Only need to be done once and I usually do this after I define the variables, but this is not required. Putting this line just before the output line that needs it works just fine.
A tip for the future. When you start working files you will need a line like:
outFile << fixed << setprecision(2);
because this is a different stream.
Used along with "std::setw()" this is a way to line up the decimal points in the output. "std::setw()" can also be used to set up columns so that each column is in the same place.
The other thing I would do is change all the "int"s to "double"s. This will not only give you a better answer, but will make use of the "setprecision".
Just to give you an idea of how you could write your code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
|
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
constexpr double FOUR{ 4.0 }, NINE{ 9.0 }; // <--- Example. Choose a better name that describes what the number is.
int carbs;
int fat;
int protein;
int totalCalories;
int proteinEnergy;
cout << fixed << setprecision(2); // <--- Only needs done once.
// <--- Prompt.
cin >> carbs; // <--- You may know what to do, but no one else does. These need a prompt.
// <--- Prompt.
cin >> fat;
// <--- Prompt.
cin >> protein;
totalCalories = (carbs * FOUR) + (fat * NINE) + (protein * FOUR);
proteinEnergy = protein / (fat + carbs);
totalCalories << " " << proteinEnergy << '\n'; // <--- This does compile, but "totalCalories" may end up with a much
// different value. I think here the "<<" means left bit shift.
// Not the insertion operator that you want.
return 0;
}
| |
I am not sure what you actually work with, but this example is much easier to read. Just a suggestion.
Andy