Help, please extra row in output

Hello! I'm very new to c++ and am currently enrolled in an intro class. For my code, my output has an extra row in it and I've tried everything I can think of! I was hoping someone can tell me where my error is! Thank you guys in advanced.

This is what it should look like when enter the number 6 and char $:


What is the height of you parallelogram? 
What character do you want to print? 

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


Mine looks like:

What is the height of you parallelogram? 
What character do you want to print? 

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


This is my 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
#include <iostream>
#include <cmath>
using namespace std;

int main()
{
int height=0;
int x=0;
int y=0;
double width;
char symbol;

cout <<"What is the height of you parallelogram? \n"; 
cin >> height;
cout <<"What character do you want to print? \n"; 
cin >> symbol;
cout <<"\n";

width=(height*2);
for (int x=0; x<=height; x++)
   {
     for (int y=0; y<=width; y++)
      {
if (x==0 || x==(height))
cout << symbol;
else if (y==0 || y==(width))
cout << symbol;

else
cout << " ";

}

cout << endl;
}
}
hi NNinja

problem lies in your loop. you used
for (int var=0; var<=height; var++)
for both cases of x and y

if we look into detail of for loop, it actually runs until the condition becomes false.

lets assume that i inputted 3 and the loop will start from zero.

so x is initialized as 0, is 0 less than or equals to 3? true. run process, increment x.
x is now 1, is 1 less than or equals to 3? true. run process, increment x.
x is now 2, is 2 less than or equals 3? true. run process, increment x.
x is now 3, is 3 less than or equals 3? true. run process, increment x.
x is now 4, is 4 less than or equals 3? false. stop.

as you can see, you ran the command ONE extra time, because you have
var<=height

solution is to either initialize x as 1, or use var<height

hope this helps.
That helped a lot and makes sense to me! Thank you so much!! E-hugs.
you're welcome, and glad it did work for you :) please mark the question as solved if you feel your question has been answered
Topic archived. No new replies allowed.