Printf和Echo提供不同的输出

问题描述 投票:0回答:1

我希望对我的程序为何以这种方式起作用有所了解。

这是“第2天”黑客等级挑战。

本质上,它正在寻找您采用餐食价格,小费百分比和税收百分比的输入,以提供正确的价格作为输出。

此代码最终通过了Hacker Rank测试过的所有测试用例,但遇到问题的是在本地计算机上测试该代码。

btw我使用Arch Linux。

黑客等级使用的输入是12.00、20和8。

更多下面的代码...

#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;

}

起初,我使用...运行代码时

$ echo 12 20 8 | ./a.out

它会给我正确的输出15或15.36,而没有nearint(totalCost)。

此后它停止了运行,因此我不得不更改为...

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

哪个给出输出

15
15.36

为了探讨为什么会发生这种情况,我开始关注通过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;
}

我跑步时...

$ echo 12 20 8 | ./a.out

...在第二个程序中,我得到...的输出

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

当我跑步...

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

...在第二个程序中,我得到...

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

本质上来说,我想了解的是为什么它最初以echo正确运行,而现在却没有。

c++
1个回答
0
投票

肖恩是对的!

我拿出...

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

这是我对代码所做的更改...

#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;

}

现在我是否要跑步...

$ echo 12 20 8 | ./a.out

或...

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

...两者的输出都是...

15
15.36
© www.soinside.com 2019 - 2024. All rights reserved.