Question

C++ Question

Question 3 [43] The Question: You are required to write a program for an Online Tutoring Centre to determine the weekly rateETutoring() 3 fillArray(string): void This constructor must initialise all the data members to default values. The method mus2 This option must read the data from the file, and populate all arrays. After populating, display the information in the arrEnter learners name : Pam Enter grade : 8 Enter number of sessions : 2 grade is 8 The cost is : R 148 1: Fill arrays 2: Enter

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <cstdlib>
#define MAXG 5
#define MAXS 4
using namespace std;

// Defines class ETutoring
class ETutoring
{
// Integer type array of size MAXG to store grades
int grades[MAXG];
// Double type matrix of size MAXG X MAXS to store price
double price[MAXG][MAXS];
// To store total price
double total;
public:
// Default constructor to assign default values to data members
ETutoring()
{
// Loops till maximum number of grade
for(int c = 0; c < MAXG; c++)
// Assigns 0 to each cell
grades[c] = 0;

// Loops till maximum number of grade
for(int r = 0; r < MAXG; r++)
// Loops till maximum number of session
for(int c = 0; c < MAXS; c++)
// Assigns 0.0 to each cell
price[r][c] = 0.0;
// total initializes to 0.0
total = 0.0;
}

// Function to read the file with parameter file name
// and stores the data in data members
void fillArray(string fileName)
{
// Variable for row and column
int row = 0;
int col = 0;
// To store comma
string comma;

// ifstream class object declared to read data from file
ifstream fileRead;

// Opens both the file for reading
fileRead.open(fileName.c_str());

// Checks if the file unable to open for reading display's error message and stop
if(!fileRead)
{
cout<<"\n ERROR: Unable to open the file "<<fileName<<" for reading.";
exit(0);
}// End of if condition

// Loops till end of the file
while(!fileRead.eof())
{
// Reads a grade and stores at row index position
fileRead>>grades[row]>>comma;

// Loops till maximum number of sessions
for(int col = 0; col < MAXS; col++)
{
// Checks if current column number is equals to MAXS minus one (for last column)
if(col == MAXS-1)
// Reads the price and stores at row and column index position (without reading comma)
fileRead>>price[row][col];

// Otherwise not the last column
else
// Reads the price and stores at row and column index position and also reads comma
fileRead>>price[row][col]>>comma;
}

// Increase the row index by one
row++;
}// End of while loop
cout<<"\n End of file reading.\n";
// Closer the file
fileRead.close();
}

// Function to display the grade and each session price
void displayPrices()
{
// Displays heading
cout<<setw(10)<<"\n Grades"<<setw(7)<<"1"<<setw(7)<<"2"<<setw(7)<<"3"<<setw(7)<<"4"<<endl;

// Loops till maximum number of grades
for(int r = 0; r < MAXG; r++)
{
// Displays the current grade
cout<<" "<<left<<setw(7)<<grades[r];

// Loops till maximum number of sessions
for(int c = 0; c < MAXS; c++)
// Displays the current session price
cout<<right<<setw(7)<<price[r][c];
cout<<endl;
}
}

// Function to accept name, grade and number of sessions and returns data using pass by reference
void enterLearning(string &name, int &grade, int &numberOfSessions)
{
// Accepts data
cout<<"\n Enter learners name: ";
cin>>name;
cout<<"\n Enter grade: ";
cin>>grade;
cout<<"\n Enter number of sessions: ";
cin>>numberOfSessions;
}

// Function to determine price based on the parameter grade and number of sessions
// returns the price
double determinePrice(int grade, int sessions)
{
// Initializes 0.0 for either invalid grade or session entered by the user.
double amount = 0.0;
// Checks the parameter grade and assigns appropriate price
switch(grade)
{
case 8:
amount = price[0][sessions-1];
break;
case 9:
amount = price[1][sessions-1];
break;
case 10:
amount = price[2][sessions-1];
break;
case 11:
amount = price[3][sessions-1];
break;
case 12:
amount = price[4][sessions-1];
break;
default:
cout<<"\n Invalid Grade or Session.";
}

// Adds the determine amount to total
total += amount;
// returns the amount
return amount;
}

// Function to return total amount collected
double getTotal()
{
return total;
}

// Function to display menu, accepts user choice and returns it
int menu()
{
int choice;
// Displays menu
cout<<"\n\n 1: Fill Arrays \n 2: Enter learning details \n 3: Total Revenue \n 4: Exit"
<<"\n : Select an option ";
// Accepts user choice
cin>>choice;
// Returns user choice
return choice;
}
};// End of class

// main function definition
int main()
{
// Creates an object of class ETutoring using default constructor
ETutoring et;
string name;
int grade;
int numberOfSessions;

// Loops till user choice is not 4
do
{
// Calls the function to accept user choice
// calls appropriate function based on returned user choice
switch(et.menu())
{
case 1:
et.fillArray("Tutoring.txt");
et.displayPrices();
break;
case 2:
et.enterLearning(name, grade, numberOfSessions);
cout<<"\n Grade is: "<<grade;
cout<<"\n Total cost is R "<<et.determinePrice(grade, numberOfSessions);
break;
case 3:
cout<<"\n Total cost of attending eTutor sessions are: "<<et.getTotal();
break;
case 4:
cout<<"\n Good~Bye";
exit(0);
default:
cout<<"\n Invalid choice!!";
}// End of switch - case
}while(1);// End of do - while loop
return 0;
}// End of main function

Sample Output:

1: Fill Arrays
2: Enter learning details
3: Total Revenue
4: Exit
: Select an option 1

End of file reading.

Grades 1 2 3 4
8 75 140 210 280
9 80 145 215 285
10 85 150 220 290
11 90 155 225 295
12 95 160 230 300


