I have written a c++ program that takes input from a file and outputs the average. The program uses structs and I need to convert the struct to a class.
I just dont know how to get started and if I will have to re-write my whole program. - #include <cstdlib>
-
#include <iostream>
-
#include <string>
-
#include <vector>
-
#include <fstream>
-
using namespace std;
-
-
ifstream in_file;
-
//structure that holds student record information.
-
typedef struct records{
-
string name;
-
float quizes[7];
-
float projects[6];
-
float exams[2];
-
float labs[14];
-
};
-
-
const int num_quizes = 7;
-
const int num_exams = 2;
-
const int num_projects = 6;
-
const int num_labs = 14;
-
-
const int quizes_pts = 70;
-
const int exams_pts = 200;
-
const int projects_pts = 300;
-
const int labs_pts = 150;
-
-
void get_scores(ifstream& infile, int num_scores, float scores[]);
-
double get_average(float scores[], int num_scores, int tot_pts, float& average);
-
void total_average(float average,float& student_average);
-
void grades_display(int counter,float ave1,float ave2,float ave3,float ave4,float total,char grade);
-
-
-
-
int main()
-
{
-
-
-
cout << "No. Name Quiz Project Exam Lab Total Grade"
-
<< endl;
-
cout << " --- --------- ----- ---------- ------ ----- ------- -----"
-
<< endl;
-
cout << endl;
-
-
-
records grades;
-
ifstream in_file;
-
// Define a file input stream and open the file
-
in_file.open("record.txt"); //opening file
-
-
if (in_file.fail())
-
{
-
// Failed to open the file (file doesn't exist or isn't readable)
-
cout << "Could not open file: "<< "\n";
-
exit(1);
-
}
-
float average = 0;
-
int counter = 1;
-
while(!in_file.eof())
-
{
-
//get string from the file
-
in_file >> grades.name;
-
-
-
-
char letter_grade;
-
float student_average = 0;
-
-
-
// Repeatedly get characters from the file
-
-
get_scores(in_file, num_quizes, grades.quizes); //function for inputing info in struct.
-
get_average(grades.quizes, num_quizes, quizes_pts, average); //function for average
-
total_average(average,student_average);
-
float quiz_average = average;
-
-
get_scores(in_file, num_projects, grades.projects); //input project scores
-
get_average(grades.projects, num_projects, projects_pts, average);
-
total_average(average,student_average);
-
float project_average = average;
-
-
get_scores(in_file, num_exams, grades.exams); //input exam scores
-
get_average(grades.exams, num_exams, exams_pts, average);
-
total_average(average,student_average);
-
float exams_average = average;
-
-
get_scores(in_file, num_labs, grades.labs); //input lab scores
-
get_average(grades.labs, num_labs, labs_pts, average);
-
total_average(average,student_average);
-
float labs_average = average;
-
-
student_average = student_average/4;
-
grades_display(counter,quiz_average,project_average,exams_average,labs_average,student_average,letter_grade);
-
-
counter++;
-
}
-
-
-
// Close the file
-
in_file.close();
-
-
-
return 0;
-
}
-
-
-
-
//function that takes in parameters to input scores in array
-
void get_scores(ifstream& in_file, int num_scores, float scores[])
-
{
-
-
for (int i = 0; i < num_scores; i++)
-
{
-
in_file >> scores[i];
-
-
}
-
-
}
-
-
//funtion that takes the average of the inputed scores
-
double get_average(float scores[], int num_scores,int tot_pts,float& average)
-
{
-
float a = 0;
-
-
for (int i = 0; i < num_scores; i++)
-
{
-
a = scores[i] + a;
-
}
-
average = (a/tot_pts);
-
average = average * 100;
-
-
return average;
-
}
-
-
//function to take total average for each member
-
void total_average(float average,float& student_average)
-
{
-
-
student_average = average + student_average;
-
-
}
-
-
//function to output letter grade and output scores
-
void grades_display(int counter,float ave1,float ave2,float ave3,float ave4,float total,char grade)
-
{
-
-
if(90 < total)
-
grade = 'A';
-
if((80 < total)&&(total < 90))
-
grade = 'B';
-
if((70 < total)&&(total < 80))
-
grade = 'C';
-
if((60 < total)&&(total < 70))
-
grade = 'D';
-
if (total < 60)
-
grade = 'F';
-
-
cout << " " << counter << " " << ave1 << " " << ave2 << " " << ave3 << " " << ave4 << " " << total << " " << grade;
-
-
-
-
}
-
would it be easier to put the class program in separate files
9 3382
Just start with your struct: -
struct records{
-
string name;
-
float quizes[7];
-
float projects[6];
-
float exams[2];
-
float labs[14];
-
};
-
;
and put your member function names inside the struct: -
struct records{
-
string name;
-
float quizes[7];
-
float projects[6];
-
float exams[2];
-
float labs[14];
-
-
void get_scores(ifstream& in_file, int num_scores, float scores[]);
-
-
};
-
Then code the function as: -
//function that takes in parameters to input scores in array
-
void records::get_scores(ifstream& in_file, int num_scores, float scores[])
-
{
-
-
for (int i = 0; i < num_scores; i++)
-
{
-
in_file >> scores[i];
-
-
}
-
-
}
-
Then make the data private and the function public: -
struct records{
-
private:
-
string name;
-
float quizes[7];
-
float projects[6];
-
float exams[2];
-
float labs[14];
-
public:
-
void get_scores(ifstream& in_file, int num_scores, float scores[]);
-
-
};
-
Re-compile and verify everything is still working.
Finally, just change struct to class and tou are done. -
class records{
-
private:
-
string name;
-
float quizes[7];
-
float projects[6];
-
float exams[2];
-
float labs[14];
-
public:
-
void get_scores(ifstream& in_file, int num_scores, float scores[]);
-
-
};
-
You can now start removing function arguments that are member data.
Finally, add your constructors.
It turns out in C++ that there really are no classes. The underlying implementation is a struct with public/private/protected members. In fact, the only differende between a struct and a class is that the DEFAULT access spoecificer for a struct is public whereas the default for a class is prviate. If you specify public/private/protected for each of your struct members, then there is no difference between a struct and a class.
You can now start removing function arguments that are member data.
Finally, add your constructors.
It turns out in C++ that there really are no classes. The underlying implementation is a struct with public/private/protected members. In fact, the only differende between a struct and a class is that the DEFAULT access spoecificer for a struct is public whereas the default for a class is prviate. If you specify public/private/protected for each of your struct members, then there is no difference between a struct and a class.[/quote]
if I remove the arguments in my functions that are member data how will my code work, would I just have to not use functions?
if I remove the arguments in my functions that are member data how will my code work, would I just have to not use functions?
if my member arrays are private and I have a function that will add the values to my arrays. how can I then pass those values on to the other functions that need them or am I able to do that. ..Im really not sure of how to implement what I'm trying to do.
i guess my problem is that since my input is coming from a file Im having a hard time figuring out how to code that for a class since I can just set the values of my string and arrays at the beginning. And after I set the values in my class array how would I pass the values to the other functions that need them?
Let's start at this point: -
//function that takes in parameters to input scores in array
-
void records::get_scores(ifstream& in_file, int num_scores, float scores[])
-
{
-
-
for (int i = 0; i < num_scores; i++)
-
{
-
in_file >> scores[i];
-
-
}
-
-
}
-
Here the target array is a private member of the class. To load the projects array write a member fuction to load it using the above function as a pattern: -
//function that takes in parameters to input projects in array
-
void records::set_projects(ifstream& in_file)
-
{
-
-
for (int i = 0; i < 6; i++)
-
{
-
in_file >> projects[i];
-
-
}
-
-
}
-
You don't need the pointer to the array because this function only does the projects array and you don't need the number of elements in the array becuse you have hard-coded the 6 in the class definition. Just create a member function to load each of your arrays and other member functions to access the array members.
I realize that you will have several functions that are mostly identical exceot fior the array name and the array size.
okay I got that now lets say i have created these - public:
-
void set_name(ifstream& in_file);
-
void set_quizes(ifstream& in_file);
-
void set_projects(ifstream& in_file);
-
void set_exams(ifstream& in_file);
-
void set_labs(ifstream& in_file);
and this is the one for the string member - void records::set_name(ifstream& in_file)
-
{
-
in_file >> name;
-
}
I then want to pass the string to another function to output it in a display.
I have loaded the array values but how can they be used by another function?
is it by passing records::quizes as an argument
The "another function" will have to be a member function since the arrays are private. So you have: -
void records::show_projects()
-
{
-
for (int i = 0; i < 6; i++)
-
{
-
cout << projects[i];
-
-
}
-
-
}
-
This will show all 6 projects.
If you need the value of a specific project, then you need a member function for that purpose: -
float records::get_project(int val)
-
{
-
//might be good to check val between 0 and 5 here to avoid a crash
-
return projects[val];
-
}
-
That is, no function outside the class has access to the array. Those external functions call member functions of records to get the information you need. The idea is that the member functions will protect the projects array and keep it from getting screwed up.
so for the most part the functions that need to use my class members should go in the public part. and then the other functions should be okay
The public functions are the class interface (an API if you will). These functions protect the private data from hackers an other evildoers. They also have the advantage that should the class need to be redesigned (which happens a lot) and if you can keep the function prototypes unchaned, then the only functions needing modification will be the member functions. To use your new design the class customers would only need to recompile.
This is why you are to never have public data.
Now, a function needing access to private data is out of luck unless there is a class member function that can provde the necessary data. You just don't put any old function inside the class.
So here is a function to display the project values: -
//num is the project to display:
-
void display_projects(records* obj, in num)
-
{
-
for (int i = 0; i < 6; i++)
-
{
-
cout << i << ". " << obj-> get_project(i);
-
-
}
-
-
}
-
This line:
obj-> get_project(i);
is the call to the member function
int records::get_project(int value);
You DO NOT put the display_projects function in the records class as a member.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: phoenix |
last post by:
Hello,
I've got a design question. I need to keep track of some variables and I am
planning to put them inside a class or struct. Basically I'm talking about
10 bools, 20 ints and 2 arrays of...
|
by: Xandau |
last post by:
hello all,
i wotk with java every day but last time i have interested in C#.
everything goes great except one thing...
in java everything is a reference (except plain types) so i thought
that...
|
by: Flip |
last post by:
I'm coming from the java world, so I'm sorry if this is a simplistic
question. I've read enough to be dangerous :< and would like a quick
overview.
In java, everything with implemenation is...
|
by: Brian |
last post by:
I am "learning" C# and have run into a problem that, though I can work around
it, I would like to know what the *right* way to handle the issue is.
I have created an "Info" struct and assigned...
|
by: Kalpesh |
last post by:
Hello All,
Please help validate this design problem
Assume that I have several entities in my project (eg Supplier,
Customer etc).
All of them save several common properties - name, address,...
|
by: arcticool |
last post by:
I had an interview today and I got destroyed :(
The question was why have a stack and a heap?
I could answer all the practical stuff like value types live on the
stack, enums are on the stack, as...
|
by: Marty |
last post by:
I am new to C# and to structs so this could be easy or just not
possible.
I have a struct defined called Branch
If I use Branch myBranch = new Branch(i); // everything works
If I use Branch...
|
by: heddy |
last post by:
I have an array of objects. When I use Array.Resize<T>(ref Object,int
Newsize); and the newsize is smaller then what the array was
previously, are the resources allocated to the objects that are...
|
by: colin |
last post by:
Hi,
I profile my code and find its spending a lot of time doing implicit
conversions from similar structures.
the conversions are mainly things like this
class Point
{
implicit conversion...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
| |