Hi folks. I need a way to display a counter in seconds until it reaches a user defined limit. I have tried but unfortunately not having much luck.

#include <cstdlib>
#include <iostream>

#include <ctime> // time header

using namespace std;

int main()
{
    cout << "Please enter seconds: ";//prompt for user
    int timer; //int for user input
    cin >> timer; //user input
    timer = clock();
    while ( (timer / 1000) < 60) // While the program hasn't been going for 60 second
    {
    system("cls"); // Clear All of the text
    timer = clock();  // Update the timer var
    cout << "Seconds since started: " << timer / 1000 << endl; // Print the time since the program started in seconds
    }
    cout << "The program has been going for a minute" << endl;
    system("pause");
    return 0;
}

Thanks in advance!

Member Avatar for iamthwee

You're on the right track, the time library is good.

You were right iamthwee! well after a little thought i rewrote the code so it now does what i want:

#include <cstdlib>
#include <iostream>

#include <ctime> // time header

using namespace std;

int main()
{
    cout << "Please enter time in seconds: "; //user prompt
    int input; //int for time input
    cin >> input; //writes time into input var
    int timer = clock();
    while ( (timer / 1000) < input) // while prog reaches inputted time
    {
    system("cls"); // Clear All of the text
    timer = clock();  // Update the timer var
    cout << "Seconds since started: " << timer / 1000 << endl; // Print the time since the program started in seconds
    }
    cout << "Completed in " << input << " seconds" << endl;//completed message
    system("pause");
    return 0;
}

but i need to pause it just after the user input so it doesnt begin until the user has entered the required time. I tried to use system pause but that didn't work any suggestions please?

Member Avatar for iamthwee

I don't know, are you looking for a portable solution or something that just works for windows.

If it's the latter you might be better of using <windows.h>

Hi, could you explain to me what do you mean by this code?

void wait ( int seconds )
{
  clock_t endwait;
  endwait = clock () + seconds * CLOCKS_PER_SEC ;
  while (clock() < endwait) {}
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.