-3

I've been working on this homework assignment for a while and am about ready to pull my hair out.

I need help rounding a float to the tenths place while still showing a 0 in the hundredths place and nothing I do seems to do that. i.e. 2.47 = 2.50

#include "stdafx.h"
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
float MPH;
float seconds;
const float MPH2MPS = (1609.00 / 3600.00);
float a;

cout << "       Acceleration calculator" << endl;
cout << "" << endl;

cout << "Please enter the velocity in miles per hour: ";
cin >> MPH;
cout << "" << endl;

cout << "Please enter the time in secounds: ";
cin >> seconds;
cout << "" << endl;
cout << "" << endl;
cout << "" << endl;

a = MPH2MPS * ( MPH / seconds );

cout << showpoint << fixed << setprecision(2);
cout << "The acceleration required by a vehicle to reach" << endl;
cout << "" << endl;

cout << "a velocity of " << MPH << " miles per hour in " << seconds << " seconds" << endl;
cout << "" << endl;

cout << "is " << setprecision(1) << a << " meters per second" << endl;
cout << "" << endl;
cout << "" << endl;

system("pause");
return 0;

Any ideas?

Clueless
  • 1
  • 1
  • 1
    Welcome to Stack Overflow. Please take the time to go through the [The Tour](http://stackoverflow.com/tour) and refer to the material from the [Help Center](http://stackoverflow.com/help/asking) on what and how you can ask here.It's specially important to post a [mcve]. – R Sahu Sep 25 '17 at 19:06
  • SHow the code, please. And tell us if you want to round the variable itself or if you want to round the display. – Christophe Sep 25 '17 at 19:06
  • The obvious choice here would be to implement your own rounding function.... – xyious Sep 25 '17 at 19:07
  • can you tell which values you use as input ? – Christophe Sep 25 '17 at 19:12
  • I mean I know I'm stupid at this as I'm just starting out and taking this class for my basics. I'm just looking for advice to help me out a bit. – Clueless Sep 25 '17 at 19:12
  • 60 mph and 6 seconds – Clueless Sep 25 '17 at 19:13
  • 1
    Possible duplicate of [Rounding a double number up to the tenths place](https://stackoverflow.com/questions/7870003/rounding-a-double-number-up-to-the-tenths-place) – Misho Tek Sep 25 '17 at 19:18
  • To answer the one question asked: Yes. I have ideas. – user4581301 Sep 25 '17 at 19:23

1 Answers1

0

Reading @asterite's answer here:

cout << "is " << static_cast<double>(std::round(a * 10)) / 10 << " meters per second" << endl;

Note the round function only works if you #include <cmath>

Update: Modified math.h with cmath, thanks to @user4581301

Neb
  • 2,270
  • 1
  • 12
  • 22
  • For whatever reason round isn't working even after including the math library. – Clueless Sep 25 '17 at 19:30
  • It works to me, prompting 100 for the vel and 10 for the sec, I get 4.50, which is 4.47 rounded to tenth and followed by a 0. If i misunderstand something, please, let me know. – Neb Sep 25 '17 at 19:41
  • I don't know why but it's claiming that round is not a member of 'std' and identifier not found. – Clueless Sep 25 '17 at 19:56
  • you need to include the cmath library. That error is shown when you do not include it properly – Neb Sep 25 '17 at 20:22