0

The out expected to be 'The total meal cost is 13 dollars.' but it give 'The total meal cost is 12 dollars.'

#include <bits/stdc++.h> #include <iostream> #include <cmath> using namespace std; void solve(double meal_cost, int tip_percent, int tax_percent) { long double tip = (meal_cost*tip_percent)/100; long double tax = (meal_cost*tax_percent)/100; double totalcost = meal_cost+tip+tax; cout<< "The total meal cost is " << int(totalcost) << " dollars." <<endl; } int main() { double meal_cost; cin >> meal_cost; cin.ignore(numeric_limits<streamsize>::max(), '\n'); int tip_percent; cin >> tip_percent; cin.ignore(numeric_limits<streamsize>::max(), '\n'); int tax_percent; cin >> tax_percent; cin.ignore(numeric_limits<streamsize>::max(), '\n'); solve(meal_cost, tip_percent, tax_percent); return 0; } Input must be -: 10.25 17 5

25th May 2018, 6:12 PM
gfdgdfgd
2 Respuestas
+ 2
Entering 10.0 for meal_cost and 10 for each tip_percent and tax_percent gives 12 as expected, can you give example for these values where calculation is erratic? You might want to look into using round or ceil function for totalcost, before passing totalcost to int() inside the solve function. http://www.cplusplus.com/reference/cmath/round/ http://www.cplusplus.com/reference/cmath/ceil/ Hth, cmiiw
25th May 2018, 7:24 PM
Ipang
+ 1
Input should be 10.25 17 5
26th May 2018, 10:05 AM
gfdgdfgd