Sign in to follow this  

My First Good Program Makes ASCII Shapes

This topic is 3934 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, fellas. As a new member of these forums, and being a person of propriety, I would like to introduce myself. I have very little experience with development. I'm something of a web developer, so I can hack existing PHP programs, I have for years. However, other than that, I don't have very much to show for myself. A friend convinced me to learn Python. I figured out the basics, it was fun, but as I did more research, I got the feeling that I wouldn't really be able to use it for the things I wanted to do, such as to help the good folks over at openFrag with their game engine. A note about openFrag-- Although the engine is young, these people are really great people. Hard working, and helpful. I feel that if I am to become a real developer, there's no better project to join-- Levia, he started C++ programming only eight months ago, and he's already very good. Now, I would like to present to you fine facilitators of facile function factoring a medium mobbed by a modicum of mediocrity of melodramatic measure. Alliteration aside, I have placed some code here: http://files.openfrag.org/Members/CryptoQuick/c++/Shapes/ I'm not entirely familiar with the way programs and source are distributed over this forum, but I find this to be an acceptable manner. A little background on this program, now. I am reading Accelerated C++, by Addison Wesley, 2000. I just finished chapter 2. In the details section, the last section of the chapter, they asked me to do this: Write a set of "*" characters so that they form a square, a rectangle, and a triangle. It inspired me to write my program. I have to confess, I made little effort to clean the source up before I uploaded it. I left in some of my old code, because I just hate deleting my work, and also, when I got the feeling that I finished the project, I no longer felt the impetus to do any more work. It's strange, but I want to break out of that, because I never finish anything. I will now impose some inquiries. First, what do you think of the way I wrote it? Could I have optimized the code any? I don't know OOP, and I'll bet I could have done it better if I just read more of my book. Also, how do I make the program go back to the beginning of main() once it's displayed its result? Last, how in heck would you propose I make an ASCII circle? Just give me a hint! I'll try to figure it out on my own. Questions, comments, criticism, all feedback is appreciated by this one Alex Trujillo. Thank you!

Share this post


Link to post
Share on other sites
Quote:
Now, I would like to present to you fine facilitators of facile function factoring a medium mobbed by a modicum of mediocrity of melodramatic measure.


Nice.

Looking at your code quickly, it seems well organized and all I see is one poor-ish habit.

From your code...


for (int b = 0; b != height; ++b)
{
for (int a = 0; a != width; ++a)
{
cout << filler_symbol;
}
cout << "\n";
}



While it's not a problem in this case, it's a little dangerous to only check a single value when working with loops. Using a > or < check is much safer in preventing crashes and infinite loops.

Like so...


for (int b = 0; b < height; ++b) {
for (int a = 0; a < width; ++a) {
cout << filler_symbol;
}
cout << "/n";
}



If your variable happens to skip past height or width, you're still covered by a bigger range of failure in the conditional statement.

Share this post


Link to post
Share on other sites
hereticprophecy: You know, it's funny, the book used !=, but I saw < and > used a lot more in my PHP days. It makes a lot of sense, though, now that you pointed it out and now that I think about it. There were two times when I incremented a value by two. One wrong move and we'd have a problem.

Thanks. :)

Share this post


Link to post
Share on other sites
To do a circle, i would suggest looping through like a square, and using some trig(pythagorean theorum would work fine) to test the distance of each char to the center(to see if it is within the radius specified).

Share this post


Link to post
Share on other sites
A couple of comments from me:


  • You're right in saying that it's not very object-oriented, but that's OK. Object orientation is a tool, not a goal, and it's not the right tool to use in every situation. There may be some aspects of your program that could be improved through the use of OOP but you can consider those once you've started covering them in your book.

  • If you don't want to obliterate your old code, get yourself set up with a source control system like Subversion. Set up a server on your own computer for your own personal use. What it can do is keep a 'history' for your source files, tracking every change you make over time, so that if you're going to replace some old code with a new version it can save the old version for you to go back to if necessary. Then you can remove it from the current version of the file to keep things neat and tidy.

  • On line 161, you use the \n escape code instead of endl. For consistency it would probably be good to use endl. It's also probably a good idea to split that line up into multiple lines so that you don't have to scroll sideways.

  • When you start testing the 'shape' string, you can simplify the comparison by collapsing the possible values the player has entered into a smaller space. For example, you can convert every character in the string to lowercase, and that way you don't need to test for separate "Rectangle" and "rectangle" cases - you'll also catch weird things like "rEcTaNgLe" too.

Share this post


Link to post
Share on other sites

This topic is 3934 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this