Dec 27, 2016 at 1:49pm Dec 27, 2016 at 1:49pm UTC
Hello there, this is a program to count the miliseconds from the program starts and also write it in a file and next time when it open to read that number, count the time opened and write it, to infinity, something like the steam play time. And the problem is at line 43, i dont know how to write that number (named 'tt') to a txt file, how to fix it? Thanks in advance!
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
#include <iostream>
#include <stdio.h>
#include <sys\timeb.h>
#include <fstream>
#include <string>
using namespace std;
int main()
{
struct timeb start, end;
int fws, sev, pct, tt;
int diff;
ftime(&start);
while (fws == 0)
{
cout<<"Type '6' to close the program safely: " ;
cin >> sev;
if (sev == 6)
{
cout<<"Closing the program with safe state" <<endl;
fws += 1;
}
else
{
cout<<"Try again!" <<endl;
}
}
ftime(&end);
diff = (int ) (1000.0 * (end.time - start.time) + (end.millitm - start.millitm));
cout<<"Operation took " <<diff<<" milliseconds." <<endl;
cout<<"Adding to the counting ..." <<endl;
std::ifstream myfile("elt.txt" , std::ofstream::trunc); //Overwrite
if (myfile.is_open())
{
tt = pct + diff;
myfile << tt;
myfile.close();
}
else cout << "Unable to open file" ;
return 0;
}
Last edited on Dec 27, 2016 at 1:54pm Dec 27, 2016 at 1:54pm UTC
Dec 27, 2016 at 1:59pm Dec 27, 2016 at 1:59pm UTC
tt = pct + diff;
We know what diff is (line 34) but what is pct? It's a declared (line 12) but uninitialized, unused variable
Dec 27, 2016 at 2:14pm Dec 27, 2016 at 2:14pm UTC
Sorry, that was an older one, heres the new 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
#include <iostream>
#include <stdio.h>
#include <sys\timeb.h>
#include <fstream>
#include <string>
using namespace std;
int main()
{
struct timeb start, end;
int fws, sev, pct, tt;
int diff;
ftime(&start);
while (fws == 0)
{
cout<<"Type '6' to close the program safely: " ;
cin >> sev;
if (sev == 6)
{
cout<<"Closing the program with safe state" <<endl;
fws += 1;
}
else
{
cout<<"Try again!" <<endl;
}
}
ftime(&end);
diff = (int ) (1000.0 * (end.time - start.time) + (end.millitm - start.millitm));
cout<<"Operation took " <<diff<<" milliseconds." <<endl;
cout<<"Adding to the counting ..." <<endl;
std::ifstream myfile("elt.txt" , std::ofstream::trunc);
if (myfile.is_open())
{
while ( getline (myfile,pct) )
{
cout << "Counted time already: " << pct;
}
tt = pct + diff;
myfile << tt;
myfile.close();
}
else cout << "Unable to open file" ;
return 0;
}
pct = proceded counted time (Must read from file) //Idk i like to do weird cuted name for all the variables
diff = time when program opened
tt = total time (Must overwrite when program close on an text file)
EDIT: I see this one has another error, at line 42, why? i dont understand too much that :P but i wanted there to read the number from file and also display it.
Last edited on Dec 27, 2016 at 2:25pm Dec 27, 2016 at 2:25pm UTC
Dec 27, 2016 at 3:36pm Dec 27, 2016 at 3:36pm UTC
you mean i can do this:
myfile >> pct;
?
also, if i change to this, i get the same error like at the first one
there's the code now:
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
#include <iostream>
#include <stdio.h>
#include <sys\timeb.h>
#include <fstream>
#include <string>
using namespace std;
int main()
{
struct timeb start, end;
int fws, sev, pct, tt;
int diff;
ftime(&start);
while (fws == 0)
{
cout<<"Type '6' to close the program safely: " ;
cin >> sev;
if (sev == 6)
{
cout<<"Closing the program with safe state" <<endl;
fws += 1;
}
else {cout<<"Try again!" <<endl;}
}
ftime(&end);
diff = (int ) (1000.0 * (end.time - start.time) + (end.millitm - start.millitm));
cout<<"Operation took " <<diff<<" milliseconds." <<endl;
cout<<"Adding to the counting ..." <<endl;
ifstream myfile("elt.txt" , ofstream::trunc);
if (myfile.is_open())
{
myfile >> pct;
cout << "Counted time already: " << pct;
tt = pct + diff;
myfile << tt;
myfile.close();
}
else cout << "Unable to open file" ;
return 0;
}
the same error (like at the first post), at line 43 this time
EDIT:
by
myfile << tt;
and
ifstream myfile("elt.txt" , ofstream::trunc);
i want to overwrite the file with the tt result
Last edited on Dec 27, 2016 at 4:12pm Dec 27, 2016 at 4:12pm UTC
Dec 27, 2016 at 4:34pm Dec 27, 2016 at 4:34pm UTC
Anyways, i fixed it, it was a problem of ifstream myfile("elt.txt" , ofstream::trunc);