Visual C++程序提前退出

Visual C++ program exits prematurely

本文关键字:退出 程序 C++ Visual      更新时间:2023-10-16

所以我有这个程序,有几个错误,我修复了它,它运行得很好。然而,我会给程序输入,它会产生输出,但在输出显示后,程序会立即关闭,而我不必做任何事情。我是C++的新手,刚开始学习Java,所以这可能是一个简单的错误,感谢您的提前帮助。代码张贴在下面。

#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 << "nnEnter 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 << "nn";
        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 << "nn";
        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

执行完程序后,C++默认退出。通常的解决方法是添加:

int z;
cin >> z;

在主函数中的return语句之前。