Hi i have just started my c++ class and really having hard time with that.i was assigned to draw a graph which should look like dis :

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

This is what i am trying but getting only this result :
*****
*
*
*
*

Please help me out with this:

#include<iostream>
using namespace std;

int main()
{

        int i=0;
        while (i<4)
        {
        cout<<'*';
        i++;
        }
        int j=0;
        while (j<5)
        {
        cout<<'*';
        j++;
        cout<<endl;
        i++;
 }
return 0;   
}

First off, please use code tags, it makes your code easy to read.

Now your loops

while (i<4)
{
cout<<'*';
i++;
}

This will print out "****"

Your next loop

while (j<5)
{
cout<<'*';
j++;
cout<<endl;
i++;
}

will add a "*" to the first line, then print a newline and then print 4 more lines with "*", giving you your output !

In your case you need to print 5 lines with 5 "*"'s in each line. You need a nested loop.
For example:

initialize i;
  initialize j;
  while (i < 5){
    while(j < 5){
      do your stuff
      inc j;
    }
    reset j to 0;
    inc i;
  }

This is what i got

*****
*
*
*
*

using this code :

#include<iostream>
using namespace std;

int main()

{
        int i=0;
        int j=0;
        while (i<4)
        {
            cout<<'*';
            i++;
        }
        while (j<5)
        {
            cout<<'*';
            j++;
            cout<<endl;
            i++;
        }
        return 0;
}

sorry i m not sure how to put them in 'code tags'

You need to use a nested loop, as I said in the post above. Your code loops as I explained already will not work.

In order to put them into code tags put your code between the tags [ code ] and [ / code ] (lose the spaces).

Hallo here an object oriented solution for your problem. I am expecting u to play with it and test different graphs :)

Dont be scared from the size.

//this is kind of object oriented solution

#include <iostream>

class CHorBar
{
public:
	CHorBar (int numSymbols)
	{
		std::cout << "*";//When i first wrote this code I wanted to print sth like +------+
		//thats why this line was required for '+', now i simply changed it to '*' to adopt ti your needs
		for (int i=0;i<numSymbols;i++)
		{
			std::cout << "*";//U can use what ever u want your graph to print
		}
		std::cout << "*"<<std::endl; //When i first wrote this code I wanted to print sth like +------+
		//thats why this line was required for '+', now i simply changed it to '*' to adopt ti your needs
	}
};
//@-@-@-@-@-@-@-@
class CVerBar
{
//this class serves for printing a verical bar combined with the previously declared horizontal bar like e.g.:
//	**********	//horizontal
//	*
//	*
//	*//vertical
public:
	CVerBar (int numSymbols)
	{
		for (int i=0;i<numSymbols;i++)
		{
			std::cout << "*"<<std::endl;
		}
	}
};
//@-@-@-@-@-@-@-@
class CFrame
{
	//this class serves for printing a frame (verical bar combined with 2 horizontal bars) upper and lower like e.g.:
//	**********	// upper horizontal
//	*
//	*
//	*//vertical
//	*
//	*
//	**********	// lower horizontal
public:
	CFrame (int hor,int ver):
		_upHorBar(hor),
		_theVerBar(ver),
		_lowHorBar(hor)
	{}
private:
	CHorBar	_upHorBar;
	CVerBar	_theVerBar;
	CHorBar	_lowHorBar;
};
//@-@-@-@-@-@-@-@
class CLadder
	//	This class serves to print a ladder(combined frames)
{
public:
	CLadder (int hor,int ver):
		_firstFrame(hor,ver),
		_theVerBar(ver),
		_secFrame(hor,ver)
	{}
private:
	CFrame	_firstFrame;
	CVerBar	_theVerBar;
	CFrame	_secFrame;
};
//@-@-@-@-@-@-@-@


int main()
{
//different tests:
/*CHorBar aHorBar1(5);
CVerBar aVerBar1(3);
CHorBar aHorBar2(7);
CVerBar aVerBar2(4);
CHorBar aHorBar3(2);
CFrame aFrame(10,3);
CLadder aLadder(5,2);*/

	//What u need is
for(int i=0;i<5;++i)
	CHorBar aHorBar3(3);
}

Have fun and hope I helped u.

What would a flowchart for this program look like?

dis:

for(int i=0; i<5; i++)
{
    for(int j=0; j<5; i++)
        cout << "*"
    cout << endl;
}

Hmm... I'm trying to figure out who that post was intended for.

you closed the i loop and then started the j loop. your program should actually look like this: i tested it:

//you closed the i loop and then started the j loop. your program should actually look like this: i tested it:

#include <iostream>
using namespace std;

int main() {

int i=0; int j=0;

while (i<5) {

while(j<5) {

cout<<'*';

j++;

}

cout<<endl;

i++;

if(i<5) { j=0; }

}

return 0;

}

note: it would have been better for you to have used nested for loops;

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.