Printf 和 Echo 提供不同的输出

Printf and Echo giving different outputs

本文关键字:输出 Echo Printf      更新时间:2023-10-16

我想深入了解为什么我的程序会这样运行。

这是"第2天"黑客排名挑战赛。

从本质上讲,它希望您输入膳食价格、小费百分比和税收百分比,以给出正确的价格作为输出。

这段代码最终通过了 Hacker Rank 测试的所有测试用例,但我遇到问题的地方是当我在本地机器上测试代码时。

顺便说一句,我使用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(总成本(。

在那之后,它停止了这样的功能,我不得不改为做......

$ printf "12n8n20n" | ./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 Costn");
double meal_cost;
cin >> meal_cost;
printf("Reprinted Meal Costn");
cout << meal_cost << endl;
cin.ignore(numeric_limits<streamsize>::max(), 'n');
printf("Enter Tip Percentn");
double tip_percent;
cin >> tip_percent;
printf("Reprinted Tip Percentn");
cout << tip_percent << endl;
cin.ignore(numeric_limits<streamsize>::max(), 'n');
printf("Enter Tax Percentn");
double tax_percent;
cin >> tax_percent;
printf("Reprinted Tax Percentn");
cout << tax_percent << endl;
cin.ignore(numeric_limits<streamsize>::max(), 'n');
solve(meal_cost, tip_percent, tax_percent);
printf("Total rounded to nearest intn");
cout << nearbyint(totalCost) << endl;
printf("Total not roundedn"); 
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 "12n8n20n" | ./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 正确运行,现在不是。

>Shawn是对的!

我拿出了...

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 "12n20n8n" | ./a.out

。两者的输出是...

15
15.36