[英]Printf and Echo giving different outputs
我想了解一下我的程序為什么會這樣。
這是“第 2 天”黑客排名挑戰賽。
本質上,它是在尋找您輸入膳食價格、小費百分比和稅收百分比的輸入,以給出正確的價格作為輸出。
這段代碼最終通過了 Hacker Rank 在其上測試的所有測試用例,但我遇到問題的地方是當我在本地機器上測試代碼時。
順便說一句,我使用 Arch Linux。
Hacker Rank 使用的輸入是 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 正確運行,而現在卻不是。
肖恩是對的!
我拿出了...
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.