好的,这是我制作的销售税计算器,控制台窗口闪烁然后消失。只是想知道我做错了什么。另外我觉得单元测试嵌入在代码本身中,但我想知道单元测试如何应用于这些参数。
#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;
}
}