setprecision not working

Write your question here.

#include <iostream>
#include <iomanip>
using namespace std;

int main() {
int carbs;
int fat;
int protein;
int totalCalories;
int proteinEnergy;


cin >> carbs;
cin >> fat;
cin >> protein;



totalCalories = (carbs * 4) + (fat * 9) + (protein * 4);

proteinEnergy = protein / (fat + carbs);

cout << fixed << setprecision(2);


totalCalories << " " << proteinEnergy << endl;





return 0;


}





// the set precision is not giving me the correct output im brand new to c++ and it gave me the right answer on another question but now it isn't working im wondering what im doing wrong
All your variables are of type int, so you will get an integer division truncation (and hence a mathematical error) on the line where you calculate proteinEnergy, your "fixed" and "setprecision" won't have any effect on ints, and you appear to have copy-paste errors on your output line.

Please use code tags.
Ok thank you im brand new to this so this helps a lot
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
I would add that you forgot a "cout" before outputting totalCalories and ProteinEnergy, it won't work without that. I assumed you meant for that to go to output, if you didn't please explain exactly what you meant to do.

@Handy Andy is right, it is considered good programming practice to output a prompt before any user input.

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
#include <iostream>
#include <iomanip>

using namespace std;

int main (int argc, char const *argv[]) 
{
	// consolidate variables so they are all one data type
	double carbs, fat, protein, totalCalories, proteinEnergy;
    
	// prompt user input
	cout << "Enter carbs. \n: ";
	cin >> carbs;
	
	// prompt user input
	cout << "Enter fat. \n: ";
	cin >> fat;
	
	// prompt user input
	cout << "Enter protein. \n: ";
	cin >> protein;
	
	totalCalories = (carbs * 4) + (fat * 9) + (protein * 4);
	
	// if you use an int here, it will truncate the remainder (like Andy said).
	// if it's a double, it will add the remainder on the end as a decimal.
	proteinEnergy = protein / (fat + carbs);
	        
	// You can also do the output like this:
	cout << fixed << setprecision(2)
	     << totalCalories << " " << proteinEnergy << endl;
    
        return 0; 
}
1) If not used, there's no need to provide parameters to main().

2) Initialise a variable when defined.

3) When a variable is defined and initialised with a value, use auto for type (unless a good reason why not).

3) if a variable isn't going/intentended to change, mark it const.

4) use \n to output a new line rather than endl - as endl does a flush which \n doesn't. Especially when outputting to a file stream.

5) Within main(), there is no requirement to end the function with return 0. This is assumed if not provided.

6) Use a const variable to specify 'magic' numbers that are used.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
	const auto fatfact {9.0}, carprotfac {4.0};
	auto carbs {0.0}, fat {0.0}, protein {0.0};

	cout << "Enter carbs: ";
	cin >> carbs;

	cout << "Enter fat: ";
	cin >> fat;

	cout << "Enter protein: ";
	cin >> protein;

	const auto totalCalories {carprotfac * (carbs + protein) + fatfact * fat };
	const auto proteinEnergy {protein / (fat + carbs)};

	cout << fixed << setprecision(2) << totalCalories << ' ' << proteinEnergy << '\n';
}

@seeplus,
Well, that's how TextMate (the code editor I use) formats the main(). I type in "main" into the text editor, hit "tab", and it automatically creates this:
1
2
3
4
5
int main (int argc, char const *argv[])
{
	/* code */
	return 0;
}

So no, the arguments aren't needed, that's just the way my code editor formats it. As for return 0, its the same thing (and I was taught to always return 0 or EXIT_SUCCESS from main).

On the side, is initializing variables with brackets like int variable {15}; legal? I've never seen it done that way before.
Last edited on
is initializing variables with brackets like int variable {15}; legal? I've never seen it done that way before.


This is uniform initialisation. It was introduced with C++11 with some tweaks in subsequent versions. See https://www.geeksforgeeks.org/uniform-initialization-in-c/
Another explanation of the benefits of C++11's uniform initialization, including a bit on initializer lists.
https://mbevin.wordpress.com/2012/11/16/uniform-initialization/
Eye-opening! Thanks, @Furry Guy!
Registered users can post here. Sign in or register to post.