Reloj en C++

Publicado por Mao
hace 5 años

<pre class="prettyprint prettyprinted"><span class="com">#include</span><span class="pln"> </span><span class="str"><iostream></span><br /><span class="kwd">using</span><span class="pln"> </span><span class="kwd">namespace</span><span class="pln"> std</span><span class="pun">;</span><br /><span class="pln">                      </span><br /><span class="kwd">int</span><span class="pln"> main</span><span class="pun">(){</span><br /><span class="pln">  </span><span class="kwd">int</span><span class="pln"> hora </span><span class="pun">=</span><span class="pln"> </span><span class="lit">0</span><span class="pun">;</span><br /><span class="pln">  </span><span class="kwd">int</span><span class="pln"> minutos </span><span class="pun">=</span><span class="pln"> </span><span class="lit">0</span><span class="pun">;</span><br /><span class="pln">  </span><span class="kwd">int</span><span class="pln"> segundos </span><span class="pun">=</span><span class="pln"> </span><span class="lit">0</span><span class="pun">;</span><br /><span class="pln">                      </span><br /><span class="pln">     </span><span class="kwd">for</span><span class="pln"> </span><span class="pun">(</span><span class="pln">segundos</span><span class="pun">=</span><span class="lit">0</span><span class="pun">,</span><span class="pln"> segundos</span><span class="pun"><=</span><span class="lit">60</span><span class="pun">,</span><span class="pln"> s</span><span class="pun">++)</span><span class="pln"> </span><span class="pun">{</span><br /><span class="pln">       </span><span class="kwd">if</span><span class="pln"> </span><span class="pun">(</span><span class="pln">segundos</span><span class="pun">=</span><span class="lit">60</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><br /><span class="pln">                              </span><br /><span class="pln">       minutos</span><span class="pun">++;</span><span class="pln">    </span><br /><span class="pln">       segundo</span><span class="pun">=</span><span class="lit">0</span><span class="pun">;</span><br /><span class="pln">                         </span><span class="pun">}</span><br /><span class="pln">        </span><span class="kwd">if</span><span class="pln"> </span><span class="pun">(</span><span class="pln">minutos</span><span class="pun">=</span><span class="lit">60</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><br /><span class="pln">                              </span><br /><span class="pln">        hora</span><span class="pun">++;</span><br /><span class="pln">        minutos</span><span class="pun">=</span><span class="lit">0</span><span class="pun">;</span><br /><span class="pln">                         </span><span class="pun">}</span><span class="pln">  </span><br /><span class="pln">         </span><span class="kwd">if</span><span class="pln"> </span><span class="pun">(</span><span class="pln">hora</span><span class="pun">=</span><span class="lit">24</span><span class="pun">)</span><span class="pln">    </span><span class="pun">{</span><br /><span class="pln">                  </span><br /><span class="pln">          segundos</span><span class="pun">=</span><span class="lit">0</span><span class="pun">;</span><br /><span class="pln">          minutos</span><span class="pun">=</span><span class="lit">0</span><span class="pun">;</span><br /><span class="pln">          hora</span><span class="pun">=</span><span class="lit">0</span><span class="pun">;</span><br /><span class="pln">                         </span><span class="pun">}</span><span class="pln">             </span><br /><span class="pln">                   </span><span class="pun">}</span><br /><span class="pln">                      cout</span><span class="pun"><<</span><span class="pln"> </span><span class="str">"hora"</span><span class="pln"> </span><span class="pun"><<</span><span class="pln"> hora </span><span class="pun"><<</span><span class="pln"> </span><span class="str">"minutos"</span><span class="pln"> </span><span class="pun"><<</span><span class="pln"> minutos</span><span class="pun"><<</span><span class="pln"> </span><span class="str">"segundos"</span><span class="pln"> </span><span class="pun"><<</span><span class="pln"> segundos </span><span class="pun"><<</span><span class="pln"> endl</span><span class="pun">;</span><span class="pln"> </span><br /><span class="pln">                         </span><br /><span class="pln">                      </span><span class="kwd">return</span><span class="pln"> </span><span class="lit">0</span><span class="pun">;</span><br /><span class="pln">                             </span><br /><span class="pln">                 </span><span class="pun">}</span></pre>

Respuesta de Agustín Gonzalez
hace 5 años

