A Slightly Friendlier I/O Toolkit
There are lots of ways to get input and output in any given program. One way other than using standard C language I/O operations is to use a set of tools developed for this reference. These tools were developed for learning the material in this reference and you will not find them anywhere else. Using these tools at this point in your learning is not critical but it does offer both ability to make nice-looking displays and it gives you plenty of opportunity to learn more about using methods.
Consider one of the previous examples, which was the code required to prompt and input data. The process looked like the following.
printf( "Enter your birth year: " );
scanf( "%d", &intInput );
If you use the formatted command line I/O tool set called Console_IO_Utility, the following would replace the printf prompt and scanf input operations.
birthYear = promptForInteger( "Enter your birth year: " );
You will need to compile and link the Console_IO_Utility files into your program but that is not difficult, and will be explained when the time comes for you to use it.
The process of prompting and inputting are packaged into a function, which makes your code more readable, and simpler. There is a promptFor function for all data types you will commonly use (and about which you will learn later in this reference).
In addition, there are output functions that allow you to output data. For example, a simple output would look like the following.
printInteger( age );
This function simply outputs an integer. However, the function also has a big brother that accepts three parameters to output an integer justified in a specified block of characters. Consider the following code:
// variables defined for parameters
blockSize = 10;
age = 19;
// function calls implemented
printCharacter( PIPE );
printIntegerJustified( age, blockSize, "LEFT" );
printChar( PIPE );
The results of this function call show age placed in a left justified form within a block of characters. This example includes printed pipes (i.e., '|') so that you can see what the results would look like. Here is the output for the above code.
|19 |
A pipe is printed, then the age is printed on the left side of the block (of ten characters), then spaces are made so that the whole block is 10 characters wide (i.e., the block size, which is between the pipes), and finally, another pipe is printed so you can see the block.
Another function is printDouble which prints double or floating point values (i.e., numbers with a decimal point). The simple version looks like the following:
// variables defined for parameters
precision = 2;
payRate = 17.44731;
// function call
printDouble( payRate, precision );
This will simply output a double value with the number of digits to the right of the radix or decimal point specified. See the results below:
17.45
The printDouble function can also be set to an outputted quantity in a formatted output. The following shows an example of the power of this tool.
// variables defined for parameters
precision = 2;
blockSize = 10;
payRate = 3.4567;
// function calls
printChar( PIPE );
printDoubleJustified( payRate, precision, blockSize, "LEFT" );
printChar( PIPE );
Both these functions are a little different from printInt. As above, since it is a double output, it also includes a parameter for the number of digits following the decimal point, which is somewhat incorrectly, but commonly, called precision. This example includes printed pipes (i.e., '|') so that you can see what happens. Here is the output for the above code.
|3.46 |
A pipe is printed, then the number with two digits (rounded) after the decimal is printed on the left side of the block, then spaces are made so that the whole block is 10 characters wide (i.e., the block size), and finally, another pipe is printed so you can see the block.
Here is another example with right justification.
// variables defined for parameters
precision = 2;
blockSize = 10;
payRate = 3.4567;
// function calls
printChar( PIPE );
printDoubleJustified( payRate, precision, blockSize, "RIGHT" );
printChar( PIPE );
This time, the data is printed at the right side of the 10-character block. Again, the pipes are printed to show you the effect.
| 3.46|
There are several possibilities for inputting and outputting using these tools. To learn more, just look at the code in the Console_IO_Utility.c, the Console_IO_Utility.h files, and the StandardConstants.h file needed for this utility. These will be needed when console I/O operations are conducted. There are several commented specifications that can help you choose and use the appropriate functions to make nice-looking displays.
Watch this series of videos: Console I/O Step One, Console I/O Step Two, Console I/O Step Three, Console I/O Step Four to see what you can do with this tool. The series implements a complete input and formatted output program using the Four Step Programming Process that will significantly exercise your linear progression and function learning. In addition, you are introduced to variables and global constants. Make sure you work through the code yourself and try some different actions. And the more times you work through the video and the code, the stronger your foundation will be.