C++ grades calculator using pointers











up vote
5
down vote

favorite
1












I have a program to calculate grades that uses pointers and I am trying to figure out if I did it right and if there are any problems with my implementation.



I'm trying to get a more firm grasp on pointers in C++ and have been using them more frequently in my practice programs. I was looking at my grades and thought to myself, "what if I made a program that could calculate grades just like this." Essentially the features it has are: having a Class (not a C++ class, a school class), that has categories. Categories can be things like, "Tests," and, "Classwork," which can be weighted differently. Then you can add assignments into the categories and the grades will be calculated accordingly. You can also change the categories name and weight, as well as attributes of each individual grade. I haven't fully completed the program yet and I plan to redo this code so that it is a little bit less redundant and well, bad; but the features I have seem to work, and my main concern are the pointers and if I am using them correctly/have any problems with them.



One other thing I should mention is that the reason I am using raw pointers is for learning, I know I could probably use something else like smart pointers, or unique pointers.



Class.h:



#include <map>
#include <vector>

class Class{
private:
struct grade;
struct category;

std::vector<category*> categories;
public:
void addGrade(const std::string&, const std::string&, float, float);
void addCategory(const std::string&, float);

void deleteGradeAtIndex(const std::string&, int);
void deleteGradeByName(const std::string&, const std::string&);
void deleteCategory(const std::string&);

void changeGradeAtIndex(const std::string&, int, const std::string&, float, float);
void changeGradeByName(const std::string&, const std::string&, const std::string&, float, float);
void changeCategory(const std::string&, const std::string&, float);

void printGrades();
};


Class.cpp:



#include <iostream>
#include <string>
#include "Class.h"

struct Class::category{
float weight;
float percentage;
std::string name;
std::vector<grade*> grades;
};

struct Class::grade{
float ptsEarned;
float ptsPossible;
float percentage;
std::string name;
};

void Class::addCategory(const std::string& name, float weight){
category* temp = new category;

temp->weight = weight;
temp->name = name;

categories.push_back(temp);
}

void Class::addGrade(const std::string& category, const std::string& name, float pEarn, float pPoss){
for(uint32_t i = 0; i < categories.size(); i++){
if(categories[i]->name.compare(category) == 0){
grade* temp = new grade;

temp->name = name;
temp->ptsEarned = pEarn;
temp->ptsPossible = pPoss;
temp->percentage = (pEarn/pPoss)*100;

categories[i]->grades.push_back(temp);

float pos = 0, ear = 0;

for(uint32_t j = 0; j < categories[i]->grades.size(); j++){
pos += categories[i]->grades[j]->ptsPossible;
ear += categories[i]->grades[j]->ptsEarned;
}
categories[i]->percentage = (ear/pos)*100;

return;
}
}

std::cout << "category not found" << std::endl;
}

void Class::deleteGradeAtIndex(const std::string& category, int index){
for(uint32_t i = 0; i < categories.size(); i++){
if(categories[i]->name.compare(category) == 0){
delete categories[i]->grades[index];
categories[i]->grades[index] = nullptr;

categories[i]->grades.erase(categories[i]->grades.begin() + index);
return;
}
}
}

void Class::deleteGradeByName(const std::string& category, const std::string& gradeName){
for(uint32_t i = 0; i < categories.size(); i++){
if(categories[i]->name.compare(category) == 0){
for(uint32_t j = 0; j < categories[i]->grades.size(); j++){
if(categories[i]->grades[j]->name.compare(gradeName) == 0){
delete categories[i]->grades[j];
categories[i]->grades[j] = nullptr;

categories[i]->grades.erase(categories[i]->grades.begin() + j);
return;
}
}
}
}

}
void Class::deleteCategory(const std::string& category){
for(uint32_t i = 0; i < categories.size(); i++){
if(categories[i]->name.compare(category) == 0){
for(uint32_t j = 0; j < categories[i]->grades.size(); j++){
delete categories[i]->grades[j];
categories[i]->grades[j] = nullptr;

categories[i]->grades.erase(categories[i]->grades.begin() + j);
}
delete categories[i];
categories[i] = nullptr;

categories.erase(categories.begin() + i);
return;
}
}
}

void Class::changeCategory(const std::string& category, const std::string& newName, float newWeight){
for(uint32_t i = 0; i < categories.size(); i++){
if(categories[i]->name.compare(category) == 0){
categories[i]->name = newName;
categories[i]->weight = newWeight;
return;
}
}
}

void Class::changeGradeAtIndex(const std::string& category, int index, const std::string& newName, float pEarn, float pPoss){
for(uint32_t i = 0; i < categories.size(); i++){
if(categories[i]->name.compare(category) == 0){
categories[i]->grades[index]->name = newName;
categories[i]->grades[index]->ptsEarned = pEarn;
categories[i]->grades[index]->ptsPossible = pPoss;
categories[i]->grades[index]->percentage = (pEarn/pPoss)*100;

float pos = 0, ear = 0;

for(uint32_t j = 0; j < categories[i]->grades.size(); j++){
pos += categories[i]->grades[j]->ptsPossible;
ear += categories[i]->grades[j]->ptsEarned;
}
categories[i]->percentage = (ear/pos)*100;

return;
}
}
}

