Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(Exercise 2) Create-courselnfo.cpp In this exercise you will expand your timeInp

ID: 3675365 • Letter: #

Question

(Exercise 2) Create-courselnfo.cpp In this exercise you will expand your timeInput.cpp, which only deals with lecture times, so that you can have more information about each lecture, and print a schedule to the student. More specifically, you should keep your Time structure from timeInput.cpp and create a new Course structure consisted of the following variables: 1. name, type string, which stores the name of the course 2. credits, type int, which stores the number of credits for the course 3. majorRequirement, type bool, whfch tells if the course is a requirement for your major 4. avgGrade, type double, which gives the average grade (in percentage) that past students received in the course 5. days, type string, which stores the days that lectures are held 6. startTime, type Time (from exercise 1), which is the start time of the lecture 7. endTime, type Time (from exercise 1), which is the end time of the lecture Your program should read the course information from a text file called "in.txt". This file should contain the following pieces of information on the lines specified below: Line 1: Course number: How many courses the user wants to sign up for. Line 2: Name of the course Line 3: Credits Line 4: Is the course a major requirement? (1-yes, 0-no) Line 5: Average grade for the course Line 6: Lecture days Line 7: Start time of the lecture Line 8: End time of the lecture Line 9-15: Repeat same information for additional course, if it exists. In order to receive full credit for this part of the lab, you MUST create the two In order to read the avgGrade, you will need to convert from strings to floats. The Note: structures, Time and Course, consisted of the proper variables. function you can use to do so is atof, which takes in an array of characters as a parameter (again, NOT a string). To convert a string to an array of characters, use the function cstrO.

Explanation / Answer

#include<iostream>

#include<fstream>

#include<stdio.in>

void convert(int&, int&,int&, char&);

void convert(int& hours, int& minutes,int& seconds, char& ampm)

{

//set AM/PM flag to PM if hour is 13 onwards, and set hours to 12hr format

if(hours > 12)

{

hours = hours - 12;

ampm = 'p';

}

else if(hours == 12) ampm = 'p'; //set AM/PM flag to PM if hour is 12 exactly, since 12 lunch time is classed as PM

else ampm = 'a'; //if neither of above true, must be AM

}

struct course {

string name;

int courseNo, credits, hours1 minutes1, seconds1,hours2, minutes2, seconds2;

string lectureDays;

bool majorReq;

double avgGrade;

};

int main(){

char ampm;

course c1;

int choice;

cout<<" 1. Create course 2. Exit Enter your choice:";

cin>>choice;

do{

switch(choice) {

case 1:

ofstream outfile;

outfile.open("in.txt");

cout<<"Enter course Number:"<<endl;

cin>>c1.courseNo;

cout<<"Enter Name of the course:"<<endl;

cin.getline(c1.name);

cout<<"Enter credits:"<<endl;

cin>>c1.credits;

cout<<"Enter major requirements:"<<endl;

cin>>c1.majReq;

cout<<"Enter avg Grade:"<<endl;

cin>>c1.avgGrade;

cout<<"Enter Lecture days:"<<endl;

cin.getline(c1.lectureDays);

cout<<"Enter Start Time:"<<endl;

cin>> c1.hours1>>":">>c1.minutes1>>":">>c1.seconds1;

cout<<"Enter End Time:"<<endl;

cin>>c1.hours2>>":">>c1.minutes2>>":">>c1.seconds2;

break;

case 2: break;

default: cout<<"invalid choice";

}

}while(ch!=0);

ifstream infile;

infile.open("in.txt");

cout<<"Course Number:"<<c1.courseNo<<endl;

cout<<"Name of the course:"<<c1.name<<endl;

cout<<"Number of credits:"<<c1.credits<<endl;

if(c1.majReq!=0)

cout<<"Major Requirements:"<<c1.majReq<<endl;

else

cout<<"Note: this course is not a major requirement...";

cout<<"Lecture days:"<<c1.lectureDays<<endl;

cout<<"Lecture Time:";

convert(c1.hours1,c1.minutes,c1.seconds,ampm);

if(ampm == 'p'){

if(minutes < 10)

cout << hours << ":0" << minutes << " P.M.";
else

cout << hours << ":" << minutes << " P.M.";

}

else{

if(minutes < 10)

cout << hours << ":0" << minutes << " A.M.";

else

cout << hours << ":" << minutes << " A.M.";

}

convert(c1.hours2,c1.minutes2,c1.seconds2,ampm);

if(ampm == 'p')

{

if(minutes < 10)

cout << "-"<<hours << ":0" << minutes << " P.M."<<endl;
else

cout<<"-" << hours << ":" << minutes << " P.M."<<endl;

}

else

{

if(minutes < 10)

cout << "-"<<hours << ":0" << minutes << " A.M."<<endl;

else

cout << "-"<<hours << ":" << minutes << " A.M."<<endl;

}

cout<<"Stat: on average students get " << c1.avgGradeGrade<<"% in this course."<<endl;

}