Here is my code....

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

#include <iomanip>

int _tmain(int argc, _TCHAR* argv[])
{
    int a,b,c,d;

    cout << "Previous odometer reading: ";
    cin >> a;

    cout << "New odometer reading: ";
    cin >> b;

    cout << "Gallons added to the tank to fill: ";
    cin >> c;

    d = (b - a)/c;
    cout << fixed << setprecision(2);
    cout << "Car got " <<  d << " miles per gallon on last tank of gas. \n" << endl;

    return 0;
}

/*
Previous odometer reading: 10543
New odometer reading: 10941
Gallons added to the tank to fill: 11.2
Car got 36 miles per gallon on last tank of gas.

Press any key to continue . . .
*/

The result supposes to be like this "Car got [B]35.45[/B] miles per gallon on last tank of gas". Although I have already setprecision(2), the ouput didn't show up. I don't know what is wrong with my code. Any comment ???

Recommended Answers

All 3 Replies

I think you just need to make d a float instead of an int

HI zoner7,
I just try using float for d and it does work. I don't know what is the different between int and float. I'll make a research for that. Thank you for your help.

Read this and after that, perhaps this

In short: an int is a whole number, without a comma (like 2). A float will have a floating point (like 2.000)

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.