Note that there are some explanatory texts on larger screens.

plurals
  1. POReading time entries from a text file and storing them into a 3D array so they are useable, in C++
    primarykey
    data
    text
    <p>I am currently trying to convert a program I have from PHP into C++ and improve it, however I am having trouble figuring out how to read the data so it is useable.</p> <p>The text file looks something like this:</p> <pre><code>No. , user No., date, time 1 , 1, 2013-12-12, 08:00 2 , 2, 2013-12-12, 08:10 3 , 1, 2013-12-12, 12:00 4 , 2, 2013-12-12, 12:01 5 , 2, 2013-12-12, 12:55 6 , 1, 2013-12-12, 13:00 7 , 1, 2013-12-12, 17:00 8 , 2, 2013-12-12, 17:20 9 , 1, 2013-12-13, 08:00 10 , 2, 2013-12-13, 08:10 11 , 1, 2013-12-13, 12:00 12 , 2, 2013-12-13, 12:01 13 , 2, 2013-12-13, 12:55 14 , 1, 2013-12-13, 13:00 15 , 1, 2013-12-13, 17:00 16 , 2, 2013-12-13, 17:20 </code></pre> <p>My goal is to read the text file, and do calculations with the time entries, determining total time worked that day, overtime that day, and total time and overtime over the month.</p> <p>So my question would be how to read the text file, so I can use the date and time entries.</p> <p>EDIT. Here is what my code currently looks like. I am not very experienced with C++ itself, which is why I am remaking a program I allready made previously, with a language I am more familiar with.</p> <pre><code> #include &lt;iostream&gt; #include &lt;fstream&gt; #include &lt;string&gt; using namespace std; int lines() { int number_of_lines = 0; std::string line; std::ifstream myfile("data.txt"); while (std::getline(myfile, line)) ++number_of_lines; std::cout &lt;&lt; "Number of lines in text file: " &lt;&lt; number_of_lines &lt;&lt; endl; return number_of_lines; } int main () { int Nr; int name; int year; int month; int day; int hour; int min; int seconds; int row = lines(); int myarray[16][8]; string line; ifstream myfile ("data.txt"); if (myfile.is_open()) { for(int i=0; i&lt;row; i++){ getline (myfile,line); sscanf(line.c_str(), "%i, %i, %i-%i-%i, %i:%i", &amp;Nr, &amp;name, &amp;year, &amp;month, &amp;day, &amp;hour, &amp;min); seconds=hour*60*60+min*60; myarray[i][1] = Nr; myarray[i][2] = name; myarray[i][3] = year; myarray[i][4] = month; myarray[i][5] = day; myarray[i][6] = hour; myarray[i][7] = min; myarray[i][8]=seconds; } myfile.close(); for(int i=0; i&lt;16; i++){ for( int j=1; j&lt;=8; j++){ cout &lt;&lt; myarray[i][j] &lt;&lt; " "; } cout &lt;&lt; endl; }} else cout &lt;&lt; "Unable to open file"; system("PAUSE"); return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload