-1

If I try to call any of my get functions in main, it returns the wrong answer. For getCourseName, the strings return empty. For getAverage, a negative number is returned. For getMaxE, a large number is returned. I have tried changing my constructor and get functions but the answer stays the same. I have also tested the different index numbers.

#include <string>
#include <iostream>
using namespace std;

class CCourse {
public:
    CCourse()
    {
        string name = "p";
        double average = 0.0;
        unsigned int maxEnrollment = 0;
    }
    CCourse(string course101, double avg, unsigned int MaxE)
    {
        string name = course101;
        double average = avg;
        unsigned int maxEnrollment = MaxE;
    }
    ~CCourse()
    {
        cout << "CCourse destructed" << endl;
    }

    void setCourseName(string course101)
    {
        string name = course101;
    }
    void setAverage(double avg)
    {
        double average = avg;
    }
    void setMaxE(unsigned int MaxE)
    {
        unsigned int maxEnrollment = MaxE;
    }

    string getCourseName()
    {
        return name;
    }
    double getAverage()
    {
        return average;
    }
    unsigned int getMaxE()
    {
        return maxEnrollment;
    }

private:
    string name;
    double average;
    unsigned int maxEnrollment;
};

const int NUM_COURSES = 10;

int main()
{
    CCourse courses[NUM_COURSES] = { { "BIT2400", 71.0, 90 },
        { "BIT1400", 52.7, 140 },
        { "ITEC2100", 85.3, 15 },
        { "BIT2000", 85.3, 15 } };
    string x = courses[0].getCourseName();
    cout << x;
}
drescherjm
  • 10,365
  • 5
  • 44
  • 64
pchong
  • 1
  • 1
  • In your constructors `double average = avg;` is a bug as well as all other local variables you declare that shadow the class member variables with the same name. – drescherjm Jul 25 '22 at 18:38
  • the indentation is more of my unfamiliarity with stack overflow... it is indented normally in my visual studio. – pchong Jul 25 '22 at 18:38
  • 1
    Both your constuctors create local variables instead of assigning to the members – UnholySheep Jul 25 '22 at 18:40
  • Also all your setters create local variables instead of assigning to the member variables – UnholySheep Jul 25 '22 at 18:40
  • To easily form a code block, copy and paste the code exactly as you have it in your IDE and put ``` on the line before and ``` on the line after your code. See the console.log example here: [https://stackoverflow.com/help/formatting](https://stackoverflow.com/help/formatting) – drescherjm Jul 25 '22 at 18:41
  • @drescherjm: That won't help w/ the indentation. – Scott Hunter Jul 25 '22 at 18:44
  • I am assuming it was properly formatted in the IDE. And somehow it got messed up while editing. I just fixed it. I used [http://format.krzaq.cc/](http://format.krzaq.cc/) to fix the reformat then created a code block. – drescherjm Jul 25 '22 at 18:46
  • @pchong `string name = course101;` -- "I want to really make sure the compiler knows about `name`, so I want to super-duper make sure it knows that `name` is a string". Sorry, but it doesn't work that way -- that `name` is completely different than the `name` member variable. – PaulMcKenzie Jul 25 '22 at 18:46
  • 1
    Mistakes like your ctors are best-evidence for why you should learn how to use [member-initialization lists](https://stackoverflow.com/questions/1711990/what-is-this-weird-colon-member-syntax-in-the-constructor). If you learning resources aren't teaching it, they're garbage and you should find alternatives. – WhozCraig Jul 25 '22 at 18:47
  • You should also enable warnings, and fix any that come from your own code. For *gcc* and *clang*, `-Wall -Wextra -pedantic` is pretty good. If you had warnings enabled, your compiler would have told you about unused variables. – hyde Jul 25 '22 at 18:50

2 Answers2

2

If you specify a type before a variable name, you're declaring a new variable.

So in

CCourse(string course101, double avg, unsigned int MaxE)
{
 string name = course101;  // doesn't do what you think!
 double average = avg;
 unsigned int maxEnrollment = MaxE;
   // now all the values you just set are destroyed.
   // Your actual member variables never got initialized.
}

void setMaxE(unsigned int MaxE)
{
    unsigned int maxEnrollment = MaxE; // this either!
}

you're creating new variables named name, average, and maxEnrollment. You've hidden the member variables with the same name.

To refer to a variable that's already declared, just use the name,

CCourse(string course101, double avg, unsigned int MaxE)
{
    name = course101;
    average = avg;
    maxEnrollment = MaxE;
}

void setMaxE(unsigned int MaxE)
{
    maxEnrollment = MaxE;
}

In a constructor, of course, you should prefer using a member initializer list instead (as suggested in the comments):

CCourse(string course101, 
        double avg, 
        unsigned int MaxE): name(course101), 
                            average(avg),
                            maxEnrollment(MaxE)
{
   // the initializers did all the work
}
Spencer
  • 1,924
  • 15
  • 27
1

Inside of your constructor,

string name = course101;

is creating & assigning to a local variable named name, and not to the instance variable w/ that name.

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
  • how do I assign it to the instance variable? – pchong Jul 25 '22 at 18:41
  • `name = course101;` instead of `string name = course101;` or use the initializer list: [https://stackoverflow.com/questions/7665021/c-member-initialization-list](https://stackoverflow.com/questions/7665021/c-member-initialization-list) – drescherjm Jul 25 '22 at 18:42
  • 1
    @pchong the same mistake in the setter functions – The Dreams Wind Jul 25 '22 at 18:54