201Spring08-L18 - CMPSC 201C Spring 2008 Lecture 18 Nesting...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CMPSC 201C Spring 2008 Lecture 18 February 25, 2008 Nesting Loops Loops can be "nested" inside another loop for (int j = 1; j < 6; j++) // outer loop { cout<< " j = "<< j <<" "; for (int k = 1; k<= 8; k++) // inner { cout<<k<<" "; } cout<<endl; } The inner loop is executed completely for each iteration of the outer loop. What would be output from this loop? More on Nested Loops Loop types do not have to be the same type. do { statement(s) while (cond) { statement(s) // body of loop } statement(s) } while (condition); More Examples of Nested Loops Common Problems with Loops Missing Braces Incorrect initialization of accumulators Incorrect update of loop-control variable Using assignment operator, =, rather than the relational operator, == Questions ???? Using Files for Input/Output Many times you may want to input data from a file rather than interactive by hand. Additionally, you may want to send the output to a file rather than just appearing on the screen. For file input and output, 3 abstract data types may be used to objects: ifstream for file input ofstream for file output fstream for either (do not use as a data type for this course!) To use file input or output you must include the fstream library file # include<fstream> // note different from datatype Then you must declare an object that will refer to the file. datatype object name; ifstream infile; // infile is just an identifier ofstream outfile; // outfile is just an identifier File Input and Output Can declare multiple objects: ifstream infile, datain, filein; ofstream outfile, dataout, fileout; File Input and Output The file must be opened before you can use it. infile.open("filename"); outfile.open("filename"); - may specify a directory path by placing it in quotes as well infile.open("a:\\somename.ext") Good programming practice closes the file once you are done using it. infile.close( ); outfile.close ( ) ; Filenames The filename may be hard coded by putting the actual name in quotes. infile.open("filename"); // in same directory infile.open("cse103/myfile.txt"); /* other directory */ The filename may also be stored in string (char array) infile.open(stringname); /* stringname is character array that contains the file name information */ Using Files Once an input object has been declared and the file has been opened, data can be accessed from it in a similar manner as cin. infile>>alpha; Similarly, once an output file has been declared an a file has been opened, information can be written to the file just like using cout. outfile<<beta; All the formatting for cout and cin will work with your file objects as well. Skipping Data in Files There may be times when you are reading from a file that you want to skip over or ignore data that is in the file. There is a function associated with file object that will ignore this data. The general format is: fileobject.ignore(n, `character'); indata.ignore(80, `\n'); Detecting File Errors Often when reading data from a file, you may want the loop to end if an error from input occurred or the end of file was reached. The fail function of your input object will return a true value if file did not open correctly or incorrect data was present, otherwise it will be false The eof (end of file) function will return a true if the end of file was detected, otherwise it will return a false. fail function Suppose we have declared and opened an object called infile. We could ensure that the file opened correctly before trying to process the data. if (infile.fail) { cout<<"Error opening file"<<endl; } else { statements to process data } eof function The eof function is often used to terminate a loop that is reading from a file. infile >> alpha; while (!infile.eof( )) //note the ! { //processing statements infile >> alpha; } See also Figure 4.5 on page 122 Questions ...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online