K
K. Frank
Hello Group!
Could people suggest a simple program that illustrates using
console input with basic error handling that would be suitable
for rank beginners?
Some context and more detail:
The question comes up from time to time as to whether it makes sense
to use C++ to teach programming to new programmers. (I think the
answer is yes, but this particular issue is not really my question.)
If you want to do so, you need to teach them basic programming
concepts, good programming habits, and good C++ programming
habits before you teach them all, or most, a lot, or even some
of the intricacies of C++.
Imagine that you are teaching a beginning programming class that
uses C++, and that the students have written their "hello world"
program, have a text editor or ide, and have made their peace with
whatever compile / build process they are using. Now you want them
to start writing simple programs that perform various simple tasks
on input data, and you want them to be able to read in data from the
console (cin), and write the results to the console (cout). (For the
sake of argument, let's stick to using cin rather than argc / argv
for input.)
But you want your students to be able to input their data in some
reasonably convenient way, and not have their program exit (or crash)
every time they mis-type some input data on the console (and you also
want your students to start facing the reality that the outside world
is not so accommodating as to make programming trivial, and you need to
have some kind of validation and error checking for input data, and,
ideally, need to give your users some support with thier data input).
So "cin >> var1 >> var2 >> var3; do_computation (var1, var2, var2);"
is out.
One approach might be to write a message to cout such as "enter var1,
var2, and var3", enter a loop in which you read in the data, verify,
and either print out a "try-again" message or accept it. You might do
this a couple of times if there were a couple of different chunks of
data you needed to read in. (But maybe there are other good approaches.)
What sample program would you suggest to give your students a basic
framework for this kind of simple data input? They wouldn't necessarily
be able to use the code unchanged from program to program, but it should
be suitable for use in similar, but distinct applications, if only through
copy-paste-modify.
Ideally, you should be able to walk the students through the sample in
one class period, and they should be able to understand the code and the
basic logic, even if they don't understand all of the nuances.
(For example, you would probably use std:string. If so, the students
presumably won't know (yet) that std::string is a specialized template,
or even that std::string is a library class, rather than a primitive type.
But they should know -- after you tell them -- that std::string is a type
that holds the value of a string, and is a good first choice for working
with strings.)
The idea is to give the students something that -- within the context of
being one of the earliest programs they encounter, and subsequently write
on their own -- starts them on the path of using modern C++ idioms (specifically
C++11), and good C++ habits.
There is clearly a trade-off between truly good code and code simple enough
for rank beginners. But within the constraint of the necessary simplicity
we want to start illustrating good programming habits.
Any comments and suggestions would be very welcome.
K. Frank
Could people suggest a simple program that illustrates using
console input with basic error handling that would be suitable
for rank beginners?
Some context and more detail:
The question comes up from time to time as to whether it makes sense
to use C++ to teach programming to new programmers. (I think the
answer is yes, but this particular issue is not really my question.)
If you want to do so, you need to teach them basic programming
concepts, good programming habits, and good C++ programming
habits before you teach them all, or most, a lot, or even some
of the intricacies of C++.
Imagine that you are teaching a beginning programming class that
uses C++, and that the students have written their "hello world"
program, have a text editor or ide, and have made their peace with
whatever compile / build process they are using. Now you want them
to start writing simple programs that perform various simple tasks
on input data, and you want them to be able to read in data from the
console (cin), and write the results to the console (cout). (For the
sake of argument, let's stick to using cin rather than argc / argv
for input.)
But you want your students to be able to input their data in some
reasonably convenient way, and not have their program exit (or crash)
every time they mis-type some input data on the console (and you also
want your students to start facing the reality that the outside world
is not so accommodating as to make programming trivial, and you need to
have some kind of validation and error checking for input data, and,
ideally, need to give your users some support with thier data input).
So "cin >> var1 >> var2 >> var3; do_computation (var1, var2, var2);"
is out.
One approach might be to write a message to cout such as "enter var1,
var2, and var3", enter a loop in which you read in the data, verify,
and either print out a "try-again" message or accept it. You might do
this a couple of times if there were a couple of different chunks of
data you needed to read in. (But maybe there are other good approaches.)
What sample program would you suggest to give your students a basic
framework for this kind of simple data input? They wouldn't necessarily
be able to use the code unchanged from program to program, but it should
be suitable for use in similar, but distinct applications, if only through
copy-paste-modify.
Ideally, you should be able to walk the students through the sample in
one class period, and they should be able to understand the code and the
basic logic, even if they don't understand all of the nuances.
(For example, you would probably use std:string. If so, the students
presumably won't know (yet) that std::string is a specialized template,
or even that std::string is a library class, rather than a primitive type.
But they should know -- after you tell them -- that std::string is a type
that holds the value of a string, and is a good first choice for working
with strings.)
The idea is to give the students something that -- within the context of
being one of the earliest programs they encounter, and subsequently write
on their own -- starts them on the path of using modern C++ idioms (specifically
C++11), and good C++ habits.
There is clearly a trade-off between truly good code and code simple enough
for rank beginners. But within the constraint of the necessary simplicity
we want to start illustrating good programming habits.
Any comments and suggestions would be very welcome.
K. Frank