473,480 Members | 4,939 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Problem With this Code...Please Help

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
12 1500

"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

"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
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
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
* 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

"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

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
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
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
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
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
> > 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
3727
by: Kostatus | last post by:
I have a virtual function in a base class, which is then overwritten by a function of the same name in a publically derived class. When I call the function using a pointer to the derived class...
2
1545
by: Fabrice Labrousse | last post by:
Hello, Here is a hard problem i cannot solve about window.opener method I think you'll need to be a specialist to find the solution... i can't find the solution by myself ! Please help me. Here...
3
3820
by: Spacy | last post by:
Am creating a HTML Report in asp.net. To save this report to excel on client-side, i write this code: Response.ContentType = "application/vnd.ms-excel" Response.AddHeader("content-disposition",...
5
2616
by: Henry Jordon | last post by:
ok I have my problem entering in an expression in infix notation and it outputs the postfix notation. I now need to evaluate the postfix notation. I have some code written and there are comments as...
28
5165
by: Jon Davis | last post by:
If I have a class with a virtual method, and a child class that overrides the virtual method, and then I create an instance of the child class AS A base class... BaseClass bc = new ChildClass();...
9
2799
by: Rajat Tandon | last post by:
Hello there, I am relatively new to the newsgroups and C#. I have never been disappointed with the groups and always got the prompt replies to my queries.This is yet another strange issue, I am...
2
7130
by: almurph | last post by:
Hi everyone, Can you help me please? I am having a problem with the encryption/decryption of words with the Irish fada in them. The Irish fada is like this: áéíóú/ÁÉÍÓÚ. It's kind of like the...
2
1606
by: cty0000 | last post by:
Please anybody help me... I have some serious problem.. I'm doing to keep equpiment list(string).. In my code, there are 3 page which are having 4 equpiment ID (user control.) like this...
8
1632
by: cpptutor2000 | last post by:
Could some C guru please help me? I have a function that takes as an argument a pointer to an array of unsigned chars (basically a hex representation of a dotted decimal IP address). When I print...
0
1952
by: Filemaxor | last post by:
I have gotten my code to be able to allow people to add new text to a .txt document and able to call up files so the user can see whats in it. The problem i'm having is getting the for loop to work...
0
7040
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7080
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
6908
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5331
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4772
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
2980
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1299
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
561
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
178
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.