I was assigned to write a program that streams a text file.
The attached file lists the courses to be offered by the BCS department for Spring 2017. It has two types of records, course title records and course section records. The course section records are fixed format (the data is at the same positions in each record) and can be identified by the two blanks in columns 1 and 2. A course title record applies to all the following sections until the next title.
Write a program to read the file and determine all the courses with starting times in the morning (i.e. the "am"). Your program should write the following output:
Of the __ BCS courses for Spring 2017, __ are offered in the morning.
This is a primarily a function exercise, so the program should have two functions that return boolean values:
isTitle() - takes a file record string and returns true if the record is a title record, and false otherwise
isMorning() - takes a file record string and returns true if the record represents a morning section
Use the getline template for reading files. Don't use magic numbers in your program, use named constants.
It may be helpful to recall that a std::string has a substr(pos, len) (substring) method that takes two integer arguments, the starting position and the length, and returns a string of the requested length starting at the requested position. String comparisons can be done with the == operator.
For extra credit, display each of the morning sections with the course department, number, section, and starting time. For example:
BCS 120 23319 MW 09:25 am
So far I have this and am completely stuck where to go next.
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
|
static int assign_courses()
{
static bool isTitle;
static bool isMorning;
std::ifstream ifs;
std::string path = "C:\\Courses.txt";
std::string value;
std::string
int rcd;
// template using streaming
ifs.open(path);
if (!ifs)
{
cout << "file not found:" << path << endl;
return -1;
}
rcd = 0;
ifs >> value;
while (!ifs.eof())
{
//process
}
ifs.close();
cout << "streaming records:" << rcd << endl;
// template using getline()
ifs.open(path);
if (!ifs)
{
cout << "file not found:" << path << endl;
return -1;
}
rcd = 0;
while (getline(ifs, value))
{
++rcd;
if (isTitle(value))
{
//process
}
}
ifs.close();
cout << "getline records:" << rcd << endl;
return 0;
}
|
The text file looks like this.
BCS 102 Computer Concepts & Appl 3 credits
21035 MW 08:00 am - 09:15 am
21036 MW 09:25 am - 10:40 am
21037 MW 10:50 am - 12:05 pm
21038 MW 12:15 pm - 01:30 pm
21039 MW 01:40 pm - 02:55 pm
21040 MW 04:30 pm - 05:45 pm
21041 TR 08:00 am - 09:15 am
21042 TR 09:25 am - 10:40 am
21043 TR 12:15 pm - 01:30 pm
21044 TR 01:40 pm - 02:55 pm
21046 F 09:25 am - 10:40 am, 10:45 am - 12:00 pm
21577 MW 07:20 pm - 08:35 pm
22228 TR 04:30 pm - 05:45 pm
22995 S 09:00 am - 10:15 am, 10:20 am - 12:25 pm
23792 TR 03:05 pm - 04:20 pm
23793 T 07:20 pm - 08:35 pm, 08:45 pm - 10:10 pm
BCS 120 Foundations Computer Prog I 3 credits
20072 MW 05:55 pm - 07:10 pm
21020 TR 01:40 pm - 02:55 pm
22754 MW 12:15 pm - 01:30 pm
23319 MW 09:25 am - 10:40 am
24040 TR 04:30 pm - 05:45 pm
24076 MW 10:50 am - 12:05 pm
24097 MW 03:05 pm - 04:20 pm
BCS 130 Website Development I 3 credits
20073 MW 09:25 am - 10:40 am
20074 TR 08:00 am - 09:15 am
23320 F 09:00 am - 11:40 am
23794 TR 05:55 pm - 07:10 pm
BCS 160 Computers,Society & Technology 3 credits
23707 TR 12:15 pm - 01:30 pm
23708 F 09:25 am - 10:40 am, 10:50 am - 12:05 pm
23709 MW 05:55 pm - 07:10 pm
23795 MW 10:50 am - 12:05 pm
23796 TR 01:40 pm - 02:55 pm
24039 MW 12:15 pm - 01:30 pm
24515 MW 08:00 am - 09:15 am
Any advice is appreciated!!