#Looking for some help understanding enums and utlilizing enums/arrays in constructs or objects.

42 messages · Page 1 of 1 (latest)

prisma path
#

Main.cpp




int main() {
    unsigned int i;
    string id;
    string f;
    string l;
    string email;
    int age;
    int daysToComplete[3];
    string d;
    int t = 0;


    cin >> id;
    cin >> f;
    cin >> l;
    cin >> email;
    cin >> age;
    for (i = 0; i < 3; ++i) {
        cin >> daysToComplete[i];
    }
    cin >> d;

    if (d == "SECURITY") {
        t = 0;
    }
    else if (d == "NETWORK") {
        t = 1;
    }
    else {
        t = 2;
    }

    DegreeProgram deg = static_cast<DegreeProgram>(t);

    Student s1(id, f, l, email, age, daysToComplete, deg);

    s1.PrintInfo();


}
modern crownBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

prisma path
#

Student.H


#ifndef STUDENT_H
#define STUDENT_H

#include <iostream>
#include <string>
#include "degree.h"
using namespace std;


class Student {
public:
    Student();
    Student(string studentID, string firstName, string lastName, string emailAddress, int age, int daysToComplete[3], DegreeProgram degree);

    string GetStudentID();
    string GetFirstName();
    string GetLastName();
    string GetEmailAddress();
    int GetAge();
    int GetDaysToComplete();
    DegreeProgram GetDegree();

    void SetStudentID(string studentID);
    void SetFirstName(string firstName);
    void SetLastName(string lastName);
    void SetEmailAddress(string emailAddress);
    void SetAge(int age);
    void SetDaysToComplete(int daysToComplete[3]);
    void SetDegree(DegreeProgram degree);
    void PrintInfo();

private:
    string studentID;
    string firstName;
    string lastName;
    string emailAddress;
    int age;
    int daysToComplete[3];
    DegreeProgram degree;

};
#endif
#

Student.CPP



#include <iostream>
#include <string>
#include "student.h"
#include "degree.h"
using namespace std;


Student::Student() {}
Student::Student(string studentID, string firstName, string lastName, string emailAddress, int age, int daysToComplete[3], DegreeProgram degree) {
    this->studentID = studentID;
    this->firstName = firstName;
    this->lastName = lastName;
    this->emailAddress = emailAddress;
    this->age = age;
    this->daysToComplete[3] = daysToComplete[3];
    this->degree = degree;
}

void Student::SetStudentID(string studentID) {
    this->studentID = studentID;
}
void Student::SetFirstName(string firstName) {
    this->firstName = firstName;
}
void Student::SetLastName(string lastName) {
    this->lastName = lastName;
}
void Student::SetEmailAddress(string emailAddress) {
    this->emailAddress = emailAddress;
}
void Student::SetAge(int age) {
    this->age = age;
}
void Student::SetDaysToComplete(int daysToComplete[3]) {
    this->daysToComplete[3] = daysToComplete[3];
}
void Student::SetDegree(DegreeProgram degree) {
    this->degree = degree;
} 
void Student::PrintInfo() {
    cout << studentID << "\t";
    cout << "First Name: " << firstName << "\t";
    cout << "Last Name: " << lastName << "\t";
    cout << "Email: " << emailAddress << "\t";
    cout << "Age: " << age << "\t";
    cout << "daysInCourse: " << "{" << daysToComplete[0] << ", " << daysToComplete[1] << ", " << daysToComplete[2] << "}";
    switch (degree) {
    case 0: cout << "DegreeProgram: SECURITY";
    case 1: cout << "DegreeProgram: NETWORK";
    case 2: cout << "DegreeProgram: SOFTWARE";
    }

}

string Student::GetStudentID() {return studentID;}
string Student::GetFirstName() {return firstName;}
string Student::GetLastName() { return lastName; }
string Student::GetEmailAddress() { return emailAddress; }
int Student::GetAge() { return age; }
int Student::GetDaysToComplete() { return daysToComplete[3]; }
DegreeProgram Student::GetDegree() { return degree; }
#

Degree.h


#ifndef DEGREE_ENUM_H
#define DEGREE_ENUM_H
#include <map>
enum DegreeProgram { SECURITY, NETWORK, SOFTWARE };


#endif

#

Sorry for the messy Main.CPP file I have been struggling on 2 parts

  1. I need to utilize a seperate degree file with an enum to store the three degrees.
  2. I need to specifically use an array of integers to store 3 data values.
#

What am I doing incorrectly that is causing 35, 40 and 55 to return 85342942

#

additionally how do I take a string and point it to the enum, maybe I am not utilizing the enum correctly?

storm marten
#

Do you see anything wrong with it?

#

There is another issue as well that could be solved by using std::array instead of a C-style array

prisma path
#

The thing that stands out to me is passing the value of the size of the array into the setter function

storm marten
#

I meant more with the body of the function

prisma path
#

Sorry I am newish to c++ if I don't catch on quickly. I do appreciate the help though.

#

So just working through the function, void seems to be correct because we are not returning a value

storm marten
#

No worries, I just want to help you focus on the right part and help you along to solving it

prisma path
#

ohh wait ok I see

#

so I am esentially setting the 3rd index = to the 3rd index and that is it

storm marten
#

Right but there is no third index

prisma path
#

oh holy crap

#

its just 0 1 and 2

#

Whats funny is I even wrote it right here

storm marten
#

Do you remember learning of a way to easily assign values to an array

prisma path
#

Yeah like array[0] = 55

storm marten
#

You could manually do it, but you’ll have to do like

this->daysToComplete[0] = daysToComplete[0];

this->daysToComplete[1] = daysToComplete[1];

#

Imagine you had 100 or a thousand stuff in the array

#

It would take forever

#

Hint:
|| It’s a type of loop ||

prisma path
#

Ok without looking at the hint, my guess would be


for (i = 0; i < 2; ++i){
  cin >> userInput
  daysToComplete[i] = userInput

}
storm marten
#

i < 2

prisma path
#

Oh yeah it would have to be 3

storm marten
#

How many times you think this will run?

#

Yea

prisma path
#

jfc I just did multiple leet code challenges using these and I got comfortable with them idk why I didn think to use it here.

storm marten
#

If you made projects while using these topics you learned they would have stuck a lot more. Leetcode is pretty bad for a lot of reasons

prisma path
#

Awesome that worked perfectly!

storm marten
#

Nice!

prisma path
#

Thanks again for the help, much appreciated.

modern crownBOT
#

@prisma path Has your question been resolved? If so, type !solved :)

storm marten
#

Np

prisma path
#

!solved