Problem with code for beginner

Hi all,

This is my first C++ course, My assignment is about prompting a user to enter height and character as inputs, and output should be a diamond shape. I think I finished the program, but my problem is that something seems to be off with my top triangle spacing. Theoritically impliment code should implement the correct spacing(I thiink), but the promt shows otherwise. Any suggestions would be appreciated. Thanks in advance.

#include<iostream>
#include<iomanip>
using namespace std;

int main()
{
int w; //spacing
int ccount=1; //character count
int z; //#characters/line limiter
int h; //height input
int counter=1; //counter
char c; //character input

while(cin)

//user prompt section
{
cout<<"Enter Print Character:\n\n";
cin>> c;
cout<<"Enter an odd number for height:"<<"\n\n";
cin>>h;
while(h%2==0)
{
cout<<"You have entered an even number for height, Please enter an odd number: ";
cin>>h;
}
h=abs(h);

cout<<"\n";

//Top Triangle
while(ccount<=h)
{
w=h+20+(counter);

z=ccount;
cout<<setw(w);
while(z>0)
{
cout<<c;
z--;
}
counter++;
cout<<"\n";
ccount=ccount+2;
}
ccount=ccount-2;
while(ccount>0)

{w=h+20-(counter);
counter--;
ccount=ccount-2;
z=ccount;
cout<<setw(w);
while(z>0)
{
cout<<c;
z--;
}
cout<<"\n";

}

}
return 0;
}


The out put im getting:


$
$$$
$$$$$
$$$
$







disregard the shape in my post, for some reasone this post wouldnt show spaces...
Last edited on
First remember to use the CODE tags, it helps us to read your 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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67

#include<iostream>
#include<iomanip>
using namespace std;

int main()
{
     int w; //spacing
     int ccount=1; //character count
     int z; //#characters/line limiter
     int h; //height input
     int counter=1; //counter
     char c; //character input

     while(cin) 
//user prompt section
     {
          cout<<"Enter Print Character:\n\n"; 
          cin>> c; 
          cout<<"Enter an odd number for height:"<<"\n\n"; 
          cin>>h; 
          while(h%2==0) 
          {
               cout<<"You have entered an even number for height, Please enter an odd number: "; 
               cin>>h; 
          }
          h=abs(h); 

          cout<<"\n";

          //Top Triangle
          while(ccount<=h) 
          {
               w=h+20+(counter);

               z=ccount;
               cout<<setw(w); 
               while(z>0) 
               { 
                    cout<<c;
                    z--; 
               } 
               counter++;
               cout<<"\n";
               ccount=ccount+2;
          }
          ccount=ccount-2;
          while(ccount>0) 
          {
               w=h+20-(counter);
               counter--;
               ccount=ccount-2;
               z=ccount;
               cout<<setw(w); 
               while(z>0) 
               { 
                    cout<<c;
                    z--;
               }
               cout<<"\n";

          }

     }
return 0; 
}


are you trying to print a shape like this is if h = 3?
1
2
3
 *
***
 *
You could try this 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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include<iostream>

using std::cout;
using std::cin;
using std::endl;

int main()
{

  int limit;
  int fill=1;
  int spaces;

  cout<<"Enter an uneven number: ";
  cin>>limit;

  while(limit<0 || !(limit%2))
       {

         cout<<"\nError.Try again\n";
         cout<<"\nEnter an uneven number: ";
         cin>>limit;

       }

  cout<<'\n';

  spaces=((limit-1)/2);

  for(int i=1;i<=((limit/2)+1);i++)
     {

       cout<<' ';

       for(int j=1;j<=spaces;j++)
          cout<<' ';

       for(int k=1;k<=fill;k++)
          cout<<'*';

       fill+=2;
       --spaces;

       cout<<'\n';

     }

  fill-=4;
  spaces=1;

  for(int i=1;i<=(limit/2);i++)
     {

       cout<<' ';

       for(int j=1;j<=spaces;j++)
          cout<<' ';

       for(int k=1;k<=fill;k++)
          cout<<'*';

       fill-=2;
       ++spaces;

       cout<<'\n';

     }

  cout<<endl;

  return 0;

}


I guess that s' what you are trying to do? By the way,everyone have a unique style for coding but i think there is no point of writing something like int h=2 //height when you can use height as an identifier directly.This makes your program auto-documented and easier to read.If you feel lazy to type long identifiers you can also use your IDE to find and replace text once you have finished your program.Cheers.
Last edited on
First I would like to thank you for taking the time to respond. I was, however, trying to understand what was going wrong with my spacing using a simple setw, since we havent got to the 2-dim sections of the course I couldnt really use your suggested approach.

I wanted to update you guys on my progress though;
here is how I fixed the problem , using this as my final code;

#include<iostream>
#include<iomanip>
using namespace std;

int main()
{
int width; //spacing
int charactercount=1; //character count
int z; //#characters/line limiter
int height; //height input
int counter=0; //counter
char printcharacter; //character input

while(cin)

//user prompt section
{
cout<<"Enter Print Character:\n\n";
cin>> printcharacter;
cout<<"Enter an odd number for height:"<<"\n\n";
cin>>height;
while(height%2==0)
{
cout<<"You have entered an even number for height, Please enter an odd number: ";
cin>>height;
}
height=abs(height);

cout<<"\n";

//Top Triangle....................................

while(charactercount<=height)
{
width=(height*2)-(counter);
cout<<setw(width);
z=charactercount;

while(z>0)
{
cout<<printcharacter;
z--;
}cout<<"\n";
counter++;

charactercount=charactercount+2;
}

//transition adjustment to reverse triangle(bottom tiangle)......................

charactercount=charactercount-2;
counter=counter-2;

//Bottom reversed triangle

while(charactercount>0)

{width=(height*2)-(counter);
counter--;
charactercount=charactercount-2;
z=charactercount;
cout<<setw(width);
while(z>0)
{
cout<<printcharacter;
z--;
}
cout<<"\n";

}

//reset before reprocess............................................................

counter=0;
charactercount=1;
}
return 0;
}

Although my understanding of setw, was the assigned spaces for characters + remaining empty spaces to the left of the characters. then it seems that its only the case when u use it in the form cout<<setw(x)<<printcharacter; and when used in separate commands like, cout<<setw(x); cout<<printcharacter; the setw(x) will assign x spaces to the left and then print the character to the right of the assigned spaces.

Would appreciate if you can confirm whether im positive about the setw now or I still got the wrong concept about it... Thanks again guys, c++ turns out to be more fun than hardware coding.
Last edited on

You have to use setw right before an I/O instruction,unlike setprecision,fixed,showbase,showpoint,etc.Sometimes you may want to do this:

cout<<setw(width)<<' ';

just as a trick but it is error prone.You can also use cout.width(width) but again it needs to be used before any I/O instruction intended to use it.You can also use the stream manipulators left and right to adjust the text,right is the value by default.

http://www.cplusplus.com/reference/iostream/manipulators/
Last edited on
Thanks Mike, This set my basics on the setw.
Topic archived. No new replies allowed.