2015-12-15 74 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

非常感谢。这么简单但很令人沮丧的哈哈。 –