IA_Supplemental_Instruction_Week_2

IA_Supplemental_Instruction_Week_2 - E NGR 101 IA...

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

View Full Document Right Arrow Icon
ENGR 101 IA Supplemental Instruction Justine Kunz
Image of page 1

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

View Full Document Right Arrow Icon
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!
Image of page 2
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;
Image of page 3

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

View Full Document Right Arrow Icon
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?
Image of page 4
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
Image of page 5

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

View Full Document Right Arrow Icon
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 }
Image of page 6
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
Image of page 7

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

View Full Document Right Arrow Icon
While Statement Example
Image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern