In: Computer Science
1) The provided sched.cpp file is missing a few lines to read in some of the data. The comments in the code describe how there is yet another stream library that works like iostream and fstream, but for processing string’s. Fill in the necessary statements and uncomment the indicated cout statements to get the output below for the attached “sched.txt” file:
CS100
Section 1 has 17 open seats
It is held on MW from 8:00A to 9:15A in SC-S146
It is taught by C. Smith
Section 2 has 11 open seats
It is held on MW from 11:00A to 12:15P in VBT-0222
It is taught by I. Yellapragada
sched.cpp /* Lab 4 part 1. Files and stringstreams */ #include <iostream> #include <fstream> #include <sstream> // like fstream, but for string's #include <iomanip> #include <cstdlib> #include <string> using namespace std; int main() { string dept, crsInfo, secInfo, days, room, lname, finit; unsigned int crsNo, secNo, cap, avail, start_hr, start_min, end_hr, end_min; char skip, start_ampm, end_ampm; ifstream sched("sched.txt"); istringstream secStrm; // Read in Course department, number sched >> dept >> crsNo; // Note that >> does not store the space between // dept and crsNo cout << dept << crsNo << endl; getline(sched, crsInfo); sched.ignore(1); // Process each section while (sched >> secNo) { // Reads rest of line from file into string getline(sched, secInfo); // Set the stringstream to take its data // from this string secStrm.clear(); // first clear what's in the stream secStrm.str(secInfo); // now set the data to process // Now you can use the usual >> operator secStrm >> cap >> skip >> avail; // ADD CODE HERE // use the skip char variable to read in extra char's // or use ignore() cout << "Section " << secNo << " has " << avail << " open seats" << endl; // Uncomment the following to get the rest of the output // after you have extracted these values /* cout << "It is held on " << days << " from " << start_hr << ':' << setw(2) << setfill('0') << start_min << start_ampm << " to " << end_hr << ':' << setw(2) << setfill('0') << end_min << end_ampm << " in " << room << endl << "It is taught by " << finit << ' ' << lname << endl; */ // Get to start of next line in file sched.ignore(1); } sched.close(); // system("pause"); return 0; }
sched.txt
CS 100 - Programming for Everyone (3 Units) 01 45/17 MW SC-S146 08:00A-09:15A Smith C. 02 45/11 MW VBT-0222 11:00A-12:15P Yellapragada I.
If you have any doubts, please give me comment...
/* Lab 4 part 1. Files and stringstreams */
#include <iostream>
#include <fstream>
#include <sstream> // like fstream, but for string's
#include <iomanip>
#include <cstdlib>
#include <string>
using namespace std;
int main()
{
string dept, crsInfo, secInfo, days, room, lname, finit, temp;
unsigned int crsNo, secNo, cap, avail, start_hr, start_min, end_hr, end_min;
char skip, start_ampm, end_ampm;
ifstream sched("sched.txt");
istringstream secStrm;
// Read in Course department, number
sched >> dept >> crsNo;
// Note that >> does not store the space between
// dept and crsNo
cout << dept << crsNo << endl;
getline(sched, crsInfo);
sched.ignore(1);
// Process each section
while (sched >> secNo) {
// Reads rest of line from file into string
getline(sched, secInfo);
// Set the stringstream to take its data
// from this string
secStrm.clear(); // first clear what's in the stream
secStrm.str(secInfo); // now set the data to process
// Now you can use the usual >> operator
secStrm >> cap >> skip >> avail>>days>>room>>temp>>lname>>finit;
start_hr = stoi(temp.substr(0, 2));
start_min = stoi(temp.substr(3, 2));
start_ampm = temp[5];
end_hr = stoi(temp.substr(7, 2));
end_min = stoi(temp.substr(10, 2));
end_ampm = temp[12];
// ADD CODE HERE
// use the skip char variable to read in extra char's
// or use ignore()
cout << "Section " << secNo
<< " has " << avail << " open seats" << endl;
// Uncomment the following to get the rest of the output
// after you have extracted these values
cout << "It is held on " << days
<< " from " << start_hr << ':'
<< setw(2) << setfill('0')
<< start_min << start_ampm
<< " to " << end_hr << ':'
<< setw(2) << setfill('0')
<< end_min << end_ampm
<< " in " << room << endl
<< "It is taught by " << finit << ' ' << lname << endl;
// Get to start of next line in file
sched.ignore(1);
}
sched.close();
// system("pause");
return 0;
}