Iteration using for loop

Hey! I am a beginner in C++ and I am trying to build a code where the count value input by the user will decrement (if input value is 10, then: 10,9,8,7,6,5,4,3,2,1) till the condition is met. And this count will be displayed till the input iteration value becomes 0.

But the code does not work and supposedly does not take the for (i) for loop. and thus displays "a" only one time.

What am i doing wrong?

[code]

#include <iostream>
using namespace std;

int main()
{
int a,i;
cout << "Enter the count value: ";
cin >> a;
cout << "Enter the iteration value: ";
cin >> i;
{
for (i;i>0; i--)
{
for (a; a>0; a--)
{
cout << a << ", ";
}
}
}
return 0;
}
your code runs fine here http://cpp.sh/9pvtp

there was a warning about your loops, if you dont want to set a start value skip it entirely.
1
2
for (;i>0; i--)
for (; a>0; a--)


but that didnt and shouldnt stop your code from running.

We do not need two different user inputs (and two loops) for this.

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>

int main()
{
    int cnt = 0 ;
    std::cout << "enter count value: " ;
    std::cin >> cnt ;
    
    // repeat as long as cnt is greater than zero 
    for( ; cnt > 0 ; --cnt ) std::cout << cnt << ' ' ;

    std::cout << '\n' ;
}
My question is actually like,

if i input my count value as 5. Yes, it will run like: 5,4,3,2,1.
But I wanna go for something like

Count Value: 5
Iteration Value: 3

Output:

5,4,3,2,1
5,4,3,2,1
5,4,3,2,1

The iteration value defines how many times the count value will be displayed.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

int main()
{
    int cnt = 0 ;
    std::cout << "enter count value: " ;
    std::cin >> cnt ;

    int iterations = 0 ;
    std::cout << "enter iteration value: " ;
    std::cin >> iterations ;

    for( int i = 0 ; i < iterations ; ++i ) // repeat iteration number of times
    {
        // print a comma after every number other than 1
        for( int j = cnt ; j > 1 ; --j  ) std::cout << j << ", " ;
        if( cnt > 0 ) std::cout << "1\n" ; // print 1 and a new line
    }
    // note that if iterations < 1, nothing is printed (the body of the outer loop does not execute)
    // note that if cnt < 1, nothing is printed
    //           (the body of the inner loop does not execute, and printing "1\n" at the end is skipped)
}
Got it, works now!! Thank you so much.
Topic archived. No new replies allowed.