Read Files?
Jul 4, 2012 at 8:04pm UTC
I am creating a program that calculates day, time, and month, year, as well as minutes using C++. The Seconds are supposed to be pulled out of a text file. For some Reason the Seconds aren't Being Recorded from the text File! I'm doing this as a bit of practice after finishing chapters 1-10 in Accelerated C++ 2000.
Here is the Struct Header:
1 2 3 4 5 6 7 8 9 10 11 12
#ifndef GUARD_TIME
#define GUARD_TIME
struct Time
{
double minutes_fin;
double hours_fin;
double days_fin;
double weeks_fin;
double months_fin;
double years_fin;
};
#endif
Here is the File Section of the code...
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 76 77 78 79 80 81 82 83 84 85 86 87 88
#include <iostream>
#include <fstream>
#include "Time.h"
using namespace std;
long double seconds;
Time time;
long double print (long double seconds)
{
cout << endl;
cout << "According to your file: " << seconds << " Seconds is equal to: "
<< time.minutes_fin << " Minutes." << endl;
cout << "And is also equal to: " << time.hours_fin << " Hours or "
<< time.days_fin << " Days!" << endl;
cout << "And also equal to: " << time.weeks_fin << " Weeks as well! Even "
<< time.months_fin << " Months! Finally " << time.years_fin <<
" Years!" ;
return seconds;
}
long double read_file (long double seconds)
{
fstream txt_file;
txt_file.open("seconds.txt" );
if (txt_file.is_open())
{
while (!txt_file.eof())
{
txt_file >> seconds;
}
}
else
{
cout << "File Not Found" ;
cin.ignore();
return 1;
}
txt_file.close();
return seconds;
}
long double sec_to_minutes (long double seconds)
{
time.minutes_fin = seconds / 60;
return time.minutes_fin;
}
void sec_to_hours ()
{
time.hours_fin = time.minutes_fin / 60;
}
void sec_to_days ()
{
time.days_fin = time.hours_fin / 24;
}
void sec_to_weeks ()
{
time.weeks_fin = time.days_fin / 7;
}
long double sec_to_months (long double seconds)
{
time.months_fin = seconds / 2629743.83;
return time.months_fin;
}
void sec_to_years ()
{
time.years_fin = time.days_fin / 365.25;
}
int main ()
{
read_file(seconds);
sec_to_minutes (seconds);
sec_to_hours ();
sec_to_days ();
sec_to_weeks ();
sec_to_months (seconds);
sec_to_years ();
print (seconds);
cin.ignore();
return 0;
}
Any answers would be highly appreciated! (Also what is an SO? (I assume something with OS's?))
Last edited on Jul 4, 2012 at 9:41pm UTC
Jul 4, 2012 at 8:10pm UTC
long double read_file(seconds);
What exactly is that doing? Likewise the following lines.
Jul 4, 2012 at 8:37pm UTC
It calls its corresponding function and gives the parameter seconds.
Jul 4, 2012 at 8:40pm UTC
I'm an idiot thanks @ moschops
Fixed everything but file being read
Still receiving 0 for my seconds variable....
Last edited on Jul 4, 2012 at 8:50pm UTC
Jul 5, 2012 at 8:49am UTC
On line 29 you permanently keep overwriting seconds
.
The stream determines an error (such as eof) after the read operation is done. In case of an error the provided value is left empty/0. So the check for eof on line 27 comes too late. The damage is done i.e. seconds
is already 0.
Topic archived. No new replies allowed.