void Class::changeGradeByName(const std::string& category, const std::string& gradeName, const std::string& newName, float pEarn, float pPoss){
for(uint32_t i = 0; i < categories.size(); i++){
if(categories[i]->name.compare(category) == 0){
for(uint32_t j = 0; j < categories[i]->grades.size(); j++){
if(categories[i]->grades[j]->name.compare(gradeName) == 0){
categories[i]->grades[j]->name = newName;
categories[i]->grades[j]->ptsEarned = pEarn;
categories[i]->grades[j]->ptsPossible = pPoss;
categories[i]->grades[j]->percentage = (pEarn/pPoss)*100;

float pos = 0, ear = 0;

for(uint32_t j = 0; j < categories[i]->grades.size(); j++){
pos += categories[i]->grades[j]->ptsPossible;
ear += categories[i]->grades[j]->ptsEarned;
}
categories[i]->percentage = (ear/pos)*100;

return;
}
}
}
}
}

void Class::printGrades(){
for(uint32_t i = 0; i < categories.size(); i++){
std::cout << "Category: " << categories[i]->name << " | weight: " << categories[i]->weight << " | " << categories[i]->percentage << "%n";

for(uint32_t j = 0; j < categories[i]->grades.size(); j++){
std::cout << "tassignment: " << categories[i]->grades[j]->name
<< " | points earned: " << categories[i]->grades[j]->ptsEarned
<< " | points possible: " << categories[i]->grades[j]->ptsPossible
<< " | " << categories[i]->grades[j]->percentage << "%" << std::endl;
}
}
}


main.cpp:



int main()
{
Class math;
math.addCategory("tests", 70);
math.addGrade("tests", "test1", 95, 100);
math.addGrade("tests", "test2", 80, 100);
math.addCategory("class work", 30);
math.addGrade("class work", "assignment1", 100, 100);
math.addGrade("class work", "assignment2", 100, 100);

math.deleteGradeByName("class work", "assignment1");

math.changeGradeAtIndex("tests", 0, "test1c", 93, 100);
math.changeGradeByName("tests", "test2", "test2c", 95, 100);

math.printGrades();
}









share|improve this question









New contributor




KevinCMD is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • I know I could probably use something else like smart pointers, or unique pointers. Or no pointers at all ;)
    – Rakete1111
    15 hours ago










  • By the way, Class is a horrible name for a class. Could you think of something more descriptive? What is the class actually doing?
    – Juho
    13 hours ago















up vote
5
down vote

favorite
1












I have a program to calculate grades that uses pointers and I am trying to figure out if I did it right and if there are any problems with my implementation.



I'm trying to get a more firm grasp on pointers in C++ and have been using them more frequently in my practice programs. I was looking at my grades and thought to myself, "what if I made a program that could calculate grades just like this." Essentially the features it has are: having a Class (not a C++ class, a school class), that has categories. Categories can be things like, "Tests," and, "Classwork," which can be weighted differently. Then you can add assignments into the categories and the grades will be calculated accordingly. You can also change the categories name and weight, as well as attributes of each individual grade. I haven't fully completed the program yet and I plan to redo this code so that it is a little bit less redundant and well, bad; but the features I have seem to work, and my main concern are the pointers and if I am using them correctly/have any problems with them.



One other thing I should mention is that the reason I am using raw pointers is for learning, I know I could probably use something else like smart pointers, or unique pointers.



Class.h:



#include <map>
#include <vector>

class Class{
private:
struct grade;
struct category;

std::vector<category*> categories;
public:
void addGrade(const std::string&, const std::string&, float, float);
void addCategory(const std::string&, float);

void deleteGradeAtIndex(const std::string&, int);
void deleteGradeByName(const std::string&, const std::string&);
void deleteCategory(const std::string&);

void changeGradeAtIndex(const std::string&, int, const std::string&, float, float);
void changeGradeByName(const std::string&, const std::string&, const std::string&, float, float);
void changeCategory(const std::string&, const std::string&, float);

void printGrades();
};


Class.cpp:



#include <iostream>
#include <string>
#include "Class.h"

struct Class::category{
float weight;
float percentage;
std::string name;
std::vector<grade*> grades;
};

struct Class::grade{
float ptsEarned;
float ptsPossible;
float percentage;
std::string name;
};

void Class::addCategory(const std::string& name, float weight){
category* temp = new category;

temp->weight = weight;
temp->name = name;

categories.push_back(temp);
}

void Class::addGrade(const std::string& category, const std::string& name, float pEarn, float pPoss){
for(uint32_t i = 0; i < categories.size(); i++){
if(categories[i]->name.compare(category) == 0){
grade* temp = new grade;

temp->name = name;
temp->ptsEarned = pEarn;
temp->ptsPossible = pPoss;
temp->percentage = (pEarn/pPoss)*100;

categories[i]->grades.push_back(temp);

float pos = 0, ear = 0;

for(uint32_t j = 0; j < categories[i]->grades.size(); j++){
pos += categories[i]->grades[j]->ptsPossible;
ear += categories[i]->grades[j]->ptsEarned;
}
categories[i]->percentage = (ear/pos)*100;

return;
}
}

std::cout << "category not found" << std::endl;
}

void Class::deleteGradeAtIndex(const std::string& category, int index){
for(uint32_t i = 0; i < categories.size(); i++){
if(categories[i]->name.compare(category) == 0){
delete categories[i]->grades[index];
categories[i]->grades[index] = nullptr;

categories[i]->grades.erase(categories[i]->grades.begin() + index);
return;
}
}
}

