Download presentation
Presentation is loading. Please wait.
1
1 Lab Session-VIII CSIT-121 Spring 2002 w Formatted Output w Call by Reference w Lab Exercises w File Handling w Lab Exercises
2
2 Formatted Output w Include to format output w You can control w width cout<<setw(12) for next value only w precision cout<<setprecision(4) w showpoint cout<<showpoint w fixed/scientific cout<<fixed w So that your floating point numbers are displayed as desired.
3
3 Call By Reference w Call by reference means passing the original variable to the function for changing w For example compute_sum_and_mean(int k, int j, int l, int& sum, float& mean) w It has three INPUT arguments and two REFERENCE arguments that are used to output the sum and mean
4
4 Lab Exercise 8-1 (Shown) w Using formatted output, try printing out the constants as below, with 3 digits to the right of decimal point and total width of 9 w const float VAL1=10.39567 w const float VAL2=13.378564 w const float VAL3=35.76835464 w Remember to close all workspaces and start fresh for each exercise
5
5 Lab Exercise 8-2 w For reference arguments exercise, develop a program that calls a function to order two integer values in the increasing numerical order. w Function name order_values(int& num1, int&num2) w when passed values (x,y), it orders the same as (y,x) if it finds y to be less than x. w The function is void as it returns the values through reference arguments
6
6 File Handling w Files are handled easily by including fstream and opening files w If the input file is not there, it will be created by the myfile.open() call w The standard error checking function calls are now given. Please include the same in your exercises
7
7 File Handling Sample Program w #include w using namespace std; w int main() w {
8
8 File Handling Sample Program w ifstream my_infile; w ofstream my_outfile; w string filename; w cout<<"Enter a file name to open: "; w cin>>filename; w my_infile.open (filename.c_str()); w
9
9 File Handling Sample Program w if (!my_infile) w { cerr << "*** ERROR: Cannot open " << filename << " for input." << endl; w return EXIT_FAILURE; w } w else cerr<<"success"<<endl; w return 0; w }
10
10 File Handling for Output Files w my_outfile.open (“out.dat”); w if (!my_outfile) w { w cerr << "ERROR: Cannot open " << “out.dat” w << " for output." << endl; w return EXIT_FAILURE; w }
11
11 Lab Exercise 8-3 w For a simple program, let us get each character from a file and display it in uppercase on the screen. w For this purpose, we need to develop a small program w A code segment is here to give you sufficient start-up. Include cctype file
12
12 Lab Exercise 8-3 w char next; w my_infile.get(next); w while (!my_infile.eof()) w { w cout.put(char(toupper(next))); w my_infile.get(next) w }
13
13 Lab Exercise 8.4 Demo Required w Develop a program that reads a text file and counts all occurrences of letter ‘E’ in it. You get bonus points if you can count all vowels with switch-case statement
14
14 Practice Exercises for File Handling w Write a program that reads two text files one after another and produces an output file that combines the content of both w Q10 page195 (Old book page 205) w Q8 page 197 (Old book page 207)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.