<p>Sería mejor que detallaras cual es el objetivo del algoritmo.</p>

Respuesta de Mao
hace 5 años

<p>es un reloj...</p><p><br /></p>

Respuesta de Cristian Olaz
hace 5 años

<p>Te marco algunos errores:</p><pre><span class="com">// Dentro del for se usa punto y coma. No solo coma.<br /></span><span class="com">for (segundos=0, segundos<=60, s++) {<br /></span><span class="com">// La variable "s" no existe.<br /></span><span class="kwd">for</span><span class="pln"> </span><span class="pun">(</span><span class="pln">segundos</span><span class="pun">=</span><span class="lit">0</span><span class="pun">,</span><span class="pln"> segundos</span><span class="pun"><=</span><span class="lit">60</span><span class="pun">,</span><span class="pln"> s</span><span class="pun">++)</span><span class="pln"> </span><span class="pun">{<br /></span><span class="com">// Aca no estás comparando. Estás asignando.<br /></span><span class="kwd">if</span><span class="pln"> </span><span class="pun">(</span><span class="pln">segundos</span><span class="pun">=</span><span class="lit">60</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{<br /></span><span class="com">// Supongo que quisiste poner esto<br /></span><span class="kwd">if</span><span class="pun">(</span><span class="pln"> segundos </span><span class="pun">==</span><span class="pln"> </span><span class="lit">60</span><span class="pln"> </span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span></pre><p><br /></p><p>No sé cuántas vuelvas querés que de tu ciclo principal, el de las horas. Pero te pongo aca un ejemplo de un reloj que llega hasta 10 horas.</p><pre class="prettyprint prettyprinted"><span class="kwd">for</span><span class="pun">(</span><span class="pln"> horas </span><span class="pun">=</span><span class="pln"> </span><span class="lit">0</span><span class="pun">;</span><span class="pln"> horas </span><span class="pun"><</span><span class="pln"> </span><span class="lit">10</span><span class="pun">;</span><span class="pln"> horas</span><span class="pun">++</span><span class="pln"> </span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><br /><span class="Apple-tab-span"><span class="pln"> </span></span><span class="kwd">for</span><span class="pun">(</span><span class="pln"> minutos </span><span class="pun">=</span><span class="pln"> </span><span class="lit">0</span><span class="pun">;</span><span class="pln"> minutos </span><span class="pun"><</span><span class="pln"> </span><span class="lit">60</span><span class="pun">;</span><span class="pln"> minutos</span><span class="pun">++</span><span class="pln"> </span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><br /><span class="Apple-tab-span"><span class="pln"> </span></span><span class="Apple-tab-span"><span class="pln"> </span></span><span class="kwd">for</span><span class="pun">(</span><span class="pln"> segundos </span><span class="pun">=</span><span class="pln"> </span><span class="lit">0</span><span class="pun">;</span><span class="pln"> segundos </span><span class="pun"><</span><span class="pln"> </span><span class="lit">60</span><span class="pun">;</span><span class="pln"> segundos</span><span class="pun">++</span><span class="pln"> </span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><br /><span class="Apple-tab-span"><span class="pln"> </span></span><span class="Apple-tab-span"><span class="pln"> </span></span><span class="Apple-tab-span"><span class="pln"> </span></span><span class="pln">cout </span><span class="pun"><<</span><span class="pln"> horas </span><span class="pun"><<</span><span class="pln"> </span><span class="str">":"</span><span class="pln"> </span><span class="pun"><<</span><span class="pln"> minutos </span><span class="pun"><<</span><span class="pln"> </span><span class="str">":"</span><span class="pln"> </span><span class="pun"><<</span><span class="pln"> segundos</span><span class="pun">;</span><br /><span class="Apple-tab-span"><span class="pln"> </span></span><span class="Apple-tab-span"><span class="pln"> </span></span><span class="pun">}</span><br /><span class="Apple-tab-span"><span class="pln"> </span></span><span><span class="pun">}</span><br /></span><span class="pun">}</span></pre>

Respuesta de Lucas
hace 5 años

<p>A mí me parece que tenés que usar alguna biblioteca que se comunique con el sistema operativo para darte la hora. Seguro tendrá alguna función que te devuelve un puntero a estructura con un, por ej., tipo Time.</p><p><br /></p><p><br /></p>