Hey,
Was bored so *TRIED* to make this simple countdown program but i'm just not sure what to put in the for loop?

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

using namespace std;

int main()
{
    int countDown;
    
    cout << "Enter a number to count down from : ";
    cin >> countDown;
    
    for(int i )
    {
            cout << i;
            Sleep(1000);
            system("cls");
    }
}

I've tried int i == countDown etc, but i don't know help please

Dani AI

Generated

Short answer: the for initializer needs an assignment, not a comparison. Writing int i == countDown is a syntax error because == tests equality; you want to set the loop variable to the start value. As pointed out, initialize the loop variable from the entered number, test a stopping condition (for example, test that the value is not below 0) and decrement each iteration.

A simple, more portable approach (no Sleep or system("cls")) is to use std::this_thread::sleep_for and overwrite the same console line. This avoids Windows-only headers and system calls:

#include <iostream>
#include <thread>
#include <chrono>

int main() {
    int countDown;
    if (!(std::cin >> countDown)) return 0;

    while (countDown >= 0) {
        std::cout << '\r' << countDown << std::flush;
        std::this_thread::sleep_for(std::chrono::seconds(1));
        --countDown;
    }
    std::cout << '\n';
}

Notes and troubleshooting:

  • Validate input (non-integer or negative values) before counting down. Decide whether negatives should count upward or be rejected.
  • std::this_thread::sleep_for is portable; see std::this_thread::sleep_for.
  • Avoid system("cls") — it is platform-specific and invokes a shell; prefer '\r' to overwrite the line or use ANSI escape codes / console APIs for full-screen control. See std::system for details.
  • Compile with C++11 or later (-std=c++11 or newer) for the threading/chrono facilities.
for(int i=countDown;i>=0;i-- )
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.