# C++ Practice lessons

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

## Recommended Posts

Hi All, new here and getting into programming C++. I've picked up the book Beginning C++ throught Game Programming, third ed and it's been quite helpful. The one problem is it doesn't have questions or many practice lessons to challenge the minute progress I make every day. Anyone have a link to practice questions that gradually increase as someone learns the language? I'd love to see 4 or 5 different pratcial coding questions after I learn something fundamental. For example I just read a chapter on basic do/while loops, the use of switch, etc and would love some basic challenges in putting the knowledge into solving a problem. Thanks for any feedback or help.

##### Share on other sites
I don't think you need practice questions. You need to work on a non-trivial program. As you learn, you'll find new ways to do things. You'll end up re-writing the program with new features and techniques you learn along the way. Test questions and example programs serve to give practice on one feature only. This is generally good, but you can do it yourself. When you learn a new feature or technique, apply it to your program.

What the program is is up to you. If you think you're ready, you can make a Pong or Arkanoid game. If not, a simple text-based RPG battle simulator or something can be fun and give you good practice. I'm not a big fan of "homework problems," just go code something.

##### Share on other sites
If you really need them look up any Deitel book.
They got tons of exercise problems you could do. I think that's why alot of colleges use their book even though it sucks to read!
For example:
4.26 A palindrome is a number or a text phrase that reads the same backwards as forwards. For example, each of the following five-digit integers is a palindrome: 12321, 55555, 45554 and 11611. Write a program that reads in a five-digit integer and determines whether it is a palindrome. [Hint: Use the division and modulus operators to separate the number into its individual digits.]

4.35 The factorial of a nonnegative integer n is written n! (pronounced "n factorial") and is defined as follows:

n! = n · (n 1) · (n 2) · ... · 1 (for values of n greater than to 1)

and

n! = 1 (for n = 0 or n = 1).

For example, 5! = 5 · 4 · 3 · 2 · 1, which is 120. Use while statements in each of the following:

Write a program that reads a nonnegative integer and computes and prints its factorial.

Write a program that estimates the value of the mathematical constant e by using the formula:

Prompt the user for the desired accuracy of e (i.e., the number of terms in the summation).

Write a program that computes the value of ex by using the formula

Prompt the user for the desired accuracy of e (i.e., the number of terms in the summation).

Personally I don't care for HW since I'd rather be making games and that's the whole point your learning to program isn't it?
To make your own custom apps/games just the way you want them not what someone else wants unless you are planning on working as a programmer for a living.
I think you'd learn alot more if you think of something you want to make for example console battleship,hangman,tictactoe,card,etc game. Start from scratch and see if you can make it with your current knowledge and only if you get stuck look it up or ask for help here. It's the only way you're truly gonna learn.

p.s. And if you want really hard exercise problems lookup a book called Oh! Pascal! it's got the most brainteaser programming problems I ever saw in a beginners book for example:
6-15 Straighten out a paperclip, ask yourself this question: suppose you cut the clip in two, then bend one part into a circle, and the other into a square. How long should each portion be to yield figures with equal areas?
6-23 A biologist doing research into phermones decides to drive a few bugs crazy. She places 4 bugs in the corners of a square test area, then douses each with a chemical that is sure to attract it's right-hand neighbor. Driven by genetics each bug starts walking counterclockwise towards it's neighbor.
Now each bug walks at the same speed. Before moving one of its little bug feet, the bug may change direction slightly so that its heading directly towards its quarry. Write a program that answers these questions: Where do the bugs meet?
How far has each bug walked, measured along its curved path, when they all finally collide?

[Edited by - daviangel on February 10, 2008 3:02:53 AM]

##### Share on other sites
I didn't want to make a new thread so I will post about my problem here. I hope you don't mind John [smile]

I am not getting the required output for this simple program [depressed]

#include<fstream>#include<string>using namespace std;ofstream fout;ifstream fin;int main() { char fileName[80]; char buffer[255]; // for user inputfout<<"File name:"; fin>> fileName; ofstream fout("fileName.txt"); // open for writing fout<< "This line written directly to the file...\n"; fout<< "Enter text for the file: ";  fin.ignore(1,'\n'); // eat the newline after the filename  fin.getline(buffer,255); // get the user's input  fout<< buffer << "\n"; // and write it to the file fout.close(); // close the file, ready for reopen ifstream fin("fileName.txt"); // reopen for reading fout<< "Here's the contents of the file:\n"; char ch; while (fin.get(ch)) fout<< ch; fout<< "\n***End of file contents.***\n"; fin.close(); // always pays to be tidychar response;fin>>response; return 0;}

This is a program from an excellent book called "Teach yourself C++ in 21 days" I have hardly made any changes. I have tried few things and also included char response, fin>>response before return statements because the output is not displaying. But nothing still happens. Can anyone tell me what is wrong.

Here is the required input output example.
Quote:
 File name: test1Enter text for the file: This text is written to the file!Here's the contents of the file:This line written directly to the file...This text is written to the file!

But it doesn't ask for any input [dead] and only a file "fileName" is created with
Quote:
 This line written directly to the file...Enter text for the file:

##### Share on other sites
You're reading from fout and writing to fin before they are opened. You want to be reading user input from cin and printing console output to cout.

Also, why use this thread? This seems completely unrelated. There's nothing wrong with starting a new thread.

##### Share on other sites
Quote:
 I didn't want to make a new thread so I will post about my problem here.
Do NOT do this. Even if the OP doesn't care, the rest of us will be very unhappy with you for hijacking someone else's thread. If you have a question that clearly doesn't fit in an existing thread (like say, it's your own and not the OP's) then create a new thread. We simply will not help if you hijack other threads with your own questions.

##### Share on other sites
Ok guys sorry [depressed]
Yes... cout,cin was also used in the book but I got some error so I used fout, fin
PS: no more post here I will scan through streams chapter again and make a new thread if have more doubts [imwithstupid]

##### Share on other sites
Thanks to all for the feedback.
Posts in this thread plus judicious use of search has neeted me some good practice stuff.

One problem Im seeing is perhaps my initial choice of C++ might be in question as it seems there are some threads that suggest beginner's consider using C# or Python. Then pursue C++ if required later.

Windows will be my chosen platform for the business plan I'm formulating. I don't intend to be the primary programmer, but rather cull the local talent at myAlma Mater/local college ;)

I want to be knowledgeable enough to pick the right people to code my magnus opus, but prefer not to spend all my time in an IDE editor.

So C++, C#, or Python?

I'll do as another post recommended and download all three and spend some time with 'em.

Thanks again for all the feedback

##### Share on other sites
Quote:
 One problem Im seeing is perhaps my initial choice of C++ might be in question as it seems there are some threads that suggest beginner's consider using C# or Python. Then pursue C++ if required later.
This is the general consensus from experienced programmers. Because there's two things that inevitably happen.

You either do not progress very far as a programmer. You then want to make sure whatever you do care to learn is of most practical use. In which case, you're better of learning a high level language with large core libraries. Python, C#, and Java are very good for this reason. Ruby is also extremely solid, and I placed it above C# and Java. Of course, you may eventually progress to be at the level of a professional. In which case, you will learn multiple languages anyway, so learning Python or C# first works out in your favor anyway. Well, that and C++ is definitely not beginner friendly. C is very not beginner friendly, and C++ in maintaining compatibilities with C does not help either.

Quote:
 Windows will be my chosen platform for the business plan I'm formulating.
If you already fixed Windows as your platform, it makes choosing C# even more appealing, as it practically ties you to Windows. Not a problem if you're okay with that.

Quote:
 I want to be knowledgeable enough to pick the right people to code my magnus opus, but prefer not to spend all my time in an IDE editor.
Definitely don't pick C++. The learning curve is enormous.

If you're doing Windows development, C# really isn't a bad choice. Seriously, MS is exposing their modern APIs in .NET only. They are effectively pushing C# as the main Windows development language, and its working. So you get to choose between Python and C#.

##### Share on other sites

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

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628652
• Total Posts
2984053

• 10
• 9
• 9
• 10
• 21