首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C++控制台窗口闪烁,然后消失

C++控制台窗口闪烁,然后消失
EN

Stack Overflow用户
提问于 2012-11-22 23:19:36
回答 2查看 338关注 0票数 0

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

代码语言:javascript
复制
#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;
 }
}
EN

回答 2

Stack Overflow用户

发布于 2012-11-22 23:28:28

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

添加一行从用户获取输入,窗口将保持打开状态,直到输入被提供。就像“按任意键关闭”一样。

如果您从Command Line启动您的应用程序,您将看到当执行结束时,控制权立即交还给Command Line,这就是关闭窗口的原因,您的过程就完成了。

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

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

代码语言:javascript
复制
#include <conio.h>

在开始的时候,然后,在你的最后一个}之前

代码语言:javascript
复制
_getch(); // getch() might be deprecated with your compiler
票数 4
EN

Stack Overflow用户

发布于 2012-11-22 23:34:45

您可以在应用程序的末尾使用:

代码语言:javascript
复制
char x;
cin >> x;

或者使用conio.h中的旧C函数getch()

程序会等待你按任意键

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13515716

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档