I’ve run into this problem multiple times now and was wondering if someone could explain why it happens? Here I am writing a program that outputs the runtime of different sorting operations to a table. When I add a the “Why does this make it work” cout statement, it works. Without it, it does not and the indentation is thrown off.
cout << "Size Selection Insert Bubble Shell Merge Heapsort Quicksort STL" << endl;
for(int i = 50; i <= 6400; i *= 2) { // Prints table
cout << "Why does this make it work" << endl;
for(int j = 0; j < i; j++) { // Creates random vector
v.push_back(rand());
}
vector<int>& arr = v;
// arr = &v;
cout << setw(4) << i;
start = clock();
SelectionSort(arr);
cout << setw(10) << clock() - start;
start = clock();
InsertionSort(arr);
cout << setw(10) << clock() - start;
start = clock();
BubbleSort(arr);
cout << setw(10) << clock() - start;
start = clock();
ShellSort(arr);
cout << setw(10) << clock() - start;
start = clock();
MergeSort(arr);
cout << setw(10) << clock() - start;
start = clock();
HeapSort(arr);
cout << setw(10) << clock() - start;
start = clock();
QuickSort(arr);
cout << setw(10) << clock() - start;
start = clock();
sort(arr.begin(), arr.end());
cout << setw(10) << clock() - start;
}
As far as I can see, the “Why does this make it work” line is the only one containing an endl. So without the endl the console wraps the line when your text reaches the end of the window (it doesn’t really change lines, it only wraps the current one).
Most likely what looks like a new line contains some remains of the “previous” line and therefore the text doesn’t look as expected.
With the endl you’re changing to the next line and the text really starts at the beginning.