void Class::deleteGradeByName(const std::string& category, const std::string& gradeName){
for(uint32_t i = 0; i < categories.size(); i++){
if(categories[i]->name.compare(category) == 0){
for(uint32_t j = 0; j < categories[i]->grades.size(); j++){
if(categories[i]->grades[j]->name.compare(gradeName) == 0){
delete categories[i]->grades[j];
categories[i]->grades[j] = nullptr;

categories[i]->grades.erase(categories[i]->grades.begin() + j);
return;
}
}
}
}

}
void Class::deleteCategory(const std::string& category){
for(uint32_t i = 0; i < categories.size(); i++){
if(categories[i]->name.compare(category) == 0){
for(uint32_t j = 0; j < categories[i]->grades.size(); j++){
delete categories[i]->grades[j];
categories[i]->grades[j] = nullptr;

categories[i]->grades.erase(categories[i]->grades.begin() + j);
}
delete categories[i];
categories[i] = nullptr;

categories.erase(categories.begin() + i);
return;
}
}
}

void Class::changeCategory(const std::string& category, const std::string& newName, float newWeight){
for(uint32_t i = 0; i < categories.size(); i++){
if(categories[i]->name.compare(category) == 0){
categories[i]->name = newName;
categories[i]->weight = newWeight;
return;
}
}
}

void Class::changeGradeAtIndex(const std::string& category, int index, const std::string& newName, float pEarn, float pPoss){
for(uint32_t i = 0; i < categories.size(); i++){
if(categories[i]->name.compare(category) == 0){
categories[i]->grades[index]->name = newName;
categories[i]->grades[index]->ptsEarned = pEarn;
categories[i]->grades[index]->ptsPossible = pPoss;
categories[i]->grades[index]->percentage = (pEarn/pPoss)*100;

float pos = 0, ear = 0;

for(uint32_t j = 0; j < categories[i]->grades.size(); j++){
pos += categories[i]->grades[j]->ptsPossible;
ear += categories[i]->grades[j]->ptsEarned;
}
categories[i]->percentage = (ear/pos)*100;

return;
}
}
}

void Class::changeGradeByName(const std::string& category, const std::string& gradeName, const std::string& newName, float pEarn, float pPoss){
for(uint32_t i = 0; i < categories.size(); i++){
if(categories[i]->name.compare(category) == 0){
for(uint32_t j = 0; j < categories[i]->grades.size(); j++){
if(categories[i]->grades[j]->name.compare(gradeName) == 0){
categories[i]->grades[j]->name = newName;
categories[i]->grades[j]->ptsEarned = pEarn;
categories[i]->grades[j]->ptsPossible = pPoss;
categories[i]->grades[j]->percentage = (pEarn/pPoss)*100;

float pos = 0, ear = 0;

for(uint32_t j = 0; j < categories[i]->grades.size(); j++){
pos += categories[i]->grades[j]->ptsPossible;
ear += categories[i]->grades[j]->ptsEarned;
}
categories[i]->percentage = (ear/pos)*100;

return;
}
}
}
}
}

void Class::printGrades(){
for(uint32_t i = 0; i < categories.size(); i++){
std::cout << "Category: " << categories[i]->name << " | weight: " << categories[i]->weight << " | " << categories[i]->percentage << "%n";

for(uint32_t j = 0; j < categories[i]->grades.size(); j++){
std::cout << "tassignment: " << categories[i]->grades[j]->name
<< " | points earned: " << categories[i]->grades[j]->ptsEarned
<< " | points possible: " << categories[i]->grades[j]->ptsPossible
<< " | " << categories[i]->grades[j]->percentage << "%" << std::endl;
}
}
}


main.cpp:



int main()
{
Class math;
math.addCategory("tests", 70);
math.addGrade("tests", "test1", 95, 100);
math.addGrade("tests", "test2", 80, 100);
math.addCategory("class work", 30);
math.addGrade("class work", "assignment1", 100, 100);
math.addGrade("class work", "assignment2", 100, 100);

math.deleteGradeByName("class work", "assignment1");

math.changeGradeAtIndex("tests", 0, "test1c", 93, 100);
math.changeGradeByName("tests", "test2", "test2c", 95, 100);

math.printGrades();
}









share|improve this question









New contributor




KevinCMD is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • I know I could probably use something else like smart pointers, or unique pointers. Or no pointers at all ;)
    – Rakete1111
    15 hours ago










  • By the way, Class is a horrible name for a class. Could you think of something more descriptive? What is the class actually doing?
    – Juho
    13 hours ago













up vote
5
down vote

favorite
1









up vote
5
down vote

favorite
1






1





I have a program to calculate grades that uses pointers and I am trying to figure out if I did it right and if there are any problems with my implementation.



I'm trying to get a more firm grasp on pointers in C++ and have been using them more frequently in my practice programs. I was looking at my grades and thought to myself, "what if I made a program that could calculate grades just like this." Essentially the features it has are: having a Class (not a C++ class, a school class), that has categories. Categories can be things like, "Tests," and, "Classwork," which can be weighted differently. Then you can add assignments into the categories and the grades will be calculated accordingly. You can also change the categories name and weight, as well as attributes of each individual grade. I haven't fully completed the program yet and I plan to redo this code so that it is a little bit less redundant and well, bad; but the features I have seem to work, and my main concern are the pointers and if I am using them correctly/have any problems with them.



