Sunday, August 2, 2009

C++ help please! why my program wont show up my output with 2 decimals?

i have included the #include %26lt;iomanip%26gt;, using std::fixed;


and using std::setprecision; still it wont show up my output with the 2 decimals. why? this is my code:





#include %26lt;iostream%26gt;


#include %26lt;iomanip%26gt;





using std::cout;


using std::cin;


using std::endl;


using std::fixed;


using std::setprecision;





int main()


{


//declare variables


char seatLocation = ' ';


int ticketPrice = 0;





//enter input


cout %26lt;%26lt; "Enter seat location (B(box), P(pavilion), or L(Lawn): ";


cin %26gt;%26gt; seatLocation;


seatLocation = toupper(seatLocation);





//calculate and display order price





switch (seatLocation)


{


case 'B':


ticketPrice = 75;


break;


case 'P':


ticketPrice = 30;


break;


case 'L':


ticketPrice = 21;


break;


default:


cout %26lt;%26lt; "Invalid location" %26lt;%26lt; endl;


}


//end switch





// display shipping charge


cout %26lt;%26lt; fixed %26lt;%26lt; setprecision(2);


cout %26lt;%26lt; "Ticket price: $" %26lt;%26lt; ticketPrice %26lt;%26lt; endl;


cin %26gt;%26gt; ticketPrice;


return 0;


} //en

C++ help please! why my program wont show up my output with 2 decimals?
If you're wondering why ticketPrice isn't showing 2 decimals, it's because you've declared it as an int. Int's do not have the capability to store decimal places, you need to use the datatype double.
Reply:Maybe because TicketPrice is an int and int's don't have decimal precisions. Try making it a double.





Hope that helps.


No comments:

Post a Comment