Set Precision not working.

Hi again everyone, so it seems I have everything working except my rounding. I want the final output to give me two decimal places not one. Also how do you write it into the forum as 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
37
38
39
40
41
42
43
int main()
{
	//Declare Variables
    string userInput;
	double basePrice = 0.0;
	double subTotal = 0.0;
	double finalPrice = 0.0;
	string optionPackageCodeArray[5] = { "BB", "SP", "NP", "HE", "UC" };
	string packageNameArray[5] = { "Base", "Sport", "Intermediate", "Luxury", "User Specified" };
	double packageCostArray[5] = { 1500.00, 3250.00, 4575.00, 7500.00, 5220.00 };

	//Prompt user for Base Price
    cout << "Enter Base Price for Car:";
    cin >> basePrice;

	//Promt user for Code
	cout << "Enter the 2-letter Code:";
	cin >> userInput;

//Loop to check if code is Valid
        for (int i = 0; i < 5; i++)
        {
            if (userInput == optionPackageCodeArray[i])
            {
				//Code is valid
				//Prices are computed
                subTotal = basePrice + packageCostArray [i];
                finalPrice = (subTotal * .015) + subTotal;

				//Display the result
                cout <<"Package: " <<packageNameArray[i] << endl <<"Final Price : $" << setprecision (2) << finalPrice << endl;
				//This is here because otherwise system refused to pause, it is also efficient.
				system ("PAUSE");
				return 0;
            }//end if
        }//end for
//Code input was not valid
 cout << "ERROR: Bad Code" << endl;
 
 system ("PAUSE");
	return 0;
} //end of main
      
Last edited on
http://www.cplusplus.com/reference/ios/fixed/
std::cout << std::fixed << std::setprecision(2) << final_price << '\n' ;


> how do you write it into the forum as code?

See: http://www.cplusplus.com/articles/jEywvCM9/
Yeaaahhh I just realized I forgot to put it in before. But thanks mate! :)
Topic archived. No new replies allowed.