One other thing I should mention is that the reason I am using raw pointers is for learning, I know I could probably use something else like smart pointers, or unique pointers.



Class.h:



#include <map>
#include <vector>

class Class{
private:
struct grade;
struct category;

std::vector<category*> categories;
public:
void addGrade(const std::string&, const std::string&, float, float);
void addCategory(const std::string&, float);

void deleteGradeAtIndex(const std::string&, int);
void deleteGradeByName(const std::string&, const std::string&);
void deleteCategory(const std::string&);

void changeGradeAtIndex(const std::string&, int, const std::string&, float, float);
void changeGradeByName(const std::string&, const std::string&, const std::string&, float, float);
void changeCategory(const std::string&, const std::string&, float);

void printGrades();
};


Class.cpp:



#include <iostream>
#include <string>
#include "Class.h"

struct Class::category{
float weight;
float percentage;
std::string name;
std::vector<grade*> grades;
};

struct Class::grade{
float ptsEarned;
float ptsPossible;
float percentage;
std::string name;
};

void Class::addCategory(const std::string& name, float weight){
category* temp = new category;

temp->weight = weight;
temp->name = name;

categories.push_back(temp);
}

void Class::addGrade(const std::string& category, const std::string& name, float pEarn, float pPoss){
for(uint32_t i = 0; i < categories.size(); i++){
if(categories[i]->name.compare(category) == 0){
grade* temp = new grade;

temp->name = name;
temp->ptsEarned = pEarn;
temp->ptsPossible = pPoss;
temp->percentage = (pEarn/pPoss)*100;

categories[i]->grades.push_back(temp);

float pos = 0, ear = 0;

for(uint32_t j = 0; j < categories[i]->grades.size(); j++){
pos += categories[i]->grades[j]->ptsPossible;
ear += categories[i]->grades[j]->ptsEarned;
}
categories[i]->percentage = (ear/pos)*100;

return;
}
}

std::cout << "category not found" << std::endl;
}

void Class::deleteGradeAtIndex(const std::string& category, int index){
for(uint32_t i = 0; i < categories.size(); i++){
if(categories[i]->name.compare(category) == 0){
delete categories[i]->grades[index];
categories[i]->grades[index] = nullptr;

categories[i]->grades.erase(categories[i]->grades.begin() + index);
return;
}
}
}

void Class::deleteGradeByName(const std::string& category, const std::string& gradeName){
for(uint32_t i = 0; i < categories.size(); i++){
if(categories[i]->name.compare(category) == 0){
for(uint32_t j = 0; j < categories[i]->grades.size(); j++){
if(categories[i]->grades[j]->name.compare(gradeName) == 0){
delete categories[i]->grades[j];
categories[i]->grades[j] = nullptr;

categories[i]->grades.erase(categories[i]->grades.begin() + j);
return;
}
}
}
}

}
void Class::deleteCategory(const std::string& category){
for(uint32_t i = 0; i < categories.size(); i++){
if(categories[i]->name.compare(category) == 0){
for(uint32_t j = 0; j < categories[i]->grades.size(); j++){
delete categories[i]->grades[j];
categories[i]->grades[j] = nullptr;

categories[i]->grades.erase(categories[i]->grades.begin() + j);
}
delete categories[i];
categories[i] = nullptr;

categories.erase(categories.begin() + i);
return;
}
}
}

void Class::changeCategory(const std::string& category, const std::string& newName, float newWeight){
for(uint32_t i = 0; i < categories.size(); i++){
if(categories[i]->name.compare(category) == 0){
categories[i]->name = newName;
categories[i]->weight = newWeight;
return;
}
}
}

void Class::changeGradeAtIndex(const std::string& category, int index, const std::string& newName, float pEarn, float pPoss){
for(uint32_t i = 0; i < categories.size(); i++){
if(categories[i]->name.compare(category) == 0){
categories[i]->grades[index]->name = newName;
categories[i]->grades[index]->ptsEarned = pEarn;
categories[i]->grades[index]->ptsPossible = pPoss;
categories[i]->grades[index]->percentage = (pEarn/pPoss)*100;

float pos = 0, ear = 0;

for(uint32_t j = 0; j < categories[i]->grades.size(); j++){
pos += categories[i]->grades[j]->ptsPossible;
ear += categories[i]->grades[j]->ptsEarned;
}
categories[i]->percentage = (ear/pos)*100;

return;
}
}
}

void Class::changeGradeByName(const std::string& category, const std::string& gradeName, const std::string& newName, float pEarn, float pPoss){
for(uint32_t i = 0; i < categories.size(); i++){
if(categories[i]->name.compare(category) == 0){
for(uint32_t j = 0; j < categories[i]->grades.size(); j++){
if(categories[i]->grades[j]->name.compare(gradeName) == 0){
categories[i]->grades[j]->name = newName;
categories[i]->grades[j]->ptsEarned = pEarn;
categories[i]->grades[j]->ptsPossible = pPoss;
categories[i]->grades[j]->percentage = (pEarn/pPoss)*100;

float pos = 0, ear = 0;

for(uint32_t j = 0; j < categories[i]->grades.size(); j++){
pos += categories[i]->grades[j]->ptsPossible;
ear += categories[i]->grades[j]->ptsEarned;
}
categories[i]->percentage = (ear/pos)*100;

return;
}
}
}
}
}

