So I was trying to write a function that prints out a string letter by letter with a 100 ms time interval between each letter. (Please don't ask me why). This is what I have written so far.
#include <iostream>
#include <time.h>
void wait(int ms){
clock_t begin = clock(), end;
double t;
while(true){
end = clock();
t=double(end - begin) / CLOCKS_PER_SEC;
if(t>=(double) ms/1000)
break;
}
return;
}
void display_dramatically(std::string x) {
for(int i=0;i<x.size();++i) {
std::cout<<x[i]<<"\n";
wait(100);
}
}
What's extremely weird is that it is only when I print a "\n" after x[i] that this function works. But of course then every character is also in a new line. When I remove that "\n", the program just waits for 100*(number of characters in string) milliseconds, and then prints out the whole string at once. This does not make any sense to me. Even the wait function is working perfectly. How can the presence of a newline command affect the program in this way?