c++ programm using loops

5. Print the sequence using do while loop.
* * * *
* * * *
* * * *
* * * *
Last edited on
Well, what's your first attempt ?
How do you think you could solve it ?
Would a counter be needed ?
Go check your lesson about do-while loops first.
well thank
but i have learnt the lesson
also i have soleved this problem by for loop
but doing this by do while it gets infinite
i m unable to determine the problem
another problem that i encounter is that i m using turbo c++ on windows 7 i cant reopen the program not can save the ide
plz if u can guide me how to solve these problems
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>

main()
{
	int n;
	n=0;
		
	do{
		n++;
		std::cout<<"* * * *"<<std::endl;
	}while(n!=4);
}
1
2
3
do{
   std::cout << "* * * *\n* * * *\n* * * *\n* * * *";
while(1!=2)
Resident, there are two issues with your program.

1. It is an infinite loop since 1 is always not equal to 2.
2. There is no ending }.

Your program fixed:
1
2
3
4
5
6
7
8
#include <iostream>

main()
{
	do{
   		std::cout << "* * * *\n* * * *\n* * * *\n* * * *";
	}while(true==false);
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>

int main()
{
	const char c = '*';
	const unsigned int N = 4;

	unsigned int i = 0;
	do
	{
		unsigned int j = 0;
		do
		{
			std::cout << c;
		} while ( ++j < N && std::cout << ' ' );
	} while ( std::cout << std::endl && ++i < N );
}
Last edited on
I think vlad wins the prize haha.
^^agreed. Vlad's is the longest...
Last edited on
And easiest to read -_-
this is a very basic question, you can find it any c++ reference books, please check it
#include<iostream.h>
#include<conio.h>
main()
{
clrscr();
int i, j,k;
for (i=1; i<=2; i++)
{
for(j=1; j<=2; j++)
{
for (k=1; k<=4; k++)
{
cout<<"*";
}
{
cout<<"\n ";
}
}
}
getche();
}
the program it did using for loop
but problem is
stars appears in this pattern
****
****
****
****
while i m asked to do it like this
****
****
****
****
row 2 and 4 appears with keeping space from start
Last edited on
I do not understand why you are showing this code that has nothing common with your original post.
As for the additional space then it appears due to the statement
cout<<"\n ";
where the string literal consists from two characters: the new line character and the space.
I think usandfriends has it. Something along these lines:

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
using namespace std;

int main () {
     int i = 0;

     do {
          cout << "* * * *" << endl;
          i++;
     } while (i <= 4); 
     return 0;
}
Last edited on
Topic archived. No new replies allowed.