Changing Displayed Text?

This is a discussion on Changing Displayed Text? within the C++ Programming forums, part of the General Programming Boards category; Alright, here's my problem. After messing around with C++, I've came up with this little program: Code: #include <iostream> #include ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    6

    Changing Displayed Text?

    Alright, here's my problem.

    After messing around with C++, I've came up with this little program:

    Code:
    #include <iostream>
    #include <windows.h>
    
    using namespace std;
    
    int main()
    {
        unsigned int secs = 0;
        unsigned int mins = 0;
        unsigned long int hours = 0; //Just in case the person's afk for a looong time :P
        int change = 0;
    
        cout<<"--AFK 1.0--\n\n";
        cout<<"Press [ENTER] key to continue";
        cin.get();
        cout<<"\n\n";
        
        while(change == 0)
        {
              cout<<"\t\nAFK for "<<hours<<" hours "<<mins<<" minutes "<<secs<<" seconds.";
              Sleep(1000);
              secs++;
              
              if (secs >= 60)
              {
                      mins++;
                      secs = 0;
              }
              if (mins >= 60)
              {
                       hours++;
                       mins = 0;
                       
              }
        }
    
        
        return 0;
    }
    Right?

    Well, after Googling the heck out of this, I can't seem to come up with a definite solution aside from putting a
    Code:
    system("CLS");
    above the "AFK for" statement.

    What I mean is, having the seconds, minutes and hours change without having it display everything over and over again.

    Also, if it helps, I am using Dev-C++ to compile all this.

    Thank you very much for your time!

  2. #2
    Registered Abuser Loic's Avatar
    Join Date
    Mar 2007
    Location
    Sydney
    Posts
    115
    Thy this, it should clear the screen every time it goes through the loop

    Code:
              system("cls");
              cout<<"\t\nAFK for "<<hours<<" hours "<<mins<<" minutes "<<secs<<" seconds.";
              Sleep(1000);
              secs++;

  3. #3
    Registered Abuser Loic's Avatar
    Join Date
    Mar 2007
    Location
    Sydney
    Posts
    115
    Or like this?

    Code:
              cout<<"AFK for "<<hours<<" hours "<<mins<<" minutes "<<secs<<" seconds."<<"\r";
              Sleep(1000);
              secs++;
    This one wont clear the screen, just the line containing that counter you have going there

  4. #4
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    Yeah i have to agree with Loic, that's probably you're best option. I know what you want it to do, but i dont know exactly how you would go about doing it. It seems like something a windows app would be better suited for but you're not ready for that yet.

    Maybe someone else can help ya but i have to say that you should try what Loic said.

    Good luck!
    "Anyone can aspire to greatness if they try hard enough."
    - Me

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    6
    Ahh, yes, perfect, that did the trick!

    I can't thank you guys enough that's exactly what I needed, that was driving me crazy!

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    What about leap years, Feburary, leap seconds etc? I'd look into the functions in ctime/time.h, specifically:
    Code:
    time_t start, end;
    double elapsed;
    
    time(&start);
    
    // [...]
    
    time(&end);
    
    elapsed = difftime(&end, &start);
    
    std::cout << "Time elapsed: " << elapsed << 's' << std::endl;
    Click on the functions in the above code to get to function documentation.

    Also see http://cppreference.com/stddate/ctime.html
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. DirectX | Drawing text
    By gavra in forum Game Programming
    Replies: 4
    Last Post: 06-08-2009, 01:23 AM
  2. How to use FTP?
    By maxorator in forum C++ Programming
    Replies: 8
    Last Post: 11-04-2005, 03:17 PM
  3. Small HTML question
    By Thantos in forum Tech Board
    Replies: 4
    Last Post: 12-29-2003, 12:37 AM
  4. changing text around
    By ColdFire in forum Windows Programming
    Replies: 1
    Last Post: 07-17-2002, 12:43 AM
  5. Changing the Console text back to black
    By Unregistered in forum C++ Programming
    Replies: 11
    Last Post: 07-12-2002, 09:11 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21