calebyg
calebyg

Reputation: 1

C++ console application window exiting too early, but not immediately (VS 2017)

I've decided to make a sandbox application that will help me practice more with vectors, and the key output of the program is only displayed in the blink of an eye. The window itself is not exiting immediately as I've seen other people dealing with that issue. My program computes an average of n amount of vector int values, which will then be displayed.

#include <iostream>
#include <vector>
using namespace std;

double avgVector(vector<int>);

int main()
{
vector<int> values;
int numValues;
double average;

cout << "How many values do you wish to average? ";
cin >> numValues;

for (int count = 0; count < numValues; count++)
{
    int tempValue;

    cout << "Enter an integer value: ";
    cin >> tempValue;
    values.push_back(tempValue);
}
average = avgVector(values);
cout << "Average: " << average << endl;
return 0;
}

double avgVector(vector<int> vect)
{
int total = 0;
double avg = 0.0;

if (vect.empty())
    cout << "No values to average.\n";
else
{
    for (int count = 0; count < vect.size(); count++)
        total += vect[count];
    avg = static_cast<double>(total) / vect.size();
}
return avg;

}

The message "Average:" + the average value is displayed in a blink of an eye, and I've tried to include character-capturing functions such as std::cin.get() and std::getChar()

Upvotes: 0

Views: 85

Answers (2)

Li Kui
Li Kui

Reputation: 680

Properties -> Linker -> System -> SubSystem

Console (/SUBSYSTEM:CONSOLE)

enter image description here

Press Ctrl + F5 instead, then you will see below: enter image description here

Upvotes: 0

wallyk
wallyk

Reputation: 57784

Run the program in a command line window. That way, after it exits, the output persists.

Upvotes: 0

Related Questions