the typical question which ask user to display THIS

*
**
***
****
*****
******
*******
********
*********
**********

using a single cout<<"*" & cout " " with the help of for loops ONLY. :)


ive come to this this prgram but can anyone tell me where i am going wrong and how to correct this error pls!!! :o

#include <iostream.h>
#include <conio.h>
int main()
{
int a,b=1,c=10;

for ( a=10 ; a >=0  ; a--)
	{

   for (  ; b>0 ; b--)
   	{
      cout<<"*";
      }

   for (  ; c>0  ; c--)
   	{
      cout<<" ";
      }
   }
getch();
return 0;
}

whats wrong here!!!!

THE PROBLEM CONTINUES ON.....but for now i need help till here :cry:

Dani AI

Generated

The bug in the original program is that the inner loop counters are never reinitialized. Writing for (; b > 0; b--) leaves the initialization blank, so b and c go to zero on the first pass and the inner loops never run again. That is why was right about reinitializing. Also prefer standard headers (<iostream>) and int main() rather than iostream.h, conio.h or void main() for portable C++.

A simple, correct pattern uses the outer loop to determine how many stars to print and initializes the inner counter in the inner for header so it resets on every row. If the assignment requires only for loops, use something like:

#include <iostream>

int main() {
  for (int row = 1; row <= 10; ++row) {
    for (int col = 0; col < row; ++col)
      std::cout.put('*');
    std::cout.put('\n');
  }
  return 0;
}

If the goal was a right-aligned triangle (your c looked like it was meant for spaces), print the spaces first, then the stars:

#include <iostream>

int main() {
  const int N = 10;
  for (int row = 1; row <= N; ++row) {
    for (int s = 0; s < N - row; ++s)
      std::cout.put(' ');
    for (int col = 0; col < row; ++col)
      std::cout.put('*');
    std::cout.put('\n');
  }
  return 0;
}

Notes: std::cout.put and '\n' avoid the implicit flush that std::endl does. If portability or modern style matters, std::string(row, '*') is a concise alternative, but for this exercise the nested for pattern above is the clear, teachable solution. Credit to and for showing the nested-loop approach; stick to reinitializing inner counters (or derive them from the outer counter) and the program will behave as intended.

Recommended Answers

All 8 Replies

First thing I see on quick glance is you need an endline command at the end of the for loop.

Oh yeah, also your b and c variables are only initialized to work the first time through. After the first run b and c are equal to zero and therefore those two nested for loops will do nothing. I am heading off to class so I can't fix the problem for you right now but that is whats going to wrong. You need to find a way to reinitialize b and c for every pass through.

Hi....I am a c++ begginer and i hav a solution for ur problem of ur program...Check out the program I wrote..the program is below...Plz message me if ur problem is solved or if u want any help...

#include<iostream.h>

#include<conio.h>

void main()

{

    int i, j;

    for(i=0; i<10; i++)

    {

        for(j=0; j<=i; j++)

            cout<<'*';

        cout<<endl;

    }

    getch();

}

THANX A LOT ETXREME!!!!! :o

but as our forum admin says u shud not help people do their homework!!!


definetly ive been tempted to copy ur program..........BUT !!!!!


as jasweb pointed out i must some re-initialise b and c to one less and one greater value !!!!!........but where exactly in the loops???? :mad:


pls tach me how to do that.....coz more important than assignment marks is that i understand how to do this tpe of stuff.......:?:

Just Extreme's code a little tidied up for Dev C++
Hope you can see the flow of things.

// Dev C++ code

#include <iostream>

using namespace std;

int main()
{
  int k, m;
  
  for(k = 1; k <= 10; k++)
  {
   // outputs 1,2,3,...,10 stars 
   for(m = 0; m < k; m++)
      cout << "*";
   // end each inner loop with a new line 
   cout << endl;
  }
  cin.get(); // wait 
  return 0;
}

Just Extreme's code a little tidied up for Dev C++
Hope you can see the flow of things.

// Dev C++ code

#include <iostream>

using namespace std;

int main()
{
  int k, m;
  
  for(k = 1; k <= 10; k++)
  {
   // outputs 1,2,3,...,10 stars 
   for(m = 0; m < k; m++)
      cout << "*";
   // end each inner loop with a new line 
   cout << endl;
  }
  cin.get(); // wait 
  return 0;
}

AHHH!!......so he's using the first loop variable in the test expression of the second loop!!!!!!!! :mrgreen:
NOW I GET IT!!!!!!!!!!!!!!!!!!!!!!!

So you don't need to use cout << " "; as you originally stated?

:SMOOCH: :SMOOCH: !!!!!!!!

THNX ALOT PPL.......u solved alot of headache............esp. u EXTREME..........for clarifying the problem solvin technique.....and all others who helped me understand this simple problem...thnx alot :o


THIS FORUM ROX!!!! :evil:

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.