CSC160 (UG20) 01
    TPS 12: Reading from a file
    Skip To Content
    Dashboard
    • Login
    • Dashboard
    • Calendar
    • Inbox
    • Help
    Close
    • My Dashboard
    • CSC160 (UG20) 01
    • Assignments
    • TPS 12: Reading from a file
    SP 2021 (UNDG)
    • Home
    • Assignments
    • Modules
    • TC/WC Appt Scheduler
    • Collaborations
    • WSJ Context
    • EvaluationKIT Course
    • EvaluationKIT
    • Barnes & Noble Bookstore

    TPS 12: Reading from a file

    • Due Apr 21, 2021 by 10am
    • Points 1
    • Submitting a file upload
    • File Types doc and docx
    • Available after Apr 21, 2021 at 9am

    Consider the following program (a version of TPS11 that reads in from a file instead of the user) and complete the three TODOs.

      1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
     13
     14
     15
     16
     17
     18
     19
     20
     21
     22
     23
     24
     25
     26
     27
     28
     29
     30
     31
     32
     33
     34
     35
     36
     37
     38
     39
     40
     41
     42
     43
     44
     45
     46
     47
     48
     49
     50
     51
     52
     53
     54
     55
     56
     57
     58
     59
     60
     61
     62
     63
     64
     65
     66
     67
     68
     69
     70
     71
     72
     73
     74
     75
     76
     77
     78
     79
     80
     81
     82
     83
     84
     85
     86
     87
     88
     89
     90
     91
     92
     93
     94
     95
     96
     97
     98
     99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    //==============================================================================
    // File:    tps12.cpp
    // Author:  Hank Feild
    // Date:    14-Apr-2021
    // Purpose: Demonstrates reading in data from a file.
    //==============================================================================
    #include <iostream>
    #include <fstream>
    #include <vector>
    using namespace std;
    
    struct Car {
        string make, model;
        int year, miles;
        bool has4WheelDrive;
    };
    
    /**
     * Reads in a car's information from the given input file stream. Data in the
     * file is expected to be in this format (one piece of data per line)
     * 
     *    make
     *    model
     *    year
     *    miles
     *    2WD (yes/no)
     * 
     * @param fin The input file stream to read the data from. 
     * @return A Car populated with the next chunk of data from the input stream.
     */
    Car ReadCarFromFile(ifstream &fin){
        Car car;
        string fourWheelDriveResponse;
    
        // TODO 1: below is the code we used to read in info about a car from
        // the user. Adapt this to read in from the input file (fin). Do we 
        // need the prompts?
    
        cout << "Please enter the following data about the car:" << endl;
    
        cout << "Make (Ford/Toyota/etc.): ";
        getline(cin, car.make);
    
        cout << "Model (F-150, Corolla, etc.): ";
        getline(cin, car.model);
    
        cout << "Year: ";
        cin >> car.year;
    
        cout << "Miles: ";
        cin >> car.miles;
        cin.ignore();
        
        cout << "Does it have 4WD? [yes/no]: ";
        getline(cin, fourWheelDriveResponse);
        car.has4WheelDrive = fourWheelDriveResponse == "yes";
    
        return car;
    }
    
    /**
     * Prints out a Car in a nice format:
     * 
     *   <year> <make> <model> with <miles> miles with<out?> 4-wheel drive
     * 
     * @param car The Car to display.
     */
    void PrintCar(Car car){
        cout << car.year << " " << car.make << " " << car.model
             << " with " << car.miles << " miles with";
        if(!car.has4WheelDrive){
            cout << "out";
        }
        cout << " 4-wheel drive" << endl;
    }
    
    /**
     * Asks the user for three cars, then prints them out.
     * 
     * @return The exit status of the program; 0 is good.
     */
    int main(){
        vector<Car> cars;
        ifstream fin;
        string filename;
        Car car;
    
        cout << "Enter the name of the file with car data: ";
        getline(cin, filename);
    
        // TODO 2: Open the file for reading (the variable `filename` has the name
        // of the file and `fin` is our input file stream variable).
    
        
        // Check if it opened okay; we can exit if it didn't.
        if(!fin.is_open()){
            cout << "The file could not be opened.";
            exit(1);
        }
    
        // Read all the cars in from the file.
        while(fin.good()){
            // TODO 3: the invocation of ReadCarFromFile is missing an argument; fix it.
            car = ReadCarFromFile();
    
            // Only add the car if all of its data was read in without error
            // (fin.good() will be true if there were no errors).
            if(fin.good()){
                cars.push_back(car);
            }
        }
    
        // Close the file -- always do this once you're finished with a file.
        fin.close();
    
        // Add 10 miles to each car.    
        for(int i = 0; i < cars.size(); i++){
            cars.at(i).miles += 10;
        }
    
        // Print the cars.
        for(int i = 0; i < cars.size(); i++){
            PrintCar(cars.at(i));
        }
    
        return 0;
    }
    

    After we've completed the group share portion, upload a copy of your updated TPS document.

    1619013600 04/21/2021 10:00am
    Additional Comments:
    Rating max score to > pts

    Rubric

     
     
     
     
     
     
     
         
    Can't change a rubric once you've started using it.  
    Find a Rubric
    Find Rubric
    Title
    You've already rated students with this rubric. Any major changes could affect their assessment results.
    Title
    Criteria Ratings Pts
    Edit criterion description Delete criterion row
    This criterion is linked to a Learning Outcome Description of criterion
    threshold: 5 pts
    Edit rating Delete rating
    5 to >0 pts
    Full Marks
    blank
    Edit rating Delete rating
    0 to >0 pts
    No Marks
    blank_2
    This area will be used by the assessor to leave comments related to this criterion.
    pts
      / 5 pts
    --
    Additional Comments
    Total Points: 5 out of 5