IA_Supplemental_Instruction_Week_2

IA_Supplemental_Instruction_Week_2 - ENGR 101 IA...

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

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

Unformatted text preview: ENGR 101 IA Supplemental Instruction Justine Kunz C++ Streams We've talked a lot about the iostream library: cin : standard input stream cout : buffered output stream cerr : output error stream (NOT buffered) There are also other types of streams: Namely, fstream : for files! The fstream Library If we want to do any sort of file I/O, we put the following at the top of our code: #include <fstream> ifstream => input file stream ofstream => output file stream Use ifstream to get data from a file ifstream input("in.txt"); input >> x >> y; //NOTE: No endl after inputs Use ofstream to get write data to a file ofstream output("out.txt"); output << x << y << endl; fstream Example #include <iostream> #include <fstream> using namespace std; int main() { ifstream input("nums.txt"); // opens nums.txt for reading ofstream output("convNums.txt"); //opens convNums.txt to write double numA, numB; input >> numA >> numB; output << numB << "\t" << numA << endl; // what is "\t"? input.close(); output.close(); // on same line to save space return 0; } Contents of nums.txt : 781.9 90.796 What does convNums.txt look like? fstream Example #include <iostream> #include <fstream> using namespace std; int main() { ifstream input("nums.txt"); // opens nums.txt for reading ofstream output("convNums.txt"); //opens convNums.txt to write double numA, numB; input >> numA >> numB; output << numB << "\t" << numA << endl; // what is "\t"? input.close(); output.close(); // on same line to save space return 0; } Contents of nums.txt : 781.9 90.796 What does convNums.txt look like? 90.796 789.1 Iteration Iteration allows the computer program to employ repetition to achieve a goal Iteration often allows us to devise more general solutions to problems Iteration can be accomplished with a while statement in the form while(condition) { loop_body } While Statement while(condition) loop_body The condition must evaluate to True or False. The loop body is an expression or compound statement in {braces} If the condition is true, the loop body executes, then the condition is checked again The loop body is repeatedly executed as long as the condition holds While Statement Example When does the following while loop stop executing? To put this executing?...
View Full Document

Page1 / 37

IA_Supplemental_Instruction_Week_2 - ENGR 101 IA...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online