Programming C++  «Prev  Next»
Lesson 3 C++ Input
ObjectiveConvert a simple input/output program in C to C++.

Convert C program to C++ program

Take a look at simple input in C++.
We will write a program that prompts the user for a value in miles. The program then converts this value to kilometers and prints it out.

Variables and declarations


Constants
Our conversion program uses variables capable of storing integer values and real values, as well as const variables. Notice that in C++, all variables must be declared before they can be used, but unlike in C, they need not be at the head of a block.

Program dissection

Move your mouse cursor over the lines of code surrounded by red rectangles to receive tooltip information with respect to the code.

C++ Input
  1. The keyword "const" replaces some uses of the "define" preprocessor command to create named literals. Using this type modifier informs the compiler that the initialized value of m_to_k cannot be changed. Thus, it makes m_to_k a symbolic constant.
  2. The new keyword "inline" specifies that a function is to be compiled as inline code, if possible. This avoids function call overhead and is better practice than C's use of define macros. As a rule, inline should be done sparingly and only on short functions. We'll discuss inlining in a later module.
  3. The input stream variable "cin" is the normal standard input. The input operator >>, called the "get from" or "extraction" operator, assigns values from the input stream to a variable.
  4. The value placed in the standard input stream is passed to the convert() function, returned as the number of kilometers, and then printed to the standard output stream.

Input Class

Programming languages

Programming languages are independent of specific computer architecture, but they are human creations and follow certain conventions. To ease the translation process, those conventions are much stricter than they are for human languages. When you talk to another person, and you scramble or omit a word or two, your conversation partner will usually still understand what you have to say. Compilers are less forgiving.
For example, if you omit the quotation mark close to the end of the instruction,
if (int_rate > 100) message_box("Interest rate error);

the C++ compiler will get quite confused and complain that it cannot translate an instruction containing this error which is a good thing. If the compiler were to try to guess what you did wrong and try to fix it, it might not guess your intentions correctly.
In that case, the resulting program would do the wrong thing with side effects.
When a compiler reads programming instructions in a programming language, it will translate them into machine code only if the input follows the language conventions exactly. Just as there are many human languages, there are many programming languages. Consider the instruction
if (int_rate > 100) cout << "Interest rate error";

This is how you must format the instruction in C++. But in Visual Basic (a popular programming language for business applications) the same instruction would be written as
if int_rate > 100 
then System.Console.Write("Interest rate error") 
end if
Compilers are language-specific and the C++ compiler will translate only C++ code, whereas a Visual Basic compiler will reject anything but legal Visual Basic code. For example, if a C++ compiler reads the instruction
if int_rate > 100 then ..., 

it will complain, because the condition of the if statement is not surrounded by parentheses ( ), and the compiler does not expect the word then. The choice of the layout for a language construct such as the if statement is somewhat arbitrary. The designers of different languages make different tradeoffs among readability, easy translation, and consistency with other constructs.

Using IO Stream Exercise

Click the Exercise link below to convert a simple input/output program in C to C++.
Using IO Stream - Exercise