0

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

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

2 回答 2

4

通过“闪烁”,我假设您的意思是Console弹出,您的应用程序运行,然后自动关闭。这是正常行为。

添加一行以从用户那里获取输入,并且窗口将保持打开状态,直到给出输入。就像“按任意键关闭”。

如果您从 启动应用程序Command Line,您会看到执行结束后,控制Command Line权立即返回,这就是关闭窗口的原因,您的过程已完成。

如果您只想在关闭应用程序之前查看输出的内容,使用断点的建议技巧也是可行的。

您正在要求一个代码示例,最简单的可能是

#include <conio.h>

在开始,然后,在你最后一次之前}

_getch(); // getch() might be deprecated with your compiler
于 2012-11-22T15:28:28.437 回答
1

您可以在应用程序结束时使用:

char x;
cin >> x;

或使用 conio.h
程序中的旧 C 函数 getch() 将等待您按下任何键

于 2012-11-22T15:34:45.017 回答