Skip to content

Instantly share code, notes, and snippets.

@romanegloo
Last active February 7, 2019 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save romanegloo/e7f3ebd688a76576ebf88d59f4b34225 to your computer and use it in GitHub Desktop.
Save romanegloo/e7f3ebd688a76576ebf88d59f4b34225 to your computer and use it in GitHub Desktop.

Lab 03

Schedule

  • Project 1 is due by Feb 8
  • Lab2 submission is due Tonight

Tips for Lab 03

How to print a float number with a fixed precision

ex) 41.4451 to 41.45 ex) 15.2 to 15.20

  • #include <iomanip> and
  • use std::setprecision(n) and std::fixed

How to get a string line from user input

if (cin.peek() == '\n') cin.ignore();
getline(cin, varname);

do-while loop vs while loop

do {
    statement1;
} while (condition);

while (condition) {
    statement1;
}

Happy Coding~!

lab3 assignment

Lab04

Objectives

  • read input temperatures by day
  • write summarization in an output file

from tempin.txt

01/24/2019 5 22 27 31 26 19
01/25/2019 8 20 25 30 35 40 38 32 29
01/26/2019 3 0 8 2
ENDOFDATA  0

to tempsout.txt

DATE       HIGH  LOW
---------- ---- ----
01/24/2019   31   19
01/25/2019   40   20
01/26/2019    8    0

skeleton of your code

int main() {
    // Open the input file (when fail, respond appropriately)

    // Open the output file (when fail, respond appropriately)

    // Start writing to output file

    // Read lines one by one (loop)

        // Read temperatures get the high and low

        // Write the summary line in "mm/dd/yyyy aaa bbb"

        // Exit loop at 'ENDOFDATA'
    
    // Close files
}

File Handling

print format

  • setw(n): set the column width
  • left or right: align
  • setprecision(m): decimal point
  • fixed: padding with zeroes up to the precision

in

#include <fstream>
ifstream f;
string name; int age; float gpa;
f.open(”mydata.txt”);
if (! f.fail()) {
    f >> name >> age >> gpa;
    f.close();
}

out

#include <fstream>
ofstream f;
f.open(”mydata.txt”);
if (! f.fail()) {
    f << ”Hello world ” << endl;
    f << setw(10) << 3.14159;
    f.close();
}

lab4 assignment

grading rubric

  • Header comment box with all the proper information
  • Use of good variable names; proper indentation; good use of comments
  • Files opened and checks for failure done
  • Header lines written correctly to output file
  • Correct number of lines (days) processed and written to the report (no line for ENDOFDATA)
  • Data for one line read and processed (correct High and Low found) correctly
  • Correct data written to output file for each day, including spacing/widths/justifications specified
  • Files closed correctly
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment