これが私が作成した消費税計算機で、コンソール ウィンドウが点滅して消えます。私が何を間違えたのか疑問に思っています。また、コード自体に単体テストが埋め込まれているような気がしますが、単体テストがこれらのようなパラメーターにどのように適用されるのか疑問に思っていました。
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <math.h>
using namespace std;
void p(double x)
{
cout << fixed << setprecision(2) << x;
}
int main()
{
ifstream basketFile;
basketFile.open("basket.txt");
int howMany;
double price;
double salesTax = 0;
double total = 0;
bool correct = true;
string printIt;
string second;
string garbage1;
string garbage2;
string garbage3;
string garbage4;
string whichImported;
while(!basketFile.eof())
{
//how many of the specific item do you have?
basketFile >> howMany;
//what is the item?
basketFile >> printIt;
cout << howMany << " ";
if(printIt == "book")
{
basketFile >> garbage1; //throw away "at"
basketFile >> price; //get price of book
total += price;
cout << printIt;
cout << " ";
cout << garbage1;
cout << " ";
p(price);
cout << endl;
}
else if(printIt == "music")
{
basketFile >> garbage1; //throw away "CD"
basketFile >> garbage2; //throw away "at"
basketFile >> price;
salesTax = ((10)*price)/100;
price += salesTax;
total += price;
cout << printIt;
cout << " ";
cout << garbage1;
cout << " ";
cout << garbage2;
cout << " ";
p(price);
cout << endl;
}
else if(printIt == "chocolate")
{
basketFile >> garbage1;
basketFile >> garbage2;
basketFile >> price;
cout << printIt;
cout << " ";
cout << garbage1;
cout << " ";
cout << garbage2;
cout << " ";
p(price);
cout << endl;
}
else if(printIt == "imported")
{
basketFile >> second;
if(second == "box")
{
basketFile >> garbage1;
basketFile >> garbage2;
basketFile >> garbage3;
basketFile >> price;
cout << printIt;
cout << " ";
cout << second;
cout << " ";
cout << garbage1;
cout << " ";
cout << garbage2;
cout << " ";
cout << garbage3;
cout << " ";
p(price);
cout << endl;
salesTax += (5)*(price)/100;
total += price;
}
else
{
basketFile >> garbage1;
basketFile >> garbage2;
basketFile >> garbage3;
basketFile >> price;
cout << printIt;
cout << " ";
cout << second;
cout << " ";
cout << garbage1;
cout << " ";
cout << garbage2;
cout << " ";
cout << garbage3;
cout << " ";
p(price);
cout << endl;
salesTax += ((15)*price)/100;
total += price;
}
}
else if(printIt == "packet")
{
basketFile >> garbage1;
basketFile >> garbage2;
basketFile >> garbage3;
basketFile >> garbage4;
basketFile >> price;
cout << printIt;
cout << " ";
cout << garbage1;
cout << " ";
cout << garbage2;
cout << " ";
cout << garbage3;
cout << " ";
cout << garbage4;
cout << " ";
p(price);
cout << endl;
total += price;
}
else if(printIt == "bottle")
{
basketFile >> garbage1;
basketFile >> garbage2;
basketFile >> garbage3;
basketFile >> price;
cout << printIt;
cout << " ";
cout << garbage1;
cout << " ";
cout << garbage2;
cout << " ";
cout << garbage3;
cout << " ";
p(price);
cout << endl;
salesTax += (10)*(price)/100;
total += price;
}
else
{
cout << "\nIncorrect parameters." << endl;
correct = false;
break;
}
}
if(correct)
{
total += salesTax;
cout << "Sales Taxes: ";
printf("%.1f",salesTax);
cout << 0 << endl;
cout << "Total: ";
p(total);
cout << endl;
}
else
{
return 0;
}
}