0

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()

  • 4
    If you select run(Ctrl+F5) instead of Debug(F5) console should stay open till you close it manually . If you want to Debug - add breakpoint (F9) at the last line of main() – Artemy Vysotsky Sep 20 '17 at 03:55
  • Since you don't have anything that would stop the program from exiting in your example, it's hard to know if any of your attempts were correct, or if incorrect, how they could be corrected. In general, you want to ignore any remaining data in the input buffer before you wait for 1 more character. FWIW I either put a breakpoint on the last line, or run it outside the debugger, whichever is more appropriate. – Retired Ninja Sep 20 '17 at 03:58
  • @ArtemyVysotsky I've tried running the program through Ctrl+F5 but the same situation arises, unfortunately. I'm not too sure what the issue is. – calebyg Sep 20 '17 at 04:10
  • https://stackoverflow.com/a/1775870/8491726 - see 1st comment to the answer – Artemy Vysotsky Sep 20 '17 at 04:16
  • @RetiredNinja My only attempts were just adding `cin.get()` and `getChar()` in the main function right before the `return 0` statement (on separate occasions). Neither of those have made any changes to the program – calebyg Sep 20 '17 at 04:17
  • @ArtemyVysotsky I'm not sure exactly where to put the break point in my program. 'last line' seems really vague, and I'm not sure why I'd suspend any execution of my program when I just need everything after the `for` loop to persist – calebyg Sep 20 '17 at 04:27
  • I believe you, however since I cannot see that code I can't give you advice about it. Assuming you have a single return from main, then placing the breakpoint there is fine, you don't care what's in scope at the time, you just want to keep the console open. – Retired Ninja Sep 20 '17 at 04:35
  • Place this code before return 0 in main: ::system("pause"); – Asesh Sep 20 '17 at 04:43
  • 1
    @Asesh Thank you! I tried this and it worked. And to Retired Ninja, I also tried that method but the program took me to a debugging process and didn't pause the screen on the second try. I'm pretty sure it's because I messed up on something but I'll try it again. – calebyg Sep 20 '17 at 08:45

2 Answers2

0

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

wallyk
  • 56,922
  • 16
  • 83
  • 148
0

Properties -> Linker -> System -> SubSystem

Console (/SUBSYSTEM:CONSOLE)

enter image description here

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

frogatto
  • 28,539
  • 11
  • 83
  • 129
Li Kui
  • 640
  • 9
  • 21
  • I've tried that many times. The issue is that the console window terminates after I've iterated through the loop of populating the vector with values. The average is displayed after the for loop ceases, but for only half a second. – calebyg Sep 20 '17 at 04:33