void Class::printGrades(){
for(uint32_t i = 0; i < categories.size(); i++){
std::cout << "Category: " << categories[i]->name << " | weight: " << categories[i]->weight << " | " << categories[i]->percentage << "%n";

for(uint32_t j = 0; j < categories[i]->grades.size(); j++){
std::cout << "tassignment: " << categories[i]->grades[j]->name
<< " | points earned: " << categories[i]->grades[j]->ptsEarned
<< " | points possible: " << categories[i]->grades[j]->ptsPossible
<< " | " << categories[i]->grades[j]->percentage << "%" << std::endl;
}
}
}


main.cpp:



int main()
{
Class math;
math.addCategory("tests", 70);
math.addGrade("tests", "test1", 95, 100);
math.addGrade("tests", "test2", 80, 100);
math.addCategory("class work", 30);
math.addGrade("class work", "assignment1", 100, 100);
math.addGrade("class work", "assignment2", 100, 100);

math.deleteGradeByName("class work", "assignment1");

math.changeGradeAtIndex("tests", 0, "test1c", 93, 100);
math.changeGradeByName("tests", "test2", "test2c", 95, 100);

math.printGrades();
}









share|improve this question









New contributor




KevinCMD is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











I have a program to calculate grades that uses pointers and I am trying to figure out if I did it right and if there are any problems with my implementation.



I'm trying to get a more firm grasp on pointers in C++ and have been using them more frequently in my practice programs. I was looking at my grades and thought to myself, "what if I made a program that could calculate grades just like this." Essentially the features it has are: having a Class (not a C++ class, a school class), that has categories. Categories can be things like, "Tests," and, "Classwork," which can be weighted differently. Then you can add assignments into the categories and the grades will be calculated accordingly. You can also change the categories name and weight, as well as attributes of each individual grade. I haven't fully completed the program yet and I plan to redo this code so that it is a little bit less redundant and well, bad; but the features I have seem to work, and my main concern are the pointers and if I am using them correctly/have any problems with them.



One other thing I should mention is that the reason I am using raw pointers is for learning, I know I could probably use something else like smart pointers, or unique pointers.



Class.h:



#include <map>
#include <vector>

class Class{
private:
struct grade;
struct category;

std::vector<category*> categories;
public:
void addGrade(const std::string&, const std::string&, float, float);
void addCategory(const std::string&, float);

void deleteGradeAtIndex(const std::string&, int);
void deleteGradeByName(const std::string&, const std::string&);
void deleteCategory(const std::string&);

void changeGradeAtIndex(const std::string&, int, const std::string&, float, float);
void changeGradeByName(const std::string&, const std::string&, const std::string&, float, float);
void changeCategory(const std::string&, const std::string&, float);

void printGrades();
};


Class.cpp:



#include <iostream>
#include <string>
#include "Class.h"

struct Class::category{
float weight;
float percentage;
std::string name;
std::vector<grade*> grades;
};

struct Class::grade{
float ptsEarned;
float ptsPossible;
float percentage;
std::string name;
};

void Class::addCategory(const std::string& name, float weight){
category* temp = new category;

temp->weight = weight;
temp->name = name;

categories.push_back(temp);
}

void Class::addGrade(const std::string& category, const std::string& name, float pEarn, float pPoss){
for(uint32_t i = 0; i < categories.size(); i++){
if(categories[i]->name.compare(category) == 0){
grade* temp = new grade;

temp->name = name;
temp->ptsEarned = pEarn;
temp->ptsPossible = pPoss;
temp->percentage = (pEarn/pPoss)*100;

categories[i]->grades.push_back(temp);

float pos = 0, ear = 0;

for(uint32_t j = 0; j < categories[i]->grades.size(); j++){
pos += categories[i]->grades[j]->ptsPossible;
ear += categories[i]->grades[j]->ptsEarned;
}
categories[i]->percentage = (ear/pos)*100;

return;
}
}

std::cout << "category not found" << std::endl;
}

void Class::deleteGradeAtIndex(const std::string& category, int index){
for(uint32_t i = 0; i < categories.size(); i++){
if(categories[i]->name.compare(category) == 0){
delete categories[i]->grades[index];
categories[i]->grades[index] = nullptr;

categories[i]->grades.erase(categories[i]->grades.begin() + index);
return;
}
}
}

void Class::deleteGradeByName(const std::string& category, const std::string& gradeName){
for(uint32_t i = 0; i < categories.size(); i++){
if(categories[i]->name.compare(category) == 0){
for(uint32_t j = 0; j < categories[i]->grades.size(); j++){
if(categories[i]->grades[j]->name.compare(gradeName) == 0){
delete categories[i]->grades[j];
categories[i]->grades[j] = nullptr;

categories[i]->grades.erase(categories[i]->grades.begin() + j);
return;
}
}
}
}

}
void Class::deleteCategory(const std::string& category){
for(uint32_t i = 0; i < categories.size(); i++){
if(categories[i]->name.compare(category) == 0){
for(uint32_t j = 0; j < categories[i]->grades.size(); j++){
delete categories[i]->grades[j];
categories[i]->grades[j] = nullptr;

categories[i]->grades.erase(categories[i]->grades.begin() + j);
}
delete categories[i];
categories[i] = nullptr;

categories.erase(categories.begin() + i);
return;
}
}
}

