Home  >  Article  >  Backend Development  >  How to write a simple countdown program in C++?

How to write a simple countdown program in C++?

PHPz
PHPzOriginal
2023-11-03 13:39:112405browse

How to write a simple countdown program in C++?

C is a widely used programming language that is very convenient and practical in writing countdown programs. Countdown program is a common application that can provide us with very precise time calculation and countdown functions. This article will introduce how to write a simple countdown program in C.

The key to implementing a countdown program is to use a timer to calculate the passage of time. In C, we can use the functions in the time.h header file to implement the timer function. The following is a code example of a simple countdown program:

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <windows.h>

using namespace std;

int main() {
    int hours, minutes, seconds, totalSeconds;
    cout << "Enter hours: ";
    cin >> hours;
    cout << "Enter minutes: ";
    cin >> minutes;
    cout << "Enter seconds: ";
    cin >> seconds;
    totalSeconds = hours * 3600 + minutes * 60 + seconds;

    for (int i = totalSeconds; i >= 0; i--) {
        int h, m, s;
        h = i / 3600;
        m = (i % 3600) / 60;
        s = i % 60;
        cout << h << ":" << m << ":" << s << endl;
        Sleep(1000);
        system("cls");
    }

    cout << "Time is up!" << endl;

    return 0;
}

First, we define four integer variables hours, minutes, seconds and totalSeconds, which are used to store the input hours, minutes, seconds and Total countdown seconds. We then let the user enter the desired countdown time by using cout and cin statements. After that, we calculate the total countdown seconds and use a for loop to count down.

In the for loop, we use integer division and modulo operations to convert the total countdown seconds into hours, minutes, and seconds and store them in the variables h, m, and s. We then use the cout statement to output the countdown time and the Sleep function to pause program execution for one second. Finally, we use the system command "cls" to clear the screen so the output can be updated.

After the countdown is completed, we output "Time is up!" to prompt the user. It should be noted here that users using Windows systems need to add the #include header file at the beginning of the code to use the Sleep function correctly.

The above is how to write a simple countdown program using C. Through this example, we can see that C can easily implement timer and countdown functions, and more complex functions can be implemented with more code. If you need a more powerful countdown program or other application, you can easily implement it in C.

The above is the detailed content of How to write a simple countdown program in C++?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn