I was looking to get a bit of insight to why my program acts the way it does.
This is the "Day 2" Hacker Rank Challenge.
Essentially it is looking for you to take the inputs of Meal Price, Tip Percent, and Tax Percent to give the correct price as an output.
This code ended up passing all of the test cases that Hacker Rank tested on it but where I run into a problem is when I test the code on my local machine.
btw I use Arch Linux.
The inputs that Hacker Rank uses are 12.00, 20, and 8.
More below code...
#include <bits/stdc++.h>
using namespace std;
double totalCost;
void solve(double meal_cost, double tip_percent, double tax_percent) {
tip_percent = tip_percent / 100;
tip_percent = meal_cost * tip_percent;
tax_percent = tax_percent / 100;
tax_percent = meal_cost * tax_percent;
totalCost = meal_cost + tip_percent + tax_percent;
}
int main(){
double meal_cost;
cin >> meal_cost;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
double tip_percent;
cin >> tip_percent;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
double tax_percent;
cin >> tax_percent;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
solve(meal_cost, tip_percent, tax_percent);
cout << nearbyint(totalCost);
cout << totalCost << endl;
return 0;
}
At first when I ran the code using...
$ echo 12 20 8 | ./a.out
It would give me the correct output of 15 or 15.36 without nearbyint(totalCost).
After that it stopped functioning as such and I had to change to doing...
$ printf "12\n8\n20\n" | ./a.out
Which gives the output
15
15.36
To explore why this was happening I started following what values were being stored in the variables with printf().
#include <bits/stdc++.h>
using namespace std;
double totalCost;
void solve(double meal_cost, double tip_percent, double tax_percent){
tip_percent = tip_percent / 100;
tip_percent = meal_cost * tip_percent;
tax_percent = tax_percent / 100;
tax_percent = meal_cost * tax_percent;
totalCost = meal_cost + tip_percent + tax_percent;
}
int main(){
printf("Enter Meal Cost\n");
double meal_cost;
cin >> meal_cost;
printf("Reprinted Meal Cost\n");
cout << meal_cost << endl;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
printf("Enter Tip Percent\n");
double tip_percent;
cin >> tip_percent;
printf("Reprinted Tip Percent\n");
cout << tip_percent << endl;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
printf("Enter Tax Percent\n");
double tax_percent;
cin >> tax_percent;
printf("Reprinted Tax Percent\n");
cout << tax_percent << endl;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
solve(meal_cost, tip_percent, tax_percent);
printf("Total rounded to nearest int\n");
cout << nearbyint(totalCost) << endl;
printf("Total not rounded\n");
cout << totalCost << endl;
return 0;
}
When I run...
$ echo 12 20 8 | ./a.out
...on the second program I get the output of...
Enter Meal Cost
Reprinted Meal Cost
12
Enter Tip Percent
Reprinted Tip Percent
4.63597e-310
Enter Tax Percent
Reprinted Tax Percent
6.95272e-310
Total rounded to nearest int
12
Total not rounded
12
And when I run...
$ printf "12\n8\n20\n" | ./a.out
...on the second program I get...
Enter Meal Cost
Reprinted Meal Cost
12
Enter Tip Percent
Reprinted Tip Percent
8
Enter Tax Percent
Reprinted Tax Percent
20
Total Rounded to nearest int
15
Total not rounded
15.36
Essentially I'm looking to understand why it was running correctly with echo at first and now it is not.