473,787 Members | 2,931 Online
Bytes | Software Development & Data Engineering Community
+ 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 1538

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


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.goo gle.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!!!


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.c om (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<<"Pleas e enter side 1: ";
cin>>side1;
cout<<"Pleas e 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<<"*"<<set w(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.c om (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.i ndividual.net.. .
* eh***@hotmail.c om (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.c om (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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
3762
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 (ClassB* b; b->func(); ) the base-class function is called instead of the new function in the derived class. All other similar functions (virtual in the base class and overwritten in the the derived class) work fine, it's just this one function. ...
2
1568
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 is the problem (quiet complicated to explain). I use two differents servers with two IIS 4.0 Web servers On the first one (let's name it Server1) i got my web site. On my web site,
3
3857
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", "attachment; filename=Report.xls") But once the report is saved in excel, on click of hyperlink for second html report, i dont get the second report. the same old first html report is displayed. Please can anyone help me. Am frustrated with...
5
2641
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 to what I want to do but am unable to get it to work. So if someone could please help me it would greatly be appreciated. Thanks for your help. code: #include <cstdio> #include <cstdlib>
28
5223
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(); .... and then call the virtual method, why is it that the base class's method is called instead of the overridden method? How do I fix this if I don't know at runtime what the child class is? I'm using Activator.CreateInstance() to load the...
9
2824
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 facing. Please please help me to solve this as soon as possible. So here we go ... I am not able to take the screen shot of the windows form based "Smart
2
7151
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 French grave... Anyway when I run encryption on a plaintext word like:
2
1623
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 window FORM ==================================================================
8
1659
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 out the received values in the receiving function, I get something completely different from what I passed in. The following are the relevant code snippets: In the calling function: unsigned char* TempAddrs = {"0xC0", "0xA8", "0x00", "0x63"};
0
1976
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 correctly so that i can allow certain indexes to be removed without completely deleting everything else. This is what i have so far. Code is below It's in the e part of the code and I have //remed the location of the for loop containg the...
0
9655
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10363
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10172
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10110
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9964
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7517
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5398
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5535
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4069
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 we have to send another system

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.