Note that there are some explanatory texts on larger screens.

plurals
  1. POLogical error when modifying a time program in c++
    primarykey
    data
    text
    <p>This program is supposed to store time in seconds since midnight and display it in standard and universal time. It runs, but the set Time function has an error in it, as the time never changes. I'm assuming that something isn't being returned right, but I can't find the error.</p> <p>Header file :</p> <pre><code>#ifndef TIME_H #define TIME_H class Time { public: Time(); //constructor void setTime(int, int, int ); void printUniversal(); //print time in universal-time format void printStandard(); // print time in standard-time format private: int secondsSinceMidnight; }; #endif </code></pre> <p>.cpp file</p> <pre><code>Time::Time()//constructor { secondsSinceMidnight = 0; } void Time::setTime(int h, int m, int s) { if ((h &gt;= 0 &amp;&amp; h &lt; 24) &amp;&amp; (m &gt;= 0 &amp;&amp; m &lt; 60) &amp;&amp; (s &gt;= 0) &amp;&amp; (s &lt; 60)) { int hoursInSecs = (h * 3600); int minutesInSecs = (m * 60); secondsSinceMidnight = (minutesInSecs + hoursInSecs); } else throw invalid_argument( "hour, minute and/or second was out of range"); } void Time::printUniversal() { int secondsSinceMidnight = 0; int hours = (secondsSinceMidnight / 3600); int remainder = (secondsSinceMidnight % 3600); int minutes = (remainder / 60); int seconds = (remainder % 60); cout &lt;&lt;setfill('0')&lt;&lt;setw(2)&lt;&lt;hours&lt;&lt;":" &lt;&lt;setw(2)&lt;&lt;minutes&lt;&lt;":"&lt;&lt;setw(2)&lt;&lt;seconds&lt;&lt;endl; } void Time::printStandard() { int secondsSinceMidnight = 0; int hours = (secondsSinceMidnight / 3600); int remainder = (secondsSinceMidnight % 3600); int minutes = (remainder / 60); int seconds = (remainder % 60); cout&lt;&lt;((hours == 0 || hours == 12) ? 12 : hours % 12) &lt;&lt; ":" &lt;&lt; setfill('0') &lt;&lt;setw(2)&lt;&lt;minutes&lt;&lt; ":"&lt;&lt;setw(2) &lt;&lt;seconds&lt;&lt;(hours &lt; 12 ? "AM" : "PM")&lt;&lt;"\n"; } </code></pre> <p>And the main program :</p> <pre><code>int main() { Time t; //instantiate object t of class Time //output Time object t's initial values cout&lt;&lt;"The initial universal time is "; t.printUniversal(); cout&lt;&lt;"\nThe initial standard time is "; t.printStandard(); int h; int m; int s; cout&lt;&lt;"\nEnter the hours, minutes and seconds to reset the time: "&lt;&lt;endl; cin&gt;&gt;h&gt;&gt;m&gt;&gt;s; t.setTime(h, m, s); //change time //output Time object t's new values cout&lt;&lt;"\n\nUniversal time after setTime is "; t.printUniversal(); cout&lt;&lt;"\nStandard time after setTime is "; t.printStandard(); } </code></pre>
    singulars
    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.
 

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