374 / 9 = 41.6 (from calculator )
if in c++, it will become 374 / 9 = 41.555
how do i make the final calculation become 41.6 ?
i've tried ceil() and setprecision(1) , the answer i got quite big different compare with the answer from calculator.

answer from ceil()
42 * 15.85 = 665.70

answer from setprecision(1), nothing change, it only works for print out
41.55 * 15.85 = 658.56

answer from calculator
41.6 * 15.85 = 659.36

> how do i make the final calculation become 41.6 ?

#include <iostream>
#include <iomanip>
#include <sstream>

int main()
{
  std::cout << 374.0 / 9.0 << '\n' ;
  std::cout << std::fixed << std::setprecision(1) 
            << 374.0 / 9.0 << '\n' ;
  
  std::stringstream stm ; 
  stm << std::fixed << std::setprecision(1) 
      << 374.0 / 9.0 << '\n' ;
  stm.seekg( 0, std::ios::beg ) ;
  double result ;
  stm >> result ;
  std::cout << result << '\n' ;
  // *note* result == ( 41.6 += epsilon )
  // http://www.parashift.com/c++-faq-lite/newbie.html#faq-29.16
  // http://en.wikipedia.org/wiki/Floating_point
}

Comments:
What kind of calculator gives result to just one decimal place? Why would you use it?

Why would you want to throw away precision? Most of us strive to maintain as much precision as possible for as long as possible in any chain of expressions. Let rounding occur on output or at the very last stage.

> how do i make the final calculation become 41.6 ?

#include <iostream>
#include <iomanip>
#include <sstream>

int main()
{
  std::cout << 374.0 / 9.0 << '\n' ;
  std::cout << std::fixed << std::setprecision(1) 
            << 374.0 / 9.0 << '\n' ;
  
  std::stringstream stm ; 
  stm << std::fixed << std::setprecision(1) 
      << 374.0 / 9.0 << '\n' ;
  stm.seekg( 0, std::ios::beg ) ;
  double result ;
  stm >> result ;
  std::cout << result << '\n' ;
  // *note* result == ( 41.6 += epsilon )
  // http://www.parashift.com/c++-faq-lite/newbie.html#faq-29.16
  // http://en.wikipedia.org/wiki/Floating_point
}

thanks lot ^^
i have learnt something new :)

Comments:
What kind of calculator gives result to just one decimal place? Why would you use it?

Why would you want to throw away precision? Most of us strive to maintain as much precision as possible for as long as possible in any chain of expressions. Let rounding occur on output or at the very last stage.

I am sorry, i didn't mean that. i just try to satisfied the question given by my lecturer.
The calculator I am using is on-screen calculator from Windows XP

*********************************************************
Write a program to compute the cost for carpeting a room. Input should consist of the room length, room width, and carpet price per square yard. Use constants for the pad charge and installation charge. Include a heading as part of the output.
A typical input screen would be:

What is the room length in feet? <Enter> ?
What is the room width in feet? <Enter> ?
What is the carpet price/square yard? <Enter> ?

Output for a sample run of this program (without heading) could be

Dimension of the room (in feet) are 17 x 22.
The area to be carpeted is 41.6 square yards.
The carpet price is $11.95 per yard.

Room dimensions 17 x 22

Carpet required 41.6 square yards

Carpet price/yard $ 11.95

Pad price/yard $ 2.95

Installation cost/yard $ 0.95

Total cost/yard $ 15.85

Total cost $659.36
**************************************************

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.