Hello, I am new to c++ and just started a for fun online course a few weeks ago.
I created some code to make a square based off what the user inputs to how big the square should be. How can I edit this code to make it into a diagonal line?
EX: If user inputs 4: (Ignore the periods)
*
.*
..*
...*
Here is what I have to make a square that works:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
int i, n, size = 0;
string line;
cout << "Enter the size of your shape:" << endl;
cin >> size;
// Outputs a square into console.
for (i = 0; i < size; ++i, line = "") {
for (n = 0; n < size; ++n)
line += '*';
cout << line << endl;
}
cout << endl;
}
Any help would be appreciated! Thanks in advance :)
What you have to keep in mind is how many spaces you need to print before each '*'. It is a quick thought for now I may come up with something different tomorrow.
What you have is the inner for loop condition is based on the outer for loops counter. This determines how many spaces to print before you print the '*' and return to the top of the outer for loop.