Output Incorrectly

closed account (3vX4LyTq)
I am writing a program that creates a rectangle with the user's dimensions. But, for some reason it always displays incorrectly.
Example
Height: 5
Width: 5
Character: *

It outputs:
* * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *

So, it's close but not quite.


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

int makeRectangle(int, int, string);

int main() {
int height = 0;
int width = 0;
string character = "*";
cout <<"I make rectangles! Set weight as -1 to quit! ";
cout <<"How high?";
cin >> height;
cout <<"How wide?";
cin >> width;
cout << "What character?";
cin >> character; 
makeRectangle(height, width, character);
}
int makeRectangle(int hi, int wi, string ch){
    while(hi >0){
        while(wi > 0) {
            wi--;
            cout << ch << " ";
        }
        cout << endl;
        hi--;
        wi=10;
    }
}
1
2
3
4
5
6
7
for(int i=0;i<wi;i++){
    for(int o=0;o<hi;o++){
        cout<<"*";
    }
    cout<<endl;
}


you've made the loop complicated for no reason
In addition to DatDankMeme's correct response, what is the purpose of w=10; in line 27? What do you expect this to do?
1
2
3
4
5
6
7
8
9
void makeRectangle(int height, int width, char ch)
{
  string line(width, ch);

  for (int row = 0; row < height; row++)
  {
    cout << line << '\n';
  }
}
closed account (3vX4LyTq)
MZH - in line 27, I set w=10 to reset the width because I would be using it again in the future. DatDankMeme - I'm pretty new to C++ so I couldn't really figure out any other way to do it. You helped a ton. Thomas1965 - I didn't know you could do that with strings. Thanks.
ja r ed: Setting w=10 would only work if the length of the row was 10. Instead, every row after the first will have a length of 10, regardless of user input. That's why the output in your initial post looks the way it does. To fix your while loop in your initial program, you need a second variable to save the original width from the function parameter.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <string>
using namespace std;

void makeRectangle( int w, int h, char ch )
{
   while ( h-- ) cout << string( w, ch ) + '\n';
}


int main()
{
   makeRectangle( 5, 5, '*' );
}
closed account (48T7M4Gy)
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
#include <iostream>

using namespace std;

void makeRectangle(int, int, char);

int main() 
{
    int height = 0;
    int width = 0;
    char character = '*';
    cout <<"I make rectangles! Set height as -1 to quit!\n";
    cout <<"How high? ";
    cin >> height;
    if(height != -1)
    {
        cout <<"How wide? ";
        cin >> width;
        cout << "What character? ";
        cin >> character;
        makeRectangle(height, width, character);
    }
    std:: cout << "Bye\n";
    
    return 0;
}

void makeRectangle(int hi, int wi, char ch)
{
    int awi = 0;
    
    while(hi > 0)
    {
        awi = wi;
        while(awi > 0)
        {
            awi--;
            cout << ch << " ";
        }
        cout << endl;
        hi--;
    }
    return;
}
I make rectangles! Set height as -1 to quit! How high?6
How wide?2
What character?I
I I 
I I 
I I 
I I 
I I 
I I 
Bye
Last edited on
Topic archived. No new replies allowed.