By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,194 Members | 859 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,194 IT Pros & Developers. It's quick & easy.

Problem With this Code...Please Help

P: n/a
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;
}
Jul 22 '05 #1
Share this Question
Share on Google+
12 Replies


P: n/a

"1111111111" <eh***@hotmail.com> wrote in message
news:3f**************************@posting.google.c om...
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
Jul 22 '05 #2

P: n/a

"1111111111" <eh***@hotmail.com> wrote in message
news:3f**************************@posting.google.c om...
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
Jul 22 '05 #3

P: n/a
On 18 May 2004 11:58:28 -0700, eh***@hotmail.com (1111111111) wrote:
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

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

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


--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html
Jul 22 '05 #4

P: n/a
1111111111 wrote:
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.
Jul 22 '05 #5

P: n/a
* eh***@hotmail.com (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.

--
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #6

P: n/a

"Alf P. Steinbach" <al***@start.no> wrote in message
news:40****************@news.individual.net...
* eh***@hotmail.com (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.


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.)

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. 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
Jul 22 '05 #7

P: n/a

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
Jul 22 '05 #8

P: n/a
Alf P. Steinbach wrote:
* eh***@hotmail.com (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).


No, do not use that one. A much better free one is availible from Microsoft
here:
http://msdn.microsoft.com/visualc/vctoolkit2003/

- Pete

<snip>
Jul 22 '05 #9

P: n/a
1111111111 wrote:

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
Jul 22 '05 #10

P: n/a
1111111111 wrote:

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
Jul 22 '05 #11

P: n/a
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

eh***@hotmail.com (1111111111) wrote in message news:<3f**************************@posting.google. com>...
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;
}

Jul 22 '05 #12

P: n/a
> > Get yourself a C++ compiler, e.g. mingw g++ (it's free).

No, do not use that one. A much better free one is availible from Microsoft here:
http://msdn.microsoft.com/visualc/vctoolkit2003/


This oughta be rich........................ ;-)
Jul 22 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.