简体   繁体   中英

Visual C++ program exits prematurely

So I have this program and had a couple of errors and I fixed it and it runs fine. However, I will give the program the input and it will produce the output, but then the program will close right away after the output is shown without me having to do anything. I am new to C++ just started learning it after Java so it may be a simple error, thanks for the help in advance. Code is posted below.

#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

After executing your program, C++ exits by default. The usual work-around is to add:

int z;
cin >> z;

Before the return statement in your main function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM