takashi wrote...
I am learning about how to use c++ language. I have attempted to make
my own programs, using the knowledge that I have, but sometimes when
I get stuck on writing a code, it took me a long time to figure out
what I should do. [...] Could anybody tell me how more experienced
programer makes his plan for writing his code, so that there is less
chance that he gets stuck while he's writing his code?
That's a very deep question, and there are whole books of advice on this
subject, with various different approaches. I would suggest that you
start by working on two skills that are generally useful in any
programming you do: using pseudocode and modular design.
If you're going to implement an algorithm in C++, then obviously you
must know what it needs to do before you start. If you can't describe
what it does in natural language (or mathematics, if you prefer) then
you can't possibly implement it in a programming language.
For example, suppose I've got a file of the form
Fred Smith,
fr**@smith.com
John Doe,
jo******@hotmail.com
and I want to write a function that reads in this file, extracts the
name and e-mail address from each line, and stores them somewhere for
future use.
I might represent this algorithm slightly more formally, in
"pseudocode", as follows:
Open file
For each line of text in the file
Parse the line, extracting name and e-mail address
Store name and e-mail address
Close file
It doesn't matter exactly what notation you choose to use, and with
experience you'll develop your own shorthands that you find useful. The
important thing is that this is a more structured representation of your
algorithm.
Some people prefer to use various diagrams for this step, or to start
with diagrams and then turn them into pseudocode. Personally, I've
always found flow charts and such cumbersome, but to others they're much
more natural. Use whatever works best for you.
Once you've got some pseudocode, however you choose to write it,
implementing the real thing is a piece of cake: you just substitute
formal C++ for each step in the pseudocode.
Personally, I find it useful to turn my pseudocode into comment lines,
and then replace them one at a time. For example, I can open a file in
C++ by creating an ofstream object, and that also takes care of the
closing at the end of the function, so my code becomes:
ifstream data_file("addresses.dat");
// While a line of text is available
// Parse the line, extracting name and e-mail address
// Store name and e-mail address
I continue to replace each line of pseudocode systematically with some
C++ that implements it:
ifstream data_file("addresses.dat");
string line;
while (getline(data_file, line)) {
// Parse the line, extracting name and e-mail address
// Store name and e-mail address
}
and so on.
In this case, the remaining steps might each be quite complicated, so
maybe it's best to put them in separate functions, which you can write
later:
ifstream data_file("addresses.dat");
string line;
while (getline(data_file, line)) {
string name;
string address;
Parse(line, name, address);
Store(name, address);
}
(Obviously there are more elegant ways of doing this in reality, but the
above is intended to illustrate my point about replacing pseudocode
iteratively.)
When you've finished replacing your pseudocode with C++, you wind up
with a completely implemented function. Essentially, this process breaks
a hard question ("How do I implement this algorithm?") down into lots of
simpler questions ("How do I represent this loop using tools in C++?").
It's much easier to answer simple questions. :-)
The other skill -- how to design a larger program -- usually comes down
to one key skill: modular design. There are so many books and on-line
articles about this that there's little point in my saying much here. If
you're into OO, you might read a few of the articles over at ObjectMentor:
http://www.objectmentor.com/resources/articleIndex
They have a very heavy pro-OO bias, and there are certainly other useful
approaches you can take, but they do a good job of illustrating the
basic principles, and an inexperienced programmer could learn a lot of
useful techniques there.
Hope that helps,
Chris