2012-11-22 74 views
0

好吧,這是我製作的銷售稅計算器,控制檯窗口閃爍然後消失。只是想知道我做錯了什麼。此外,我覺得像代碼本身嵌入了單元測試,但我想知道單元測試如何應用於這些參數。C++控制檯窗口閃爍然後消失

#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; 
} 
} 
+1

您從vs20xx中調試運行?嘗試Ctrl + F5它會在關閉之前暫停(即使它崩潰)或在main中的返回中放置一個斷點。 – Caribou

+1

從命令行運行它也起作用。 – Rook

+0

我希望它只是通過點擊它來作爲可執行文件運行。並保持開放直到用戶想要關閉它。 –

回答

4

通過「閃爍」我想你指的是Console彈出,您的應用程序運行,然後它會自動關閉。這是正常的行爲。

添加一行以從用戶獲取輸入,並且窗口將保持打開狀態,直到給出輸入。就像「按任意鍵關閉」一樣。

如果您從Command Line啓動您的應用程序,您會看到當執行結束時,控制立即返回到Command Line,這就是讓窗口關閉,您的過程完成。

如果您只想在關閉應用程序之前查看輸出內容,建議的使用斷點的技巧也是可行的。

您所要求的代碼示例,最簡單的很可能是

#include <conio.h> 

在開始的時候,然後,在你最後}

_getch(); // getch() might be deprecated with your compiler 
+0

我需要一個如何在我的代碼看起來的例子 –

+0

@MichaelReynolds你在這裏有很多例子:http://www.cplusplus.com/articles/iw6AC542/對於簡單的例子,我通常會使用'getch();' – emartel

1

你可以在年底使用您的應用程序:

char x; 
cin >> x; 

或使用舊的C函數getch()from conio.h
程序將等待您按任意鍵