Thread: Request for a beginner program

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    18

    Request for a beginner program

    I have just finished Chapter 5 of the book Accelerated C++. Lately, however, I no longer feel like doing the exercises at the end of the chapters anymore. I want to rush through this book so that I will be able to create a program on my own as soon as possible, but that obviously is not advisable. I suppose I'm an impatient type who likes to see the result quickly...

    That said, I still want to create a program (exercises are programs, but you know what I mean >_<), and since rushing through the book won't help, I'm going to do something with what I already know right now.

    Roughly, the main things that I have learned so far are: loops; creating functions; vector, list, and string container types; returning error (although I'm extremely weak on this one); struct; separate compilation; in addition to other language features(?) such as typedef, const, using reference, etc.

    So... with what I have already known so far, are there any useful beginner/basic programs that I can actually write? Generally, I'm interested in games, and with what I have learned, I think I can do something like hangman (or can I?).

    Thank you for any suggestions.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You don't need to finish the book to start writing programs.

    Sure your first ever hangman will be rough, but as you keep reading and learning, you'll find better ways of solving the problems.

    Like doing cout << a[0] << a[1] << a[2] ... << a[n], then discovering for loops.

    Try a series of evolutionary steps
    - simple console hangman with a fixed word list
    - import word list from a file
    - add difficulty levels - harder levels choose longer words, or limit response times.
    - add demo mode which plays if user does nothing for a while
    - switch to a graphical display
    - add animation and sounds
    - add a network server option to allow remote play

    It's all the same program, but you'll learn a lot of new stuff with each step. One aspect of C++ you should focus on is the proper design of your classes. If you find yourself re-writing a lot of code with each new step, then you're doing something wrong (for example modifying the dictionary code if you add graphics).



    I don't think I've ever finished a book AND done all the exercises.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User guesst's Avatar
    Join Date
    Feb 2008
    Location
    Lehi, UT
    Posts
    179
    Okay, don't peek if you don't want to have it spoiled for you, but here's my hangman which can read from a file or use its own array.

    Ooh, actually that's the version I wrote that uses getche. I need to pull out the one for getchar when I put it on my website, which I'm trying to make a perfect inspiration point for beginners.
    Type-ins are back! Visit Cymon's Games at http://www.cymonsgames.com for a new game every week!

  4. #4
    Weak. dra's Avatar
    Join Date
    Apr 2005
    Posts
    166
    imo you should keep doing at least a few exercises per chapter. They help you apply what you learned and some of them can be quite challenging (in a good way).

  5. #5
    Registered User guesst's Avatar
    Join Date
    Feb 2008
    Location
    Lehi, UT
    Posts
    179
    BTW, what sort of challenges does the book present.
    Type-ins are back! Visit Cymon's Games at http://www.cymonsgames.com for a new game every week!

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    what sort of challenges does the book present.
    Problems like:
    5-10. Palindromes and words that are spelled the same right to left as left to right. Write a program to find all the palindromes in a dictionary. Next, find the longest palindrome.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    May 2008
    Location
    Paris
    Posts
    248
    imo you should keep doing at least a few exercises per chapter. .
    Or even better, and nicer, make your own examples!

  8. #8
    Registered User Kudose's Avatar
    Join Date
    Jun 2006
    Posts
    92
    Quote Originally Posted by laserlight View Post
    Problems like:
    5-10. Palindromes and words that are spelled the same right to left as left to right. Write a program to find all the palindromes in a dictionary. Next, find the longest palindrome.
    I need a book with cool exercises like that.

    Or maybe laserlight could create such a book of exercises and make millions?

  9. #9
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    ... I have learned so far are: loops; creating functions; vector, list, and string container types; returning error... struct; separate compilation; in addition to other language features(?) such as typedef, const, using reference, etc...

    So... with what I have already known so far, are there any useful beginner/basic programs that I can actually write?
    I think you know enough to make a simple game... I suggest you take a break for a day or two and think about what you've learned, and how you might use that knowledge to make a game. If there's something you want your game to do, and you don't know how to do it, you should still be able to get started on the game. And, you can look-ahead through the book or do some online research. (Of course, you can only make text-based games at this point.*)

    There are two major concepts that make programming (in any language) useful... The first is branching (if-statements and switch-statements). I assume you've learned about these. Branching is how programs "make decisions". The second is looping (doing stuff over-and-over), which you have learned. Once you understand these two concepts (and a few other basics) you can start to write useful, interesting, programs.

    ...I'm not saying it's as simple as that. C++ is a complex language, and there is much more to learn than how to make a loop or if-statement.

    One more thing you'll need to know before making a game is how to make things random (unpredictable). If you haven't covered it yet, look-up rand().


    * In case you don't know, there are no graphics in ANSI Standard C++. You'll need to use an additional graphics library. Before you jump-into something like that you should have a good understanding of Standard C++ (i.e. The topics in Accelerated C++.) These additional libraries are (mostly) function-libraries, so you need to have a good understanding of all-kinds of functions. (Some libraries use lots of structures and/or classes too.)

    And as you probably know, most commercial games are written by a team of programmers. If it takes 20 programmers a year to make a game (20 man-years), it would take 20 years for one expert-programmer to write the same program/game!
    Last edited by DougDbug; 06-06-2008 at 03:13 PM.

  10. #10
    Registered User
    Join Date
    May 2008
    Posts
    18
    Quote Originally Posted by Salem View Post
    Like doing cout << a[0] << a[1] << a[2] ... << a[n], then discovering for loops.

    Try a series of evolutionary steps
    - simple console hangman with a fixed word list
    - import word list from a file
    - add difficulty levels - harder levels choose longer words, or limit response times.
    - add demo mode which plays if user does nothing for a while
    - switch to a graphical display
    - add animation and sounds
    - add a network server option to allow remote play

    It's all the same program, but you'll learn a lot of new stuff with each step. One aspect of C++ you should focus on is the proper design of your classes. If you find yourself re-writing a lot of code with each new step, then you're doing something wrong (for example modifying the dictionary code if you add graphics).

    I don't think I've ever finished a book AND done all the exercises.
    Lol... I probably can't even start this since I haven't learned it. How do you get word from a word list anyway?

    Also, I haven't learned designing classes either. The book has taught on designing functions, though.

    Quote Originally Posted by dra View Post
    imo you should keep doing at least a few exercises per chapter. They help you apply what you learned and some of them can be quite challenging (in a good way).
    (Some of) the exercises are indeed challenging. Just that after realizing I have only finished 1/3rd of the book so far, I want to at least try making something from what I have already learned.

    Quote Originally Posted by DougDbug View Post
    I think you know enough to make a simple game... I suggest you take a break for a day or two and think about what you've learned, and how you might use that knowledge to make a game. If there's something you want your game to do, and you don't know how to do it, you should still be able to get started on the game. And, you can look-ahead through the book or do some online research. (Of course, you can only make text-based games at this point.*)

    There are two major concepts that make programming (in any language) useful... The first is branching (if-statements and switch-statements). I assume you've learned about these. Branching is how programs "make decisions". The second is looping (doing stuff over-and-over), which you have learned. Once you understand these two concepts (and a few other basics) you can start to write useful, interesting, programs.

    ...I'm not saying it's as simple as that. C++ is a complex language, and there is much more to learn than how to make a loop or if-statement.

    One more thing you'll need to know before making a game is how to make things random (unpredictable). If you haven't covered it yet, look-up rand().


    * In case you don't know, there are no graphics in ANSI Standard C++. You'll need to use an additional graphics library. Before you jump-into something like that you should have a good understanding of Standard C++ (i.e. The topics in Accelerated C++.) These additional libraries are (mostly) function-libraries, so you need to have a good understanding of all-kinds of functions. (Some libraries use lots of structures and/or classes too.)

    And as you probably know, most commercial games are written by a team of programmers. If it takes 20 programmers a year to make a game (20 man-years), it would take 20 years for one expert-programmer to write the same program/game!
    And graphic artists and sound producer and etc. I guess...

    I have learned about looping, but regarding branching, I have only learned about if and else statements. Never heard of switch before. Also, I forgot to mention it for some reason, but I just learned about iterator this chapter.

    I was actually looking for some ideas on what simple program I can write that I should write, but I guess I will go with hangman.

    Thank you for the help. Since I'm having school project and my final exams are coming soon (along with a whole bunch of games I want to play), I don't know when I will finish this, but I will post my code then (and when I get stuck).


    I have one more thing I want to ask about what I have read in the book. When you write a function, what's the purpose of having the return type as reference?

    For example, let's making up a function:

    Code:
    vector<double>& function(vector<double>& d)
    {
    
    }
    The book mentions that when a reference is used as an argument, you are telling that you want to change the value of whatever you pass in the argument. But what is the reference to the return type for? Is it to save the workload since the value is returned, instead of being copied then returned?
    Last edited by tiachopvutru; 06-06-2008 at 06:49 PM.

  11. #11
    Registered User
    Join Date
    May 2008
    Posts
    16
    This world is full of games. How about something useful? Do you have one of those graphing calculators that can solve algebra equations (TI 89)? Write a program that will do just that. say you input 2 * x + 4 = x + 2 and it solves for x.

  12. #12
    verbose cat
    Join Date
    Jun 2003
    Posts
    209
    An even simpler game you could make would be a Number Bowling game (something I wrote in Basic when I was in 9th grade).

    10 frames (like bowling)
    each frame:
    - you get 3 random one digit numbers (like 6, 5, 1)
    - arrange the numbers with normal +, -, * and / to make the 3 digits total as close to 30 as you can without going over ( (5 - 1) * 6 = 24, (6 - 1) * 5 = 25, (6 * 5) - 1 = 29, etc.)
    - add that frame's total to the running total
    At the end of 10 frames, you will have a total score out of a possible 300 (like Bowling).

    I suggest this because it involves using math instead of text, and it is a very simple program that won't need more than a few functions, a few loops, a switch statement or two and some if statements. Once you get that working, you could add a vector to keep track of all the scores in each frame to let the player review how they did, maybe some simple text graphics (a few !!'s to show a visual representation of how many pins were left in the frame for example).

    Once you get that down, writing a hangman game would be a nice step since it would be somewhat similar. You would have to change a few functions to play the game of hangman instead of just doing a bunch of math (which is something you have to do for any program you write), and learn how to use std::string and some of the text processing abilities of strings...
    abachler: "A great programmer never stops optimizing a piece of code until it consists of nothing but preprocessor directives and comments "

  13. #13
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    This world is full of games. How about something useful? Do you have one of those graphing calculators that can solve algebra equations (TI 89)? Write a program that will do just that. say you input 2 * x + 4 = x + 2 and it solves for x.
    That is a lot more difficult than it seems. Definitely out of the current skill level of the OP. (parsing equations)

    *edit* oops, sorry for reviving this semi-ancient thread =) */edit*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 10:58 PM
  2. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  3. my server program auto shut down
    By hanhao in forum Networking/Device Communication
    Replies: 1
    Last Post: 03-13-2004, 10:49 PM
  4. Replies: 3
    Last Post: 01-14-2003, 10:34 PM
  5. redirection program help needed??
    By Unregistered in forum Linux Programming
    Replies: 0
    Last Post: 04-17-2002, 05:50 AM