2

I am a beginner. This is a program to print a right angled triangle.The height and base should same. for example if input n = 3 then we should get the output as.

*
* *
* * *

I wrote the following code for this:

#include<iostream>
using namespace std;

int main(){
    int n;
    cin>>n;
    for(int i=1;i<=n;i++){
        while(i>=1){
            cout<<"*";
            cout<<" ";
            i--;
            cout<<i;
        }
        cout<<"\n";
    }

}

but it goes on forever. anything wrong?

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
Abhinav K
  • 87
  • 5
  • 2
    What is the value of `i` each time after the while loop? – Galik Jun 20 '21 at 14:24
  • 1
    [`using namespace std` is a bad habit](https://stackoverflow.com/q/1452721/5994041). – Peter Badida Jun 20 '21 at 15:22
  • @PeterBadida and use `++i` and use `i = 0; i < n` and use proper formatting and don't print chars as strings and ... and ... and. I think getting the code to work in the first place should be a greater concern for a beginner than making it pretty. Besides, they are probably following some sh*t course that teaches it that way, so recommending a [good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) would be more useful. – Nikita Demodov Jun 20 '21 at 15:42
  • @NikitaDemodov One thing is hiding errors, other one is style and optimization. It's not the same. – Peter Badida Jun 20 '21 at 15:45
  • @PeterBadida I'm 101% sure that this code doesn't fail because of the `using namespace std;`. – Nikita Demodov Jun 20 '21 at 15:48

4 Answers4

3

You are decrementing i in while loop and incrementing in for loop. Use separate variable j.

using namespace std;

int main(){
    int n;
    cin>>n;
    for(int i=1;i<=n;i++){
        int j = i;
        while(j>=1){
            cout<<"*";
            cout<<" ";
            j--;
            cout<<j;
        }
        cout<<"\n";
    }

}
Akshay Bande
  • 2,491
  • 2
  • 12
  • 29
1

The problem is that, after you exit the while loop, the value of i changes back to 1 and the for loop again start from i = 1 which results in the infinite loop

You can store the value of i to another varible like j and use this j to do the while loop Here is the code :

#include<iostream>
using namespace std;

int main(){
    int n;
    cin>>n;
    for(int i=1;i<=n;i++){
        int j=i;
        while(j>=1){
            cout<<"*";
            cout<<" ";
            j--;
            cout<<j;
        }
        cout<<"\n";
    }

}

Output :

* 0
* 1* 0
* 2* 1* 0
Rohith V
  • 1,089
  • 1
  • 9
  • 23
1

This happens because your while loop reverts i to 0 and then your for loop starts from 0 each time.

A solution is to use other variable for inner loop:

#include<iostream>
using namespace std;

int main() {
    int n;
    cin >> n;
    for (int i = 0; i < n; i++){
        for (int j = 0; j <= i; j++) {
            cout << "* ";
        }
        cout << '\n';
    }
}
Meowster
  • 447
  • 3
  • 14
0

The main issue in the program is that variable i is being shared by both inner and the outer loop. The inner while loop is trying to modify the value of i to 0 the the value of i is incremented by 1 by outer for-loop , again the control come back to while loop and makes the value of i to 0. This repeats in subsequent steps leading to the infinite loop.

To avoid this ambiguity it is recommended to use another variable j for the inner while loop.

Please have a look at the modified code that solved this issue below :

#include<iostream>
using namespace std;

    int main(){
        int n;
        cin>>n;
        int j;
        for(int i=1;i<=n;i++){
            j=i;
            while(j>=1){
                cout<<"*";
                cout<<" ";
                j--;
                cout<<j;
            }
            cout<<"\n";
        }
    
    }
Chandra Shekhar
  • 598
  • 1
  • 7
  • 25
  • 1
    Keep your variables as close to their uses as possible. In this case, put `j` in front of the `while` loop(don't forget: your compiler isn't stupid). Also another `for` loop would be better suited here. – Nikita Demodov Jun 20 '21 at 15:46