need output on one line
Jul 3, 2015 at 3:19pm UTC
I'm using aide for android to code c++ on my tablet. I'm making a download progress bar for my game. It will output the characters on one line but I have to wait until the simulated download is fully complete and it spits the whole thing out with no pause. If I output it line by line it works like it should.
I want to have a horizontal progress bar not a vertical one.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
//Functions
int msleep(unsigned long milisec)
{
struct timespec req={0};
time_t sec=(int )(milisec/1000);
milisec=milisec-(sec*1000);
req.tv_sec=sec;
req.tv_nsec=milisec*1000000L;
while (nanosleep(&req,&req)==-1)
continue ;
return 1;
}
int main()
{
string line;
int fileSize = 0;
float result = 0;
int downloadTime = 0;
int downloadTime1 = 0;
int hour = 0;
int minute = 0;
int second = 0;
fileSize = 250000; //Set file size
result = (fileSize / 3500); //Set download bps
//Convert seconds into seconds, minutes, hours
if (result < 60)
{
cout << "ETA - " << result << " second(s)" << endl;
}
if (result >= 60)
{
downloadTime = result / 60;
if (downloadTime < 60)
{
downloadTime1 = result - (downloadTime * 60);
cout << "ETA - " << downloadTime << " minute(s) " << downloadTime1 << " second(s)" << endl;
}
if (downloadTime > 60)
{
downloadTime1 = result / 60;
hour = downloadTime1 / 60;
minute = downloadTime1 - (hour * 60);
second = result - (downloadTime1 * 60);
cout << "ETA - " << hour << " hour(s) " << minute << " minute(s) " << second << " second(s)" << endl;
}
}
//Start download progress
cout << "" << endl;
cout << " -- Download Started --" << endl;
result = 1000 * (result / 8); //Convert result to miliseconds
ifstream com_file ("/storage/emulated/0/MyGame/downloadProgress/jni/progressBar.txt" );
if (com_file.is_open())
{
for (int i=0; !com_file.eof(); i++)
{
getline (com_file,line);
msleep(result);
cout << line << endl; //Remove endl here and it waits till the end and didn't pause for each character.
}
}
com_file.close();
cout << "" << endl;
cout << " -- Download Complete --" << endl;
}
Jul 3, 2015 at 3:23pm UTC
Flush buffer explicitely. std::cout << line << std::flush;
Jul 3, 2015 at 3:29pm UTC
Works thanks.
Topic archived. No new replies allowed.