Sunday 5 June 2016

C++ : I/O and Implementation of I/O

Accuracy in the processing of the problem lies in whether the results are in accordance to requirement or not (valid or invalid). To get the expected results, it should be considered the input, process, and output.

The performed Operation on the input is to read data or value that will be processed. The value of a variable can be early defined in the program or entered by the user from the keyboard by using the existed functions of the library in C++ programming language.

Unlike the operation on the input, the output operation is done to transmit or display data or value to the output device (output device), such as a printer or screen (monitor). Examples of ouput operation is displaying the sentence to the screen, that usually is done to show commands to enter an input into the program, or display the value of a variable to the monitor by using the existed libraries of C++ programming language, also usually done to show the results of a calculation or the result of a solution . In C++ iostream library, standard input and output operations for programming are supported by 2 data streams: cin for input and cout for output.

Standard output (cout), the use of cout stream is coupled with overloaded operator << (A pair of the "less than").

Example :

cout << "Output Sentence"; // print the output sentence on the screen.
cout << 120; // prints number 120 on screen.
cout << x; // print the contents of the variable x on the screen.

Operator << is also Known as the insertion operator, which is used to yield the data that follows it. If a string, then it must be enclosed in double quotes ("), so is unlike with the variable.

Example :
cout << "Hello"; // prints the output phrase Hello on the screen.
cout << Hello; // prints the content of Hello variable on the screen.

Operator insertion (<<)may be used more than 1 time in the same sentence, for example: cout << "Hello, " << "Good " << "Morning"; ”The above example will display Hello, I am a C + + sentence on the monitor screen. Benefit from the repetition use of insertion operator (<<) is to display a combination of one or more variables and constants,

Example 1 :
cout << "Hello, My Age" << Age << "Years and I class of" << class of;


For instance, age variable is filled with the value 18, and the class of variable is charged with 2016. Then the result of output is:

Hello, umurku 18 tahun dan aku angkatan 2012

Example 2 :
cout << "First Setence.\n "; cout << "Second Sentence.\nThird Sentence."; 

Then the resulting output is:
First Sentence.
Second Sentence.

Third Sentence.

Beside the newline character, can also use the endl manipulator, for example:
cout << "First Sentence. " << endl;
cout << "Second Sentence." << endl;

Output :
First Sentence.

Second Sentence.

Standard input (cin), its use by adding overloaded extraction operator(>>) on the cin stream that followed by variable that will store the data.

Example:

int Age;
cin >> Age;

The above example declares 'Age' variable in type of int and wait for input from cin (keyborad) to be stored in the Age variable . Cin command will process the input from the keyboard once and the ENTER key must be pressed.The cin can also be used for more than one input:

cin >> a >> b;

Equivalent to :

cin >> a;
cin >> b;

In this case the data in the input should be 2, one for variables a and the other for b which are written separated by: space, tabular or newline.

Usually cout (standard of stream output) is intended to the monitor and cin (the standard of stream intput) is intended for the keyboard. By using these two streams, then we can interact with the user by displaying messages on the monitor and receive input from the keyboard.

Implementation of I/O

Example for implementations of I/O (Input Output), which is a program used to receive input from the students namely nim, name, age, address, and NEM values and displays each of student data themselves to the screen.

1. Task 1 : Create a student data program.

  • Step 1 : Declare the variables used and their data types.
    Explanation: The variables that are used to display process of the students data themselves namely the variable nim, name, and address that each of them are variable of char data type, these difference only in size. Variable age of integer type, and nem variable of float type because it consists of a value that requires number precision after the decimal point.
  • Step 2 : Initialization.
    Explanation: Because the program receives direct input from the user, then it may not be given early initialization.Giving early initialization is adapted to the needs of the processed case.
  • 3) Step 3 : The process of solving the problem.
    Explanation: Reading input from the user by using cin function, and displays the results to the screen using cout function.
  • 4) Step 4 : Finalization.
    Explanation: Stopping the main() function is by using the return 0 command.

#include<iostream.h>#include<conio.h>using namespace std;int main(){   char nim[20]; char name [20]; char address; int age; float NEM; //variable declaration   cout << "input your nim : "; //show to the screen   //cin.getline(nim,20);   gets (nim); //accept input is nim variable   cout << "input your name : ";   gets (name);   cout << "input your address : ";   gets (address);   cout << "input your age : ";   gets (age); //accept input is age variable   cout << "input your NEM : ";   gets (NEM);
   //show results to the screen
   cout << "Hello" << nama << ", Welcome . ." << endl;
   cout << "Your data is : " << endl;
   cout << "nim : " << nim << endl;
   cout << "name : " << name << endl;
   cout << "address : " << address << endl;
   cout << "age : " << age << " years" << endl;
   cout << "NEM : " << NEM << endl;
   getch();   return 0;}
finalization Sourcecode

2. Task 2: Compile and Execution

running programs display






Exercise

  1. Make summation program for two value by using input numbers from the user when program run.
  2. Create a program to calculate the time taken based on the given speed and distance by using the input by the user.
  3. Create a program to consider a number is odd or even.
  4. Create a program to determine a number is prime or not.
  5. Create a program to calculate the area of a circle.


EmoticonEmoticon