New to c++

Hey , it's been a few weeks since I've been learning c++

I got a question ,

I wanted the output to be"

********
*******8
******78
.
.
.
and so on until the last one is going to be
12345678

Instead I get:
********
1*******
12******
.
.
.
.
12345678
so it's the opposite ,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iomanip>
#include <iostream>
using namespace std;
int main (){

int c=1,row=1;
while(c<=8){
	row=1;
	while(row<=8){
		if(row<c or c==8)
		cout<<row;
		else
		cout<<"*";
		row++;
	}
	cout<<endl;
	c++;
	
}

getchar();
getchar();
return(0);	
}
there is error in this code because i hava no compiler but use this approach.
int l=9;
for(int i=0;i<8;i++)
{
for(int m=0;m<8;m++)
{
for(int j=8;j>i;j--)
{
cout<<"*";
}
for(int k=0;k<i;k++)
{
l--;
cout<<l;
}
}
}
@mk12345 Please use code tags
http://www.cplusplus.com/articles/z13hAqkS/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>
using namespace std;

int main()
{
	string starString = "*********";
	string numString = "";
	string tempString = "";

	for (int i = 0; i < 9; i++)
	{
		cout << starString << numString;
		starString = starString.substr(1);
		tempString = numString;
		numString = ('8' - i);
		numString += tempString;
		cout << endl;
	}

	cin.ignore();
	return 0;
}



Here is a recursive approach
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

void star(string myString);
void num(int myNum);

const int MAX_SIZE = 9;

int main()
{
	string starString = "*********";
	star(starString);

	cin.ignore();
	return 0;
}

void star(string myString)
{
	if (myString.size() != 0)
	{
		cout << myString;
		num(myString.size());
		cout << endl;
		star(myString.substr(1));
		cout << endl;
	}
}

void num(int myNum)
{
	if (myNum != MAX_SIZE)
	{
		cout << myNum;
		num(++myNum);
	}
}
Last edited on
Topic archived. No new replies allowed.