Labsheet_3

635 views

Published on

Published in: Technology, Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
635
On SlideShare
0
From Embeds
0
Number of Embeds
87
Actions
Shares
0
Downloads
17
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Labsheet_3

  1. 1. F2037 PROGRAMMING FUNDAMENTAL WITH C++ LAB 3: INPUT/OUTPUTObjectivesBy the end of this lab, students should be able to :• Describe the structure of input-output statement• Write a program that use the input and output statementsTheory/ Topics• In C++, << is called stream insertion operator.• Output on standard output device (such as monitor) is accomplish via the use of cout and operator >>.• In C++, >> is called stream extraction operator.• Input from the standard input device (such as keyboard) is accomplished by using cin.• To use cin and cout, the program must include header file iostream.• A manipulator is used to format output.• endl is the simplest manipulator which cause insertion point to move to the beginning of next line.• In C++, is called the escape character while n is called newline escape sequence.• Common escape sequence is shown in the following table: For this Use this Example Output "Dont do Dont do that that" "She said" " She said "hi" "hi"" "Backslash: Backslash: " 1
  2. 2. F2037 PROGRAMMING FUNDAMENTAL WITH C++ 1[newline] n "1n2" 2[horizontal tab] t "1t2" 12 • Manipulators allowed us to format the output in a desired way. • To use manipulator we need to include header file iomanip. • The following table is showing manipulators that is commonly used: Syntax Purposesetprecision(n) Set decimal precisionsetfill(c) Setfill charactersetw(n) Set field widthActivity 3AProcedure :Step 1: Type the programs given belowStep 2: Compile and run the program. Write the output.Step 3: Save the program as lab3A.cpp.The following program illustrate input name from user andthe usage of usage of header file string.h#include <iostream>#include <string >using namespace std;void main() { 2
  3. 3. F2037 PROGRAMMING FUNDAMENTAL WITH C++ string name; cout << "Please enter your name : "; cin >> name; cout << "Your name have " << name.size() << " characters.n"; }Activity 3BProcedure :Step 1: Type the programs given belowStep 2: Compile and run the program. Write the output.Step 3: Save the program as Lab3B.cpp.// setfill example#include <iostream>#include <iomanip>using namespace std;int main () { cout << setfill (x) << setw (10); cout << 77 << endl; return 0; }Activity 3CProcedure :Step 1: Type the programs given belowStep 2: Compile and run the program. Write the output.Step 3: Save the program as Lab3C.cpp. 3
  4. 4. F2037 PROGRAMMING FUNDAMENTAL WITH C++#include <iostream>#include <iomanip>using namespace std;int main() { const int max = 12; const int width = 6; for(int row = 1;row <= max;row++) { for(int col = 1;col <= max;col++) { cout << setw(width) << row * col; } cout << endl; } return 0; }Activity 3DProcedure :Step 1: Type the programs given belowStep 2: Compile and run the program. Write the output.Step 3: Save the program as Lab3C.cpp.#include <iostream>#include <iomanip>using namespace std;int main(){ const int max = 12; const int width = 6; for(int row = 1;row <= max;row++) { if(row % 2) { cout << setiosflags(ios::left); } 4
  5. 5. F2037 PROGRAMMING FUNDAMENTAL WITH C++ else { cout <<resetiosflags(ios::left); } for(int col = 1;col <= max;col++) { cout << setw(width) <<row * col; } cout << endl; } return 0;}Activity 3DProcedure :Step 1: Type the programs given belowStep 2: Compile and run the program. Write the output.Step 3: Save the program as Lab3D..cpp.#include <iostream>#include <iomanip>using namespace std;int main() { double x = 800000.0/81.0; cout << setprecision(2) << x; return 0; }Exercise 3.1 5
  6. 6. F2037 PROGRAMMING FUNDAMENTAL WITH C++ Display the following output using function provided in headerfile, iomanip. ********** ******** ***** *** *Exercise 3.2Write a program the will receive an input in $US anddisplay the corresponding value in RM. (RM3.52 = $US1).Exercise 3.3Write a program that will receive 2 inputs (a and b) andcalculate the value of c by using Pythagoras Theorem.(Hint: Use mathematical library functions) Check: c If a = 4, b = 3 b hence c = 5 a 6

×