Search

Computer Programming Made Easy: Part 3 of 3

Well done folks! We have finally arrived. This the third part of the three-part computer programming post. We will learn how to write a program that will accept data by user input through the keyboard and also print out a result to the computer monitor after performing a simple task of checking if the number is positive of negative.

For simplicity, I have chosen the object-oriented, higher level programming language C++ to program the computer to perform a simple computation for us.

We will write a C++ program that will prompt the user to type in an integer and the program will check whether the entered integer is positive or negative and display an appropriate output.

////////////////////////
#include <iostream>
using namespace std;

int main() {
/*variable declarations*/
int number;
int threshold = 0; // we assume 0 is positive

/*user prompted for input, then input received from keyboard*/
cout<< "Please enter a negative or positive integer: ";
cin>> number;

/*behavior processing, then output is displayed to terminal*/
if ( number >= threshold) {
cout << "You entered a positive number: "<<number<<endl;
}
else {
cout<<"You entered a negative number: "<<number<<endl;
}

/* This message is always printed */
cout<<"Thank you for working on your first computer program.";
return 0;
}
//////////////////

In order to use the program above, you need to use a text editor such as vi or emacs and type in the lines of code displayed above into your ascii/text file and save it with a name such as "program1.cpp".

Next you need to open up a shell in linux or unix and type "man g++" for the full manual if g++, if you want to know how to use g++. To compile the program, you need to type the following statement in the shell.
% g++ program1.c -o program1

This statement tells g++ to compile "program1.c" and if there are no errors, then it will create an output file named "program1." If there are errors in your program, then the compiler will show them on the screen and then return you to the prompt. You should then correct these errors by opening the program in your text editor.

After you have corrected your errors, you can use the same g++ command to compile it again. If there are no errors in your code, then the compiler will return you to the prompt without any error messages. This means that your code has been compiled into a separate executable file which is named "program1". You may run the code by typing in "program1" at the prompt as follows:
% program1

The program will prompt you to enter a negative or positive integer. If your enter a negative number e.g. -13, the program will display this message:
"You entered a negative number: -13".
 
If your enter a positive number e.g. 92, the program will display this message:
"You entered a positive number: 92".

So essentially, the computer will read an integer typed in by the user and perform a simple comparison and display a message on the terminal whether the integer you entered was a positive or negative integer.

I have added comments to document the sections of the code. This is recommended practice to help people understand your computer program. Give yourself a pat in the back because you have just written, compiled and executed your first C++ computer program.

As an extension, we can change our program to accept any real number by changing the declaration to either "float number;" or "double number;" depending on how big we want our number to be. We can also easily change this program to one that tells us if a student had a grade of A or not in their exam assuming that they get a grade of A if they score a 90 or above. I would ask that you create this modified program as an exercise.
if (isMyPost) { }