Clear variable when re-entering the for c ++

0

Good, how to clean the variables of hour, minutes, and seconds at the end of for to start me in 0 ? thank you I leave my code

#include<iostream>
#include<conio2.h>
#include<stdlib.h>
using namespace std;
main () {
 system ("COLOR E9");
 cout<<("\t\t\t\tPrograma reloj\n\n");
 int hr=0,min=0,seg=0;
 int hora=0,minu=0,segu=0;
 cout<<("Marque el numero para la hora\n\n:");
 cin>>hora;
 cout<<("Marque el numero para el minuto\n\n:");
 cin>>minu;
 cout<<("Marque el numero para el segundo\n\n:");
 cin>>segu;
 for ( hr=hora ; hr<12 ; hr++){
        for ( min=minu ; min<60 ; min++){
              for ( seg=segu; seg<60; seg++){
                     for ( int mls=0 ; mls<1100 ; mls++){
                     gotoxy (35,7);
                     cout << hr <<" : " << min <<" : "<< seg <<endl;
                     }
              }
        }
 }

 getch();
}
    
asked by Rigoberto Oviedo Bolaños 07.04.2017 в 18:18
source

3 answers

2

For the account to start over, your code would be such that:

 #include<iostream>
 #include<conio.h>
 #include<stdlib.h>
 #include <unistd.h>
 using namespace std;

 int main ()
 {
     system ("COLOR E9");
     cout<<("\t\t\t\tPrograma reloj\n\n");
     int hr=0,min=0,seg=0;
     int hora=0,minu=0,segu=0;

     cout<<("Marque el numero para la hora: ");
     cin>>hora;
     cout<<("\nMarque el numero para el minuto: ");
     cin>>minu;
     cout<<("\nMarque el numero para el segundo: ");
     cin>>segu;

 for ( hr=hora ; hr<=12 ; hr++)
 {
    hora=0;
     for ( min=minu ; min<60 ; min++)
     {
        minu=0;
          for ( seg=segu; seg<=60; seg++)
          {
            gotoxy(35,7);
            system("cls");
            cout << hr <<" : " << min <<" : "<< seg <<endl;
            sleep(1);
            segu=0;
      }
  }
 }
    getch();
 }

I have added the system ("cls"); to clean the console screen after every second, plus a sleep (); with which the program waits 1 second to continue with its process, thus saving the milli seconds loop that you had.

    
answered by 07.04.2017 в 19:23
1

You only have to reset to 0 after each cycle your control variable for the counter, like this:

system("COLOR E9");
cout << ("\t\t\t\tPrograma reloj\n\n");
int hr = 0, min = 0, seg = 0;
int hora = 0, minu = 0, segu = 0;
cout << ("Marque el numero para la hora\n\n:");
cin >> hora;
cout << ("Marque el numero para el minuto\n\n:");
cin >> minu;
cout << ("Marque el numero para el segundo\n\n:");
cin >> segu;
for (hr = hora; hr<12; hr++){
    for (min = minu; min<60; min++){
        for (seg = segu; seg<60; seg++){
            for (int mls = 0; mls<1100; mls++){

                cout << hr << " : " << min << " : " << seg << endl;
            }
        }
        segu = 0;

    }
    minu = 0;
}
    
answered by 08.04.2017 в 00:38
0

So my friend , if you have any questions, you tell me

#include<iostream>
#include<conio2.h>
#include<stdlib.h>
using namespace std;
main () {
    system ("COLOR E9");
    cout<<("\t\t\t\tPrograma reloj\n\n");
    int hr=0,min=0,seg=0;
    int hora=0,minu=0,segu=0;
    cout<<("Marque el numero para la hora\n\n:");
    cin>>hora;
    cout<<("Marque el numero para el minuto\n\n:");
    cin>>minu;
    cout<<("Marque el numero para el segundo\n\n:");
    cin>>segu;
    for ( hr=hora ; hr<12 ; hr++){
        for ( min=minu ; min<60 ; min++){
            for ( seg=segu; seg<60; seg++){
                for ( int mls=0 ; mls<1100 ; mls++){
                    gotoxy (35,7);
                    cout << hr <<" : " << min <<" : "<< seg <<endl;
                }
                segu = 0;
            }
            minu = 0;
        }
        hora = 0;
    }

    getch();
}
    
answered by 10.04.2017 в 08:56