1: Fill Arrays
2: Enter learning details
3: Total Revenue
4: Exit
: Select an option 2

Enter learners name: Pam

Enter grade: 8

Enter number of sessions: 2

Grade is: 8
Total cost is R 140

1: Fill Arrays
2: Enter learning details
3: Total Revenue
4: Exit
: Select an option 2

Enter learners name: Susan

Enter grade: 9

Enter number of sessions: 2

Grade is: 9
Total cost is R 145

1: Fill Arrays
2: Enter learning details
3: Total Revenue
4: Exit
: Select an option 2

Enter learners name: Sam

Enter grade: 10

Enter number of sessions: 2

Grade is: 10
Total cost is R 150

1: Fill Arrays
2: Enter learning details
3: Total Revenue
4: Exit
: Select an option 3

Total cost of attending eTutor sessions are: 435

1: Fill Arrays
2: Enter learning details
3: Total Revenue
4: Exit
: Select an option 4

Good~Bye

Tutoring.txt file contents

8, 75, 140, 210, 280
9, 80, 145, 215, 285
10, 85, 150, 220, 290
11, 90, 155, 225, 295
12, 95, 160, 230, 300

Add a comment
Know the answer?
Add Answer to:
C++ Question Question 3 [43] The Question: You are required to write a program for an...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In this lab, you will create a program to help travelers book flights and hotel stays that will b...

    In this lab, you will create a program to help travelers book flights and hotel stays that will behave like an online booking website. The description of the flights and hotel stays will be stored in two separate String arrays. In addition, two separate arrays of type double will be used to store the prices corresponding to each description. You will create the arrays and populate them with data at the beginning of your program. This is how the arrays...

  • Can you help us!! Thank you! C++ Write a program that can be used by a...

    Can you help us!! Thank you! C++ Write a program that can be used by a small theater to sell tickets for performances. The program should display a screen that shows which seats are available and which are taken. Here is a list of tasks this program must perform • The theater's auditorium has 15 rows, with 30 seats in each row. A two dimensional array can be used to represent the seats. The seats array can be initialized with...

  • *** Please construct a flowchart for the following code (this was in C++): *** #include using...

    *** Please construct a flowchart for the following code (this was in C++): *** #include using namespace std; void printPrompt(int n){    switch(n){        case 1:            cout << "Enter the quiz grades:" <            break;        case 2:            cout << "Enter the assignments grades:" <            break;        case 3:            cout << "Enter the exam grades:" <            break;        case...

  • using java Program: Please read the complete prompt before going into coding. Write a program that...

    using java Program: Please read the complete prompt before going into coding. Write a program that handles the gradebook for the instructor. You must use a 2D array when storing the gradebook. The student ID as well as all grades should be of type int. To make the test process easy, generate the grade with random numbers. For this purpose, create a method that asks the user to enter how many students there are in the classroom. Then, in each...

  • Please, please help with C program. This is a longer program, so take your time. But...

    Please, please help with C program. This is a longer program, so take your time. But please make sure it meets all the requirements and runs. Here is the assignment: I appreciate any help given. Cannot use goto or system(3) function unless directed to. In this exercise you are to create a database of dogs in a file, using open, close(), read, write(), and Iseek(). Do NOT use the standard library fopen() ... calls! Be sure to follow the directions!...

  • Please write this program in C++, thanks Task 9.3 Write a complete C++ program to create a music player Your program sh...

    Please write this program in C++, thanks Task 9.3 Write a complete C++ program to create a music player Your program should read in several album names, each album has up to 5 tracks as well as a genre. First declare genre for the album as an enumeration with at least three entries. Then declare an album structure that has five elements to hold the album name, genre, number of tracks, name of those tracks and track location. You can...

  • I am struggling with a program in C++. it involves using a struct and a class...

    I am struggling with a program in C++. it involves using a struct and a class to create meal arrays from a user. I've written my main okay. but the functions in the class are giving me issues with constructors deconstructors. Instructions A restaurant in the Milky way galaxy called “Green Alien” has several meals available on their electronic menu. For each food item in each meal, the menu lists the calories per gram and the number of grams per...

  • Your assignment is to write a grade book for a teacher. The teacher has a text file, which includ...

    Your assignment is to write a grade book for a teacher. The teacher has a text file, which includes student's names, and students test grades. There are four test scores for each student. Here is an example of such a file: Count: 5 Sally 78.0 84.0 79.0 86.0 Rachel 68.0 76.0 87.0 76.0 Melba 87.0 78.0 98.0 88.0 Grace 76.0 67.0 89.0 0.0 Lisa 68.0 76.0 65.0 87.0 The first line of the file will indicate the number of students...

  • DO NOT USE ARRAYS IF YOU KNOW ABOUT ARRAYS, I WANT YOU TO USE 3 VARIABLES,...

    DO NOT USE ARRAYS IF YOU KNOW ABOUT ARRAYS, I WANT YOU TO USE 3 VARIABLES, IT WILL FORCE YOU TO USE COMPOUND LOGICAL STATEMENTS FOR THE IF STATEMENTS. Create a program that will display a menu to the user. The choices should be as follows: Enter 3 grades Show average (with the 3 grades) and letter grade Show highest grade Show lowest grade Exit If you want to put the menu into a function you may. The program should...

  • hello there. can you please help me to complete this code. thank you. Lab #3 -...

    hello there. can you please help me to complete this code. thank you. Lab #3 - Classes/Constructors Part I - Fill in the missing parts of this code #include<iostream> #include<string> #include<fstream> using namespace std; class classGrades      {      public:            void printlist() const;            void inputGrades(ifstream &);            double returnAvg() const;            void setName(string);            void setNumStudents(int);            classGrades();            classGrades(int);      private:            int gradeList[30];            int numStudents;            string name;      }; int main() {          ...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT