program that draws a rhombus, or something like it

Hey!

I need some help to write a program that would draw a rhombus (not exactly).
For example-if a person inputs number 4, it would draw:
1
2
3
4
   ****
  ****
 ****
****

I have no idea on even how to start to write such thing, and im sure it uses for() loops.

And can someone also post a link to a tutorial on for() loops, couse i am unable to understand how they work and where to use them
Last edited on
This is what i got so far, it draws only 4 lines, i need it to draw as much lines as the length of the string. I dunno what to write witch would write spaces and lines accordingly.

1
2
3
4
5
6
7
8
9
10
11
      
cout<<"Enter a number: ";
cin>>chars;


string length (chars,'*');
cout<<length<<"\nTest succesfull, proceding to draw!;\n";
cout<<"\n    "<<length;
cout<<"\n   "<<length;
cout<<"\n  "<<length;
cout<<"\n "<<length<<endl;



So if the input is 5, it draws :
1
2
3
4
   *****
  *****
 *****
*****

Thats only 4 lines! it should draw this:
1
2
3
4
5
    *****
   *****
  *****
 *****
*****


Any help please?
Ad a for loop to print the lines:

( input = 4 )
iteration|spaces|stars|    
     0   |   3  |  4  | \n
     1   |   2  |  4  | \n
     2   |   1  |  4  | \n
     3   |   0  |  4  | \n
Could you please tell me how? i have absolutely no knowledge of the for loops
Something like this:
1
2
for ( int i = 0; i < chars; i++ )
     cout << /*Spaces*/ << length << '\n';

Notice how the number of spaces is equal to (chars-1) - i
Just a question,

heres some code:

1
2
3
string i = '****';

string b = i - 1;


So, string b would be equal to '***' ?
No, there are some functions to do that eg:
i.erase(i.size()-1); removes the last character from the string
b = i.substr(0,i.size()-1) copies all the characters but the last one from i to b

( You should use double quotes for strings: "****" )
ok, thanks. So could you help me write the for loop? i have no idea on how to even start with it
Start from the loop i posted earlier.
You can use the string constructor like this: string ( n, ' ' ) to get a string with n whitespaces
Ok, i will try :D

EDIT:

Ok heres what i came up with:


1
2
3
for ( int n = chars, string spc(n,' ');  n > 0; n--){
     cout <<spc<< length << '\n';
     }


It doesn't compile, it says "expected init-declarator before 'spc';

EDIT 2:

Ive did it!!! IT WORKS WOOOHOOOO!!!

look, heres the code!!!

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
#include <cstdlib>
#include <iostream>
#include <cstring>
using namespace std;

int main(int argc, char *argv[])
{
int chars;  
      
cout<<"Enter a number: ";
cin>>chars;


string length (chars,'*');

int n;


for ( n = chars;  n > 0; n--){
    string spc(n,' ');
     cout <<spc<< length << '\n';
     }
  
    system("PAUSE");
    return EXIT_SUCCESS;
}


THANKS BAZZY!!!
Last edited on
You can't declare variables of different types in a single statement.
You can just use the constructor: cout << string ( /*...*/ ) << //...
The right formula for the number of spaces is this: http://www.cplusplus.com/forum/beginner/16586/#msg82965

Why did you buyp if your post was already the first?
Nvm, Thanks verry much bazzy, i owe you one. No, make it 10. :D
Topic archived. No new replies allowed.