2015-12-15 75 views
0

所以我有這個程序,並有一些錯誤,我修好了,它運行良好。但是,我會給程序輸入,它會產生輸出,但是程序會在顯示輸出後馬上關閉,而不需要我做任何事情。我是C++的新手,剛剛開始學習Java,因此可能是一個簡單的錯誤,感謝提前的幫助。代碼如下。Visual C++程序過早退出

#include <iostream> 
#include <string> 
#include <iomanip> 

using namespace std; 

//Global Declarations of Variables 
double iovertime_hours = 0, iovertime_pay = 0, iovertime_extra = 0; 
int ihours, iwage; 
string cname; 

int main() 
{ 
    //Enter Employee Information 
    cout << "\n\nEnter the employee name = "; 
    cin >> cname; 
    cout << "Enter the hours worked = "; 
    cin >> ihours; 
    cout << "Enter his or her hourly wage = "; 
    cin >> iwage; 

    // Determine if hours are greater than 40 
    if (ihours < 40) 
    { 
     //Do Calculations 
     iovertime_hours = ihours + 40; 
     iovertime_pay = iwage - 1.5; 
     iovertime_extra = iovertime_hours*iovertime_pay; 

     // Display Employee Details 
     cout << "\n\n"; 
     cout << "Employee Name ............. = " << cname << endl; 
      cout << "Base Pay .................. = " << iwage * 40 << endl; 
     cout << "Hours in Overtime ......... = " << iovertime_hours << endl; 
     cout << "Overtime Pay Amout......... = " << iovertime_extra << endl; 
     cout << "Total Pay ................. = " << iovertime_extra+(40*iwage) << endl; 
    } 
    else // Else hours are less than 40 hours 
    { 
     cout << "\n\n"; 
     cout << "Employee Name ............. = " << cname << endl; 
     cout << "Base Pay .................. = " << iwage*40 << endl; 
      cout << "Hours in Overtime ......... = " << iovertime_hours << endl; 
     cout << "Overtime Pay Amout......... = " << iovertime_extra << endl; 
     cout << "Total Pay ................. = " << iovertime_extra + (40 * iwage) << endl; 
    } // End of the primary if statement 

    return 0; 
} //End of Int Main 
+1

如果你在Windows上開發,請看這裏:https://stackoverflow.com/questions/2529617/how-to-stop-c-console-application-from-exiting-immediately否則請說出你是哪個平臺因爲解決方案可能會有所不同。 –

回答

1

執行程序後,C++默認退出。通常的解決方法是添加:

int z; 
cin >> z; 

在主函數返回語句之前。

+1

非常感謝。這麼簡單但很令人沮喪的哈哈。 –