Another Simple Program: Adding Two Intergers

Posted by Didi Setyapramana On 9:56 PM 0 komentar

Our next Program Uses the input object std::cin and the stream extraction operator ,>>,to obtained two interger typed by user at the keyboard,computer the sum of these value and output result using std::cout

.Figure 1.6 show program and sample output.
The comment in lines 1 and 2

//fig.1.6 fig01 06.cpp
//addition program
State the name of the file and porpose of the program.The C++ preposeor directive

#include
In line 3 includes the contents of the iostream header file in program.
As stated earlier,every program begin execution with function main’s body and the corresponding right brace marks the of main .



  1. //Fig.1.6: fig 01 06.cpp
  2. //adtion program
  3. #include<iostream>
  4. //function main begin program execution
  5. Int main()
  6. {
  7. Int interger1;
  8. Int interger2;
  9. Int sum;
  10. Std::cout<<”Enter first interger\n”;
  11. Std::cin>>interger1;
  12. Std::cout<<”Enter second interger\n;
  13. Std::cin>>interger2;
  14. Sum= interger1+interger2;//assign result to sum
  15. Std::cout<<”sum is”<<sum<<std::endl;
  16. Return 0;
  17. }
Enter first interger
45
Enter second interger
72
Sum is 177

Line 8-10

Int interger1//first number to the input by user
Int interger2//second number to input by user

Int sum;//variable in which sum will be stored
Are declaration .The word interger.The word interger1,interger2 and sum are the names of variables .
 A variable is location in the computer’s memory where a value can be stored for use by a program.
This declaration specifies that the variables interger1,interger2 and sum are data of type int.
which means that these variables will hold interger value,I .e.whole number such as 7,-11,0,3,31914.
All variables must be decalated whith a aname and data type before they can be used in a program.
Several variable of the same type may be decalated in one decalaration or multiple decalarions.we could have decalared all three variables in one decalaration as follows:
Int interger1,interger2,sum;
However ,this makes the program les readable and pevent us from providing comments that describe each variable’pupose in the program.if more than one name is declared in a declaration(as shown here),the name are separated by commas(,)this refered to as acomma-separated list

Good Programing practice 1.8


Some programmers prefer to declare each variable on a separate line.This format for esy insertion of a descripitive comment next to each declaration.


We will soon the data type double (for sepecifiying real number,i,e., mumber with decimal point like 3.4,00-11,19)and char ( for specifying character data; a char variable may hold only single lowercase letter,a single uppercase letter,a single digit or single special character like a x,$,7,*,etc).

Good Programming practice


Place a sapace after each comma(,) to make programs more readable


    A variable name is any valid identifier.And identifier is a series of character consisting of letter,digits and underscores(_) that does not begin whit a digit,C++ is care sensitive –upercase letter different,so a1 and A1 are different identifiers.
C++ allows identifier of any length but your system and/or C++ implementation may impose some restriction of length of identifiers,use identifiers of 31 character of fewer to ensure portability.


Good programming practice


Choosing meaningful variable names helps a program to be”self-documentting,”I,e.,it becomes easier  to understand the program simply by reading it rather than having to read manuals or  use excessive comment.
Good programming Practice


Avoid identifiers that begin with underscores and double underscore because C++ compiler may use name like that for their own purpose  internally,This will prevent names you chose  frome being confused with name the compilers chose.

Declaration of variables can be placed almost anywhere in function ,but they must appear before their corresponding variables are used in the program.for example,in the program of fig 1.6 the declaration.
Int interger1;

Could have been placed immediately before the line
Std::cin>>interger1;
The declaration
Std::cin>>integer2;
And the declation
Int sum;
Could have been placed immediately before the line
Sum= interger1 + interger2;

Good Programming Practice


Always place a blank line between a declation and adjacent executable ststments,this makes the decalarations stand out the program anf the contributes to program clarity

Good Programming Practice


If you prefer to place declaration s at the beginning of a function ,separate those declarations from the executable ststments in the that function with one blank line highlight where the declarations end and the executable statement begin

Line 12
Std::cout<<”enter first interger\n”;//promt Prints the string Enter first interger (also know as string literal or a literal) on the screen and position the cursor to the beginning of the next line.
This message is called a promt because it tell the user to take specific action.
We like to pronounce the predicting statement as “cout”gets the character string”enter First interger\n”

Line 13 Std::cin>>interger1;
Use input stream object cin (of name space std)and the stream exraction operator,>>,to obtain a value from the keyboard .
Using the stream extraction operator with >>,to obtain std::cin takes character input from the standard input stream which is usually the keyboard.
We like to pronounce the preceding statement as, “std::cin gives a value to interger1 “or simply”std::cin gives interger1.
When the computer executes the preceding ststment,it waits for the user to enter a value for variable interger1 .
The user responds by typing an interger(as character)then pressing the Enter key (sometimes called the return key) to send character to computer.
The computer then converts the character reperentasion of the number to an integer assigns this number(or value) to the variable interger1 in the this program will use same value.
The std::cout<<”enter second ineterger\n”;//promt Print the word Enter second interger on the screen,then postions the cursor to the beginning of the next line.this statement prompts the user make to take action

Line 16. Std::cin>>interger2;

Obtains a value for variable interger2 from user.
The assignment statement in the line 18
Sum=integer1+integer2;
Calculating the sum of variable intereger 1 dan interger2 and assing the result to variable sum using the assign operator=the statement is read as,”sum gets gets value of interger1+integer2.”
Most calculating are performaed in assignment statements”The =operator and the + operator called Binary operators because they a=each have two operands.in the caseof the+operator,the two operands are interger1 and interger2.
In the case of preceding =operator ,two operand are sum and the value of expression interger1+interger2.

Good Programiing practice


Place space on either side a binary operator .tis make the operator stand out and makes the program more readable
Line 20

Line 20
Std::cout<<”sum is “<<sum<<std::endl;//print sum
Display the character string Sum is followed by numerical value variable sum followed by std::endl (endl is abbreviation for “end line”endl also is a name in namespace std)_a so- called stream manipulator.The std::endl manipulator outputs a newline the “flushes the output buffer”.
This simply means that on some system where  output accumulate in the machine until there are enough to make”it worthwhile”to display on the screen,std::endl forces any accumulate outputs to be displayed  at the moment.
            Note that pereceding  statement outputs multiple value of different types.The stream insertion operator”knows:how to outputs each piece of data.
Using multilple streamcasading stream isnsertion operations.
Thus ,its unnescesary to have  multiple output statement to output mu;tiple pieces of data.
Calculation can be also be performed in output statements.
We could have combined the statement at the line 18 and 20 into the statmennt
 Std::cout<<”sum is”<<interger1+interger2<<std:;endl;
Thus eliminating the need for the variable sum.
 The right brace,} inform the computer thet the end of the function main has ben reached A powerful feature of C++ is that user can be create their own data types



0 Response for the "Another Simple Program: Adding Two Intergers"

Post a Comment