void Class::changeCategory(const std::string& category, const std::string& newName, float newWeight){
for(uint32_t i = 0; i < categories.size(); i++){
if(categories[i]->name.compare(category) == 0){
categories[i]->name = newName;
categories[i]->weight = newWeight;
return;
}
}
}

void Class::changeGradeAtIndex(const std::string& category, int index, const std::string& newName, float pEarn, float pPoss){
for(uint32_t i = 0; i < categories.size(); i++){
if(categories[i]->name.compare(category) == 0){
categories[i]->grades[index]->name = newName;
categories[i]->grades[index]->ptsEarned = pEarn;
categories[i]->grades[index]->ptsPossible = pPoss;
categories[i]->grades[index]->percentage = (pEarn/pPoss)*100;

float pos = 0, ear = 0;

for(uint32_t j = 0; j < categories[i]->grades.size(); j++){
pos += categories[i]->grades[j]->ptsPossible;
ear += categories[i]->grades[j]->ptsEarned;
}
categories[i]->percentage = (ear/pos)*100;

return;
}
}
}

void Class::changeGradeByName(const std::string& category, const std::string& gradeName, const std::string& newName, float pEarn, float pPoss){
for(uint32_t i = 0; i < categories.size(); i++){
if(categories[i]->name.compare(category) == 0){
for(uint32_t j = 0; j < categories[i]->grades.size(); j++){
if(categories[i]->grades[j]->name.compare(gradeName) == 0){
categories[i]->grades[j]->name = newName;
categories[i]->grades[j]->ptsEarned = pEarn;
categories[i]->grades[j]->ptsPossible = pPoss;
categories[i]->grades[j]->percentage = (pEarn/pPoss)*100;

float pos = 0, ear = 0;

for(uint32_t j = 0; j < categories[i]->grades.size(); j++){
pos += categories[i]->grades[j]->ptsPossible;
ear += categories[i]->grades[j]->ptsEarned;
}
categories[i]->percentage = (ear/pos)*100;

return;
}
}
}
}
}

void Class::printGrades(){
for(uint32_t i = 0; i < categories.size(); i++){
std::cout << "Category: " << categories[i]->name << " | weight: " << categories[i]->weight << " | " << categories[i]->percentage << "%n";

for(uint32_t j = 0; j < categories[i]->grades.size(); j++){
std::cout << "tassignment: " << categories[i]->grades[j]->name
<< " | points earned: " << categories[i]->grades[j]->ptsEarned
<< " | points possible: " << categories[i]->grades[j]->ptsPossible
<< " | " << categories[i]->grades[j]->percentage << "%" << std::endl;
}
}
}


main.cpp:



int main()
{
Class math;
math.addCategory("tests", 70);
math.addGrade("tests", "test1", 95, 100);
math.addGrade("tests", "test2", 80, 100);
math.addCategory("class work", 30);
math.addGrade("class work", "assignment1", 100, 100);
math.addGrade("class work", "assignment2", 100, 100);

math.deleteGradeByName("class work", "assignment1");

math.changeGradeAtIndex("tests", 0, "test1c", 93, 100);
math.changeGradeByName("tests", "test2", "test2c", 95, 100);

math.printGrades();
}






c++ pointers






share|improve this question









New contributor




KevinCMD is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




KevinCMD is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 22 hours ago









200_success

127k15148411




127k15148411






New contributor




KevinCMD is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked yesterday









KevinCMD

262




262




New contributor




KevinCMD is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





KevinCMD is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






KevinCMD is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • I know I could probably use something else like smart pointers, or unique pointers. Or no pointers at all ;)
    – Rakete1111
    15 hours ago










  • By the way, Class is a horrible name for a class. Could you think of something more descriptive? What is the class actually doing?
    – Juho
    13 hours ago


















  • I know I could probably use something else like smart pointers, or unique pointers. Or no pointers at all ;)
    – Rakete1111
    15 hours ago










  • By the way, Class is a horrible name for a class. Could you think of something more descriptive? What is the class actually doing?
    – Juho
    13 hours ago
















I know I could probably use something else like smart pointers, or unique pointers. Or no pointers at all ;)
– Rakete1111
15 hours ago




I know I could probably use something else like smart pointers, or unique pointers. Or no pointers at all ;)
– Rakete1111
15 hours ago












By the way, Class is a horrible name for a class. Could you think of something more descriptive? What is the class actually doing?
– Juho
13 hours ago




By the way, Class is a horrible name for a class. Could you think of something more descriptive? What is the class actually doing?
– Juho
13 hours ago










1 Answer
1






active

oldest

votes

















up vote
4
down vote













Welcome on Code Review!



Congratulation, it's a pretty clean code, just some points:




  • There's missing the #include "Class.h" in main.cpp

  • Although a C string literal can be implicitly converted to a std::string, if you don't #include <string> in main.cpp Clang with the -pedantic flag will complain.

  • In Class::changeGradeByName you hide the previous declared j in your 3rd-nested for-loop.

  • Since the header file is what the user (or you, later) will look at first to understand your interface, a good practice is to don't omit parameter name in prototypes. It make the interface more explicit:


Compare:



void changeGradeByName(const std::string&, const std::string&, const std::string&, float, float)


Versus:



