c++ - Having problems converting from nested for loop to nested while loop -


i'm trying convert nested loop nested while loop:

the program:

#include<iostream>    using namespace std;    void main()    {     int i,j,n,stars=1;     cout<<"enter number of rows:";     cin>>n;     for(i=1;i<=n;i++)     {         for(j=1;j<=stars;j++)             cout<<"*";     cout<<"\n";             stars=stars+1;     }     } 

while trying nested while loop loop doesn't stop can please give me solution?

#include<iostream> using namespace std; void main() { int n,i,j,k,stars=1;    cout<<"enter number of rows";    cin>>n;     i=1;     while(i<=n)     {     j=1;     while(j<=stars)     {         cout<<"*";         cout<<"\n";         stars=stars+1;     }     j=j+1; } i=i+1;  } 

you have incement control varibales i ans j inside loops. did outsid loops directly after. apart varibale stars incremented in outer for loop. in secend code snippet did in inner while loop. adapt code this:

#include<iostream>  int main() {     int n;     std::cout<<"enter number of rows";     std::cin>>n;     int stars=1;     int i=1;     while ( i<=n )         // corresponds for(i=1;i<=n;i++) { .... }     {         int j=1;         while ( j<=stars ) // corresponds for(j=1;j<=stars;j++) cout<<"*";         {             std::cout<<"*";             j++;           // increment control variable inside of loop           }          std::cout<<"\n";         stars++;         i++;               // increment control variable inside of loop         }     return 0; } 

note if improve formatting of code, you'll find such mistakes easily.


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -