Problem With this Code...Please Help

1

1111111111

I am writing a program that tells users to enter 2 sides of a
rectangle. From that the program will output a drwaing of the
rectangle using "*"'s... Here is my problem. Say I enter 5 and 10 for
the 2 sides.. This is what is output.

********** *
* *
* *
* *
**********

I dont know why it is adding that asterk on the first row.. Same
thing happens if I enter 2 other sides. Etc... Any help would be
appriciated!!!

Here is my code so far: I use Turbo hence the .h

#include <iostream.h>
#include <conio.h>
#include <iomanip.h>
int main()
{
clrscr();

int side1, side2;

cout<<"Please enter side 1: ";
cin>>side1;
cout<<"Please enter side 2: ";
cin>>side2;

for(int a=1; a<side1; a++)
cout<<"*";

for(int b=1; b<=(side2-1); b++)
cout<<"*"<<setw(side1-1)<<"*"<<endl;

for(int c=1; c<=side1; c++)
cout<<"*";


return 0;
}
 
A

Allan Bruce

1111111111 said:
I am writing a program that tells users to enter 2 sides of a
rectangle. From that the program will output a drwaing of the
rectangle using "*"'s... Here is my problem. Say I enter 5 and 10 for
the 2 sides.. This is what is output.

********** *
* *
* *
* *
**********

I dont know why it is adding that asterk on the first row.. Same
thing happens if I enter 2 other sides. Etc... Any help would be
appriciated!!!

Here is my code so far: I use Turbo hence the .h

#include <iostream.h>
#include <conio.h>
#include <iomanip.h>
int main()
{
clrscr();

int side1, side2;

cout<<"Please enter side 1: ";
cin>>side1;
cout<<"Please enter side 2: ";
cin>>side2;

for(int a=1; a<side1; a++)
cout<<"*";

for(int b=1; b<=(side2-1); b++)
cout<<"*"<<setw(side1-1)<<"*"<<endl;

for(int c=1; c<=side1; c++)
cout<<"*";


return 0;
}

I would use something like this:

for (int x=0; x<side1; ++x)
{
for(int y=0; y<side2; ++y)
{
// if on an edge draw an *
if ((x==0) || (y==0) || (x==side1-1) || (y==side2-1))
cout << "*" ;
else
cout << " "; // a space
}
cout << endl;
}

Allan
 
J

John Harrison

1111111111 said:
I am writing a program that tells users to enter 2 sides of a
rectangle. From that the program will output a drwaing of the
rectangle using "*"'s... Here is my problem. Say I enter 5 and 10 for
the 2 sides.. This is what is output.

********** *
* *
* *
* *
**********

I dont know why it is adding that asterk on the first row.. Same
thing happens if I enter 2 other sides. Etc... Any help would be
appriciated!!!

Because you are forgetting to output a newline after the end of the first
row (and after the end of the last row). The extra asterisk is actually the
second asterisk on the next row. There are some other problems but you'll
probably figure out those once you've fixed this one.

There's a lesson here, computer do exactly what you tell them to, not what
you *think* you've told them to.

john
 
L

Leor Zolman

I am writing a program that tells users to enter 2 sides of a
rectangle. From that the program will output a drwaing of the
rectangle using "*"'s... Here is my problem. Say I enter 5 and 10 for
the 2 sides.. This is what is output.

********** *
* *
* *
* *
**********

I dont know why it is adding that asterk on the first row.. Same
thing happens if I enter 2 other sides. Etc... Any help would be
appriciated!!!

Here is my code so far: I use Turbo hence the .h

#include <iostream.h>
#include <conio.h>
#include <iomanip.h>
int main()
{
clrscr();

int side1, side2;

cout<<"Please enter side 1: ";
cin>>side1;
cout<<"Please enter side 2: ";
cin>>side2;

for(int a=1; a<side1; a++)
cout<<"*";

Above, you print one too few '*'s for the top row, and then compound the
confusion by forgetting to emit a newline after that line.
-leor
 
A

Aggro

1111111111 said:
for(int a=1; a<side1; a++)
cout<<"*";

for(int b=1; b<=(side2-1); b++)
cout<<"*"<<setw(side1-1)<<"*"<<endl;

for(int c=1; c<=side1; c++)
cout<<"*";

Change the above lines to look something like this:

for(int a=1; a<side1; a++)
cout<<"*";
for(int b=1; b<=(side2-1); b++)
cout<<"%"<<setw(side1-1)<<"&"<<endl;
for(int c=1; c<=side1; c++)
cout<<"#";

i.e. Don't print asterisks only, use different character on each
different place where you print it. This will help you to see what you
are doing, and that helps you to find your error.
 
A

Alf P. Steinbach

* (e-mail address removed) (1111111111) schriebt:
Here is my code so far: I use Turbo hence the .h

Get yourself a C++ compiler, e.g. mingw g++ (it's free).

#include <iostream.h>
#include <conio.h>
#include <iomanip.h>

Fix the above when you've got yourself a C++ compiler.

int main()
{
clrscr();

int side1, side2;

cout<<"Please enter side 1: ";
cin>>side1;
cout<<"Please enter side 2: ";
cin>>side2;

for(int a=1; a<side1; a++)
cout<<"*";

1) If side1 is 1, how many asterisk will be output?
2) Always use braces around the loop body.
3) Use ++a not a++.
4) Why is there no newline?

for(int b=1; b<=(side2-1); b++)
cout<<"*"<<setw(side1-1)<<"*"<<endl;

5) (2) and (3), plus: you don't have to introduce a new loop
control variable name (when using a C++ compiler, that is).

for(int c=1; c<=side1; c++)
cout<<"*";

6) (5) and (4).
return 0;
}

7) Indentation.
 
D

Default User

1111111111 said:
I am writing a program that tells users to enter 2 sides of a
rectangle. From that the program will output a drwaing of the
rectangle using "*"'s... Here is my problem. Say I enter 5 and 10 for
the 2 sides.. This is what is output.

********** *
* *
* *
* *
**********

I dont know why it is adding that asterk on the first row..

You are. You didn't put any kind of end of line termination following
the first line of '*' characters.

Here's a fixed up version. I changed your loops to start from 0 and use
consistent comparisons. Also your use of endl was overkill, so I changed
it to be a line terminator.


int main()
{

int side1, side2;

cout<<"Please enter side 1: ";
cin>>side1;
cout<<"Please enter side 2: ";
cin>>side2;

for(int a=0; a<side1; a++)
cout<<"*";
cout << '\n';
for(int b=0; b<(side2-2); b++)
cout<<"*"<<setw(side1-1)<<"*"<<'\n';

for(int c=0; c<side1; c++)
cout<<"*";
cout << '\n';

return 0;
}


You should also note that your program doesn't work well for bad input.
Note what happens if the user enters "three" for the first side.




Brian Rodenborn
 
D

Default User

1111111111 said:
I am writing a program that tells users to enter 2 sides of a
rectangle. From that the program will output a drwaing of the
rectangle using "*"'s... Here is my problem. Say I enter 5 and 10 for
the 2 sides.. This is what is output.

Oh, I meant to add, learn to use your debugger. Follow through the
program execution.




Brian Rodenborn
 
H

Howard

Alf P. Steinbach said:
* (e-mail address removed) (1111111111) schriebt:

Get yourself a C++ compiler, e.g. mingw g++ (it's free).



Fix the above when you've got yourself a C++ compiler.

I take it you mean a "conforming" C++ compiler? VC++ 6.0 certainly uses a
C++ compiler, but it's not conforming in this aspect (and others, no doubt).
(It's one of several I have, and am required to use due to compatibility
with SDKs for some of the audio programs I write. One cannot always choose
the compiler at will.)
1) If side1 is 1, how many asterisk will be output?
2) Always use braces around the loop body.
Good practice, but not an error, and not relevant to the problem.
3) Use ++a not a++.
Again, good practice, but not relevant to the problem, and it makes no
difference at all when using int.
4) Why is there no newline?
Here's the relevant item as far as the posted problem is concerned.
7) Indentation.

What about indentation? I would not doubt that this was indented when
written. But copy&paste from some editors into some newsreaders (e.g., VC++
to Outlook Express) results in the tabs being dropped. It's so common I'm
surprised you bothered to comment on it.

-Howard
 
H

Howard

I take it you mean a "conforming" C++ compiler? VC++ 6.0 certainly uses a
C++ compiler, but it's not conforming in this aspect (and others, no
doubt).

Oops...I mixed up my response a little after rearranging it. The "this
aspect" part I was referring to above was the following:
5) (2) and (3), plus: you don't have to introduce a new loop
control variable name (when using a C++ compiler, that is).

In VC++ 6.0 you have to either enclose the code in braces or define the loop
variable just once, because it does not correctly scope the loop control
variable.

-Howard
 
S

Schisto

The problem is that you're not moving to the next line after the first row of *s.

If you write:
*********
Then:
* *

The result is
********** *

Just add a "cout << endl;" after the first for and replace "a<side1" by "a<=side1".

Schisto
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,610
Members
45,255
Latest member
TopCryptoTwitterChannels

Latest Threads

Top