C++: Using a for loop to print a countdown

  • C/C++
  • Thread starter needOfHelpCMath
  • Start date
  • Tags
    Loop
In summary, the code provided should print a countdown from the user's input down to 1, and then output "Blastoff!" on a new line. The correct version of the code would not reset the value of userNum and would use a for loop to iterate through the countdown.
  • #1
needOfHelpCMath
72
0
Write code that prints: userNum ... 2 1 Blastoff! Your code should contain a for loop. Print a newline after each number and after Blastoff!. Ex: userNum = 3 outputs:
3
2
1
Blastoff!

Code:
#include <iostream>
using namespace std;
int main() {
   int userNum = 0;
   int i = 0;
   userNum = 3;
   i = 1;
   for (userNum = 3; 1 <= userNum; --userNum) {
   cout << userNum << endl;   
   
   }
 cout << "Blastoff!" << endl;
   return 0;
}

Run
Testing with userNum = 3.
Your output: 3
2
1
Blastoff!
✖ Testing with userNum = 1.
Expected output: 1
Blastoff!
Your output: 3
2
1
Blastoff!
 
Technology news on Phys.org
  • #2
Re: So close what is wrong with my program

You are resetting the value of [m]userNum[/m] when you begin the for loop. This is how I would write the program:

Code:
#include <iostream>
using namespace std;
int main()
{
	int userNum = 3;
	int i;

	for (i = userNum; i > 0; i--)
	{
		cout << i << endl;   
	}

	cout << "Blastoff!" << endl;
	return 0;
}

Please note that I have edited your thread title to describe the nature of the question being asked, and enclosed your code in the [CODE][/CODE] tags so that whitespaces are preserved and the indentation will enhance readability. :)
 

FAQ: C++: Using a for loop to print a countdown

How do I create a countdown using a for loop in C++?

To create a countdown using a for loop in C++, you can use the following syntax:
for (int i = startingNumber; i >= endingNumber; i--) {
    cout << i << endl;
}
Replace "startingNumber" with the number you want to start the countdown from, and "endingNumber" with the number you want to end the countdown at.

Can I use a for loop to print a countdown in reverse order?

Yes, you can use a for loop to print a countdown in reverse order by changing the condition in the for loop. For example, if you want to start the countdown from 10 and end at 1, the syntax would be:
for (int i = 10; i >= 1; i--) {
    cout << i << endl;
}

How can I add a delay between each number in the countdown?

You can add a delay between each number in the countdown by using the sleep() function from the unistd.h library. This function takes in the number of seconds you want to pause the program for. For example, if you want a 1-second delay, you can use sleep(1); after the cout statement in the for loop.

Is it possible to use a for loop to print a countdown with a different increment?

Yes, you can use a for loop to print a countdown with a different increment by changing the third argument in the for loop. The third argument controls the increment or decrement of the loop variable. For example, if you want to count down by 2s, the syntax would be:
for (int i = 10; i >= 1; i -= 2) {
    cout << i << endl;
}

Can I use a for loop to print a countdown more than once?

Yes, you can use a for loop to print a countdown more than once by using a nested for loop. The outer for loop will control the number of times the countdown is printed, and the inner for loop will print the countdown. For example, if you want to print the countdown 3 times, the syntax would be:
for (int j = 0; j < 3; j++) {
    for (int i = 10; i >= 1; i--) {
        cout << i << endl;
    }
}
This will print the countdown from 10 to 1, three times.

Similar threads

Replies
2
Views
12K
Replies
4
Views
11K
Replies
28
Views
29K
Replies
4
Views
5K
Replies
3
Views
10K
Replies
2
Views
12K
Replies
5
Views
2K
Back
Top