Public Group

# [C++] Recommend me some small projects

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

## Recommended Posts

What are some good projects in order of difficulty for a beginner of C++? They don't necessarily have to be game-oriented, but that would be nice. Thanks :) [Edited by - rippingcorpse on March 24, 2008 12:57:07 PM]

##### Share on other sites
guess a number
hang man
tetris
life force/ gradius/r-type some sort of shooter or favorite 8 bit game.

##### Share on other sites
Be warned, you may find this progression fairly steep. A true beginner also won't be able to make sense of all of this yet. It will all fall into place as you work your way through.

1) "Hello World", or as at least one game programming book calls it, "Game Over". Just output a message.
2) Get a line of text from the user and output it backwards. Try several approaches to the problem. Also try getting the "line" (probably only a word) from the command line arguments.
3) Prompt for two numbers and report the product. Add in all the appropriate error handling: for each number, repeatedly ask until you actually get a valid number. Read the standard input a line at a time for this, and ignore any "garbage" text that appears after a valid number on the line (but do complain about garbage at the beginning).
4) Guess the number.
5) Rectangle blitter: Ask for an x-position, y-position, width, height, and symbol, and draw a corresponding rectangle filled with that symbol onto the console. Assume that the first character you output will appear at the top-left of the console window; output enough characters to fill one screen of the console. You don't get full marks until you have robust error handling. Note: if part of the rectangle would be off-screen, make sure you only draw the on-screen part. (It is ok if none of it is on screen; just output a whole field of spaces.)
6) Mad Libs. Read an input file that contains placeholders marked by square brackets (you may assume there are no other instances of square brackets in the file). As you read not-enclosed text, append it to an output string. When you find a square bracket, read the text between the square brackets (it will be some kind of description, like "proper noun") and prompt the user to supply something of that sort. "Fill in the blank" by appending it to the output string. Once you reach the end of the file, output the whole output string.
7) Hangman. This isn't necessarily harder than Mad Libs, but it's more like a "complete game". Read in a word list from a file. Ensure that there are no arbitrary limitations on how many words there are, and pick a random word each game (make sure you are using the srand() properly).
8) Rectangle blitter II. Keep a buffer in memory of the desired screen contents. Ask the user several times to give rectangle coordinates and a symbol, and update the contents of the buffer, then redraw. You may also want to add a (platform-specific) call to make sure that the output lines up with the console window. Also, accept two symbols for each rectangle. Use the first to draw a border, and the second for all the "inside" spaces. The usual error handling standard applies here.
9) Mad Libs II. In this one, you are also responsible for designing the necessary changes to the input file format, and documenting them.
a) Provide some kind of escape syntax that allows the final output to have square brackets in it.
b) Provide a way to "mark" placeholders with an ID to indicate that they should reuse a common value. (When you're telling a story, you want to keep the protagonist's name consistent, for example.)
c) Do error checking to make sure that tags are properly closed. Your escape syntax needs to allow for square brackets in the descriptive text and in the ID for the placeholder, as well as anything else that might be ambiguous.
d) Parse the whole file before you start asking for text from the user. Find some structured way to hold the data as you parse it. If you encounter any error, report it and bail out. (If you are making proper use of the standard library, and/or have learned your stuff about constructors/assignment operators/destructors properly, the data representing the partially-parsed file should get cleaned up automatically.)
10) Rectangle blitter III. Now we are going to model "viewports" in addition to rectangles. A rectangle has a size (width and height), and data that is read from a file. A viewport has a size (width and height) and location (top and left). Write a function that draws a rectangle into a viewport, with a given offset. Use a structure to represent (x,y) coordinate pairs. An offset of (0,0) means that the top-left of the rectangle aligns with the top-left of the viewport; increasing the X offset moves the rectangle to the right, and increasing the Y offset moves it down. Any part of the viewport not covered by the rectangle should be filled with spaces. If the file data for the rectangle is too short, pad with '#' symbols (don't include any in your data files, so that you can see if this has happened); if it is too long, just ignore the excess (this should be automatic if you are doing things in a sane way :) ).
Design the user interface so that the user can create viewports and rectangles (providing all the necessary information for construction), ask for a rectangle to be blitted into a viewport, or remove a viewport (the interesting part: how will you know which one to remove? How will the user be able to tell you?). Only one rectangle should be blitted into a viewport at once: if another "blit rectangle into viewport" call is made, it should replace the existing contents. Figure out what happens if viewports overlap: which is drawn "on top"? Is it consistent? Explain and document the behaviour.
11) jwalsh's Project 2, from the C++ Workshop.
12) Create a representation of a directed graph structure. Make a node structure that represents a point in the graph. It should have a container of handles to the nodes that are reachable from here. Also make a graph class that simply holds a container of all the nodes. Write all the member functions for the graph that you think are useful for creating and modifying a graph. You should be sure that a node will never "duplicate" a connection to one of its neighbours, or point to a non-existant node, and that you never leak memory. Also make sure that your container choice in the graph class, and your handle choice in the node structure, will "work": i.e. a change to the graph should never cause any corruption to other nodes.

Write a function to determine how many separate "pieces" there are in the graph.

By this point, you should be more than ready to start looking at some basic graphical APIs.

##### Share on other sites
Quote:
 Original post by Zahlman2) Get a line of text from the user and output it backwards. Try several approaches to the problem. Also try getting the "line" (probably only a word) from the command line arguments.

Am I allowed to look this up? I don't see how I would know this just after doing the "Hello World" program without learning some sort of reverse function or looking it up online.

##### Share on other sites
Learning things pretty much requires looking things up. I did say "try several approaches" for a reason. :)

There is such a reverse function in the standard library. It's also not difficult to do by hand, if you consider that a string is basically a sequence of characters. First figure out how to swap two characters, and then figure out how to use swapping of pairs of elements to reverse a sequence.

##### Share on other sites
I'm currently teaching myself Python + Ruby. Will this general progression work for me as well, or is there some alternate path that'd be more suited?

##### Share on other sites
It will work for you as well some will be easier because of the libraries that ship with the language.

##### Share on other sites
How come the following code doesn't work?:

#include "stdafx.h"#include <iostream>using namespace std;int main(){	char cString[50];	char cTemp;		system("CLS");		cout << "Enter a string to write backwards: " << endl;		cin >> cString;				for(int i = 0; i < 50; i++)		{			cTemp = cString;			cString = cString[i + 1];			cString[i + 1] = cTemp;		}		cout << "The string backwards is " << cString << endl;}

##### Share on other sites
Quote:
 Original post by rippingcorpseHow come the following code doesn't work?:*** Source Snippet Removed ***

Specifically, use std::string instead of char[]. If you're going to learn c++, you might as well learn c++ (meaning the standard libraries, as well as the syntax). That's something that a lot of us could work on more, actually, myself included.

##### Share on other sites
Also, in general, you want to define "doesn't work": give the exact compiler errors (if you find you're getting dozens, post the first one – some problems have a tendency to cause a whole long chain). If you've cropped out code (which is good, so long as it's not done to excess), try to indicate where the line of code with the problem is (the compiler will tell you).

1. 1
2. 2
3. 3
Rutin
19
4. 4
5. 5

• 9
• 19
• 9
• 31
• 16
• ### Forum Statistics

• Total Topics
632617
• Total Posts
3007459

×