0

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.

  • 1
    I'm guessing that when it was working with echo, you didn't have all those `ignore()` calls? You should always check input operations to make sure they succeeded before trying to use values they read. – Shawn Dec 30 '19 at 02:26
  • That is correct! To clarify that part was Hacker Ranks code and I didn't add those in. – flowingisart Dec 30 '19 at 02:33
  • Echo doesn't put a newline between each argument... – Shawn Dec 30 '19 at 02:37
  • How do I mark this as solved? and upvote your answer so you get credit? – flowingisart Dec 30 '19 at 02:42
  • I understand that Echo doesn't put new lines and that with printf /n is creating line breaks. – flowingisart Dec 30 '19 at 02:43
  • Please, never, never use those first two lines of that code in any production. It makes code reviewer a *sad panda* – Swift - Friday Pie Dec 30 '19 at 02:44
  • Why does it make a code reviewer a sad panda? D:? – flowingisart Dec 30 '19 at 02:46
  • 2
    @flowingisart [See this about that header file](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) – PaulMcKenzie Dec 30 '19 at 02:52
  • Please, don't answer in question. FYI: [Can I answer my own question?](https://stackoverflow.com/help/self-answer). Btw. that's the correct way to mark question as solved. Btw. btw. answers may be upvoted to give credits. Comments may be upvoted to support them - no credits (but every commenter does know this). And, in general, comments are not intended to give answers but may be used to give hints or ask for details. – Scheff's Cat Dec 30 '19 at 06:41

1 Answers1

0

Shawn was correct!

I took out the...

cin.ignore(numeric_limits<streamsize>::max(), '\n');

Here are the changes I made to my 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;

      double tip_percent;
      cin >> tip_percent;

      double tax_percent;
      cin >> tax_percent;

      solve(meal_cost, tip_percent, tax_percent);

      cout << nearbyint(totalCost) << endl;
      cout << totalCost << endl;

      return 0;

}

Now whether I run...

$ echo 12 20 8 | ./a.out

or...

$ printf "12\n20\n8\n" | ./a.out

...the output for both is...

15
15.36