void changeGradeByName(const std::string& category, const std::string& gradeName, const std::string& newName, float pEarn, float pPoss);



  • Use meaningful names. In this latter, what mean pPoss or pEarn ?

  • You should provide constructor/destructor for your structs, it will make the code easier.


I surely miss something, but I hope others will come and review your code with a different perspective or another angle.






share|improve this answer





















  • Thanks this was really helpful, and I've been trying to work on making my code cleaner. The J in the third for loop in Class::changeGradeByName was a mistake I didn't notice, thanks for pointing that out, i didn't even know something like that would compile. I added a constructor and a destructor to my structs which also shortened it a bit. #include "Class.h" in main.cpp is there, I just accidentally left that bit off when I copied main.cpp. Also i will take note to put parameter identifiers in my header too because it does get confusing when reading the header before reading the source.
    – KevinCMD
    yesterday










  • And didn't forget to add #include <string> in main.cpp otherwise Clang will complain with the -pedantic flag. (Or 1// provide const char* overloads, or 2// use c++14 and string literal operator to defines your string, or 3// explicitly pass std::string("tests") to your functions. But I think the include is the simplest. ;))
    – Calak
    yesterday













Your Answer





StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");

StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});






KevinCMD is a new contributor. Be nice, and check out our Code of Conduct.










 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f208356%2fc-grades-calculator-using-pointers%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
4
down vote













Welcome on Code Review!



Congratulation, it's a pretty clean code, just some points:




  • There's missing the #include "Class.h" in main.cpp

  • Although a C string literal can be implicitly converted to a std::string, if you don't #include <string> in main.cpp Clang with the -pedantic flag will complain.

  • In Class::changeGradeByName you hide the previous declared j in your 3rd-nested for-loop.

  • Since the header file is what the user (or you, later) will look at first to understand your interface, a good practice is to don't omit parameter name in prototypes. It make the interface more explicit:


Compare:



void changeGradeByName(const std::string&, const std::string&, const std::string&, float, float)


Versus:



void changeGradeByName(const std::string& category, const std::string& gradeName, const std::string& newName, float pEarn, float pPoss);



  • Use meaningful names. In this latter, what mean pPoss or pEarn ?

  • You should provide constructor/destructor for your structs, it will make the code easier.


I surely miss something, but I hope others will come and review your code with a different perspective or another angle.






share|improve this answer





















  • Thanks this was really helpful, and I've been trying to work on making my code cleaner. The J in the third for loop in Class::changeGradeByName was a mistake I didn't notice, thanks for pointing that out, i didn't even know something like that would compile. I added a constructor and a destructor to my structs which also shortened it a bit. #include "Class.h" in main.cpp is there, I just accidentally left that bit off when I copied main.cpp. Also i will take note to put parameter identifiers in my header too because it does get confusing when reading the header before reading the source.
    – KevinCMD
    yesterday










  • And didn't forget to add #include <string> in main.cpp otherwise Clang will complain with the -pedantic flag. (Or 1// provide const char* overloads, or 2// use c++14 and string literal operator to defines your string, or 3// explicitly pass std::string("tests") to your functions. But I think the include is the simplest. ;))
    – Calak
    yesterday

















up vote
4
down vote













Welcome on Code Review!



Congratulation, it's a pretty clean code, just some points:




  • There's missing the #include "Class.h" in main.cpp

  • Although a C string literal can be implicitly converted to a std::string, if you don't #include <string> in main.cpp Clang with the -pedantic flag will complain.

  • In Class::changeGradeByName you hide the previous declared j in your 3rd-nested for-loop.

  • Since the header file is what the user (or you, later) will look at first to understand your interface, a good practice is to don't omit parameter name in prototypes. It make the interface more explicit:


Compare:



void changeGradeByName(const std::string&, const std::string&, const std::string&, float, float)


Versus:



void changeGradeByName(const std::string& category, const std::string& gradeName, const std::string& newName, float pEarn, float pPoss);



  • Use meaningful names. In this latter, what mean pPoss or pEarn ?

  • You should provide constructor/destructor for your structs, it will make the code easier.


I surely miss something, but I hope others will come and review your code with a different perspective or another angle.






share|improve this answer





















  • Thanks this was really helpful, and I've been trying to work on making my code cleaner. The J in the third for loop in Class::changeGradeByName was a mistake I didn't notice, thanks for pointing that out, i didn't even know something like that would compile. I added a constructor and a destructor to my structs which also shortened it a bit. #include "Class.h" in main.cpp is there, I just accidentally left that bit off when I copied main.cpp. Also i will take note to put parameter identifiers in my header too because it does get confusing when reading the header before reading the source.
    – KevinCMD
    yesterday










  • And didn't forget to add #include <string> in main.cpp otherwise Clang will complain with the -pedantic flag. (Or 1// provide const char* overloads, or 2// use c++14 and string literal operator to defines your string, or 3// explicitly pass std::string("tests") to your functions. But I think the include is the simplest. ;))
    – Calak
    yesterday















up vote
4
down vote










up vote
4
down vote









Welcome on Code Review!



Congratulation, it's a pretty clean code, just some points:




  • There's missing the #include "Class.h" in main.cpp

  • Although a C string literal can be implicitly converted to a std::string, if you don't #include <string> in main.cpp Clang with the -pedantic flag will complain.

  • In Class::changeGradeByName you hide the previous declared j in your 3rd-nested for-loop.

  • Since the header file is what the user (or you, later) will look at first to understand your interface, a good practice is to don't omit parameter name in prototypes. It make the interface more explicit:


