Square made of *'s with a number inside

Ask the user for a single digit number. Store as int.
Print a rectangle made of *'s with the number inside
Im only allowed to use cin, cout and variables
This is what I have so far how do I put the number in the middle

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  #include <iostream>
using namespace std;
int main()
{
	cout << "Enter a single digit number\n";
	int x; cin; x;


	cout << "*****************\n";
	cout << "*               *\n";
	cout << "*               *\n";
	cout << "*               *\n";
	cout << "*               *\n";
	cout << "*               *\n";
	cout << "*               *\n";
	cout << "*****************";
}
1
2
#include <iostream>
int main() { int n; if (std::cin >> n) std::cout << "***\n*" << n << "*\n***\n"; }
Last edited on
Im only allowed to use cin cout and variables. No if statements
I'm sure you can figure out a solution based on what I posted.
Hello Notanormie,

Thank you for using code tags.

How does this work for you:
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
#include <iostream>

using namespace std;  // <--- Best not to use.

int main()
{
    int x{};  // <--- ALWAYS initialize all your variables.
    
    cout << "Enter a single digit number: ";
    cin >> x;


    cout <<
        "******************\n"
        "*                *\n"
        "*                *\n"
        "*        "
               << x <<
                  "       *\n"
        "*                *\n"
        "*                *\n"
        "*                *\n"
        "******************";

    return 0;  // <--- Not required, but makes a good break point for testing.
}

In your line 6 the "cin" needs the extraction operator, (>>) to work properly. Right now it just skips the the "cin" because there is nothing to extract into.

You do not need a "cout" for everyline. Good use of the (\n) though.

Lines 13 - 23 are set up to look more like your output, the compiler will ignore the extra white space. Lines 14 - 17 are considered just 1 string even-though they are on separate lines. And the same is true for lines 19 - 23.

I added 1 extra (*) on the first and last line and a space on the other lines to better center the number inside the box.

Andy
Thank you so much!
Topic archived. No new replies allowed.