The Basics of Programming

For a program to be really useful, it has to be able to take input from a user and output the results. Some input can be from a keyboard and output sent to the screen. In simple C programs, you would use the shell or command prompt to take input and the output. These are known as the standard input and outputs.

A) Input

When in putting data, you need to scan the keyboard for characters and store them in a buffer or a variable. In C the command is scanf(format, variable) and for C++ it is just variable << cin . The scanf command will take two parameters, the first is the format of the input,which will be a format string to tell it whether it is a number (%d), a character( %c) or a string (%s). Other types include inputs for octal, hex, floating point and unsigned decimal values. If using numbers then the variable should be pre-fixed with the ampersand (&) character to tell it that 'it is the address of' the variable.

Before you can use any input/output functions, you must include the standard I/O header file (.h) which tells the compiler about the new functions. You specify the following line at the top of your program. The less than and more than signs tell the compiler to include in the default Include folder for your C compiler.

#include <stdio.h> /* Include standard Input/Output header */

For C++ programs, you need the iostream header:

#include <iostream> /* Include Input/output stream header for cin and cout (the .h externsion is left off) */

Example input code:

char buffer[50]; /* Create a 50 character buffer*/
scanf("%s", buffer); /* Read a string upto 50 characters long */
int number;
scanf("%d", &number); /* Read an integer from input and store it at the address of number */

Warning. If scanning numbers from input then you have to careful what you type in, typing the wrong characters can crash the program. So, ideally use strings for input and then convert the string to a number.
You should always validate your input, and rejest any invalid inputs so that your program does not process something unexpected. Test it will different values to ensure the program works as expected.

If using C++ then you can use the simpler cin command and direct it into a variable using the '>>' characters.

 cin >> buffer ;


For AmigaOS 4, you should use the std namespace for C++ I/O functionality.

using namespace std;

std::cin >> buffer;

If using a graphical front end, then the input will be via Text Boxes, Buttons and Dialog boxes where you can make enter text, numbers and make selections, or answer questions by the program. On Amiga C, this is provided via the Amiga OS Workbench using libraries such as Intuition, Gadtools and MUI.
Modern computers also have a mouse so your program can make use of the mouse's position (its X, Y) position and buttons (Left or Right) to perform input.

b) Output

When outputting data or information, you would normally use the shell or command prompt window to display it. In C, the command to output is printf("string" [, variable-list]), where the string can be anothing you want whether its a prompt, some information, a description or it can contain placeholders for your variable output using the format as scanf: a number (%d), a character( %c) or a string (%s). Other types include inputs for octal, hex, floating point and unsigned decimal values. When you want to move to a new line then you would add the '\n' (new line) string.

e.g.

printf("Please enter your name:\n"); /* Display the text 'Please enter your name:' followed by new line */
printf("Result is %d\n", result); /* Display the text 'Result is ' and follow it with a decimal number from the variable called result and a new line */
printf("Your name is %s and your age is %d.\n", name, age); /* Display the text 'Your name is ' and the contents of variable name followed by more text ' and your age is ', and then the contents of decimal variable age */

In C++, then you can use the simpler cout command using format cout << expression, use the '<<' between variables or constants, and then use endl to indicate the end of line.

e.g

cout << sum; 
cout << a << b << c << endl ;
cout << "My name is " << aName << endl; 

For AmigaOS 4, you should use the std namespace for C++ I/O functionality.

using namespace std;

std::cout << sum;

If using a graphical front end, then the output will be via Labels or Text Boxes, where you can display text or numbers. On Amiga C, this is provided via the Amiga OS Workbench using libraries such as Intuition, Gadtools and MUI.
Also, instead of just text or numbers or list of numbers you can also output results on forms, and use charts and graphs using the drawing tools provided by AmigaOS using the graphics library.

c) Writing and reading from files

You can also read and write to files from with in C/C++ using functions such as fopen, fclose, fputc, fgetc, fprintf, fscanf etc from the standard I/O library.

To open a file, use the fopen function, you specify the name of the file and the mode 'r' = read, 'w' = write, and it will return a filehandler pointer. The filehandler is

#include <stdio.h>

FILE *fh;

fh = fopen("myfile", "r");

To Read a character, use fgetc using the file handler and return a single character value.

c = fgetc(fh);

To read a string, you can use fscanf function. You need the file handler, the input format (%s = string) and string buffer. Note, that you should always input into a string value including numbers are they are stored as strings in files. You can convert strings to numbers using functions such as atoi() (ascii to integer), or atol() (ascii to long), atof() (ascii to float) etc.

fscanf(fh, "%s", strinput);

To check that you have reached the end of the file, use the feof(fh) function, it returns a non-zero value if end of file is reached.
To write a character to a file use the fputc function using a character and the file handler.

fputc(ch, fh);

To write a full string, use the fprintf function. You need the file handler, the output expression with any variable position format (%s = string, %d = integer) and anyvalues to output.

fprintf(fh, "Text to print: %s\n", strvar);

Next Page