Compare:



void changeGradeByName(const std::string&, const std::string&, const std::string&, float, float)


Versus:



void changeGradeByName(const std::string& category, const std::string& gradeName, const std::string& newName, float pEarn, float pPoss);



  • Use meaningful names. In this latter, what mean pPoss or pEarn ?

  • You should provide constructor/destructor for your structs, it will make the code easier.


I surely miss something, but I hope others will come and review your code with a different perspective or another angle.






share|improve this answer












Welcome on Code Review!



Congratulation, it's a pretty clean code, just some points:




  • There's missing the #include "Class.h" in main.cpp

  • Although a C string literal can be implicitly converted to a std::string, if you don't #include <string> in main.cpp Clang with the -pedantic flag will complain.

  • In Class::changeGradeByName you hide the previous declared j in your 3rd-nested for-loop.

  • Since the header file is what the user (or you, later) will look at first to understand your interface, a good practice is to don't omit parameter name in prototypes. It make the interface more explicit:


Compare:



void changeGradeByName(const std::string&, const std::string&, const std::string&, float, float)


Versus:



void changeGradeByName(const std::string& category, const std::string& gradeName, const std::string& newName, float pEarn, float pPoss);



  • Use meaningful names. In this latter, what mean pPoss or pEarn ?

  • You should provide constructor/destructor for your structs, it will make the code easier.


I surely miss something, but I hope others will come and review your code with a different perspective or another angle.







share|improve this answer












share|improve this answer



share|improve this answer










answered yesterday









Calak

1,939314




1,939314












  • Thanks this was really helpful, and I've been trying to work on making my code cleaner. The J in the third for loop in Class::changeGradeByName was a mistake I didn't notice, thanks for pointing that out, i didn't even know something like that would compile. I added a constructor and a destructor to my structs which also shortened it a bit. #include "Class.h" in main.cpp is there, I just accidentally left that bit off when I copied main.cpp. Also i will take note to put parameter identifiers in my header too because it does get confusing when reading the header before reading the source.
    – KevinCMD
    yesterday










  • And didn't forget to add #include <string> in main.cpp otherwise Clang will complain with the -pedantic flag. (Or 1// provide const char* overloads, or 2// use c++14 and string literal operator to defines your string, or 3// explicitly pass std::string("tests") to your functions. But I think the include is the simplest. ;))
    – Calak
    yesterday




















  • Thanks this was really helpful, and I've been trying to work on making my code cleaner. The J in the third for loop in Class::changeGradeByName was a mistake I didn't notice, thanks for pointing that out, i didn't even know something like that would compile. I added a constructor and a destructor to my structs which also shortened it a bit. #include "Class.h" in main.cpp is there, I just accidentally left that bit off when I copied main.cpp. Also i will take note to put parameter identifiers in my header too because it does get confusing when reading the header before reading the source.
    – KevinCMD
    yesterday










  • And didn't forget to add #include <string> in main.cpp otherwise Clang will complain with the -pedantic flag. (Or 1// provide const char* overloads, or 2// use c++14 and string literal operator to defines your string, or 3// explicitly pass std::string("tests") to your functions. But I think the include is the simplest. ;))
    – Calak
    yesterday


















Thanks this was really helpful, and I've been trying to work on making my code cleaner. The J in the third for loop in Class::changeGradeByName was a mistake I didn't notice, thanks for pointing that out, i didn't even know something like that would compile. I added a constructor and a destructor to my structs which also shortened it a bit. #include "Class.h" in main.cpp is there, I just accidentally left that bit off when I copied main.cpp. Also i will take note to put parameter identifiers in my header too because it does get confusing when reading the header before reading the source.
– KevinCMD
yesterday




Thanks this was really helpful, and I've been trying to work on making my code cleaner. The J in the third for loop in Class::changeGradeByName was a mistake I didn't notice, thanks for pointing that out, i didn't even know something like that would compile. I added a constructor and a destructor to my structs which also shortened it a bit. #include "Class.h" in main.cpp is there, I just accidentally left that bit off when I copied main.cpp. Also i will take note to put parameter identifiers in my header too because it does get confusing when reading the header before reading the source.
– KevinCMD
yesterday












And didn't forget to add #include <string> in main.cpp otherwise Clang will complain with the -pedantic flag. (Or 1// provide const char* overloads, or 2// use c++14 and string literal operator to defines your string, or 3// explicitly pass std::string("tests") to your functions. But I think the include is the simplest. ;))
– Calak
yesterday






And didn't forget to add #include <string> in main.cpp otherwise Clang will complain with the -pedantic flag. (Or 1// provide const char* overloads, or 2// use c++14 and string literal operator to defines your string, or 3// explicitly pass std::string("tests") to your functions. But I think the include is the simplest. ;))
– Calak
yesterday












KevinCMD is a new contributor. Be nice, and check out our Code of Conduct.










 

draft saved


draft discarded


















KevinCMD is a new contributor. Be nice, and check out our Code of Conduct.













KevinCMD is a new contributor. Be nice, and check out our Code of Conduct.












KevinCMD is a new contributor. Be nice, and check out our Code of Conduct.















 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f208356%2fc-grades-calculator-using-pointers%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

サソリ

広島県道265号伴広島線

Setup Asymptote in Texstudio