Vectors of class
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Vectors of class

  1. #1
    Join Date
    Apr 2013
    Posts
    2

    Vectors of class

    im having a hard time trying to get this vector to work in the class. This is my second class of taking computer science and im really confuse can someone please help

    #include <iostream>
    #include <string>
    #include <vector>

    using namespace std;

    class Course {
    private:
    int scores;
    public:
    Course();
    Course(int s);
    int minimum();
    /* void print() {cout << scores; }
    void countA();
    void read();
    int maximum();
    int standard_deviation();
    int average(); */
    };
    Course::Course()
    {
    scores = 0;
    }
    Course:: Course(int s)
    {
    s=scores;
    }

    int Course::minimum()
    {
    int minimum= numbers[0];
    for (int i=0; i<numbers.size(); i++)
    {
    if (numbers[i] <minimum)
    {
    minimum = numbers[i];
    }
    }
    return minimum;
    }
    int main()
    {
    Course cs202;

    vector<int> numbers;
    int grade;

    cout<<"Enter set of scores for students until eof\n";
    cin >> grade;
    while (!cin.eof()) {
    numbers.push_back(grade);
    cin>>grade;
    }



    /*cs202.read();*/

    cout << "Minimum = " << cs202.minimum() << endl;
    /*cout << "Average = " << cs202.average() << endl;
    cout << "Standard Deviation = " << cs202.standard_deviation() << endl;
    cout << "Maximum = " << cs202.maximum() << endl;
    cout << "A's Count = " << cs202.countA() << endl;

    cout << "The scores again: \n";
    cs202.print();*/


    }

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,011

    Re: Vectors of class

    First, using an external global object within a class (like your vector<int> numbers within Course ) sounds like a bad design.

    Second, what problem do you have with the code you have posted?
    Victor Nijegorodov

  3. #3
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,001

    Re: Vectors of class

    When posting code, please format your code before posting and use code tags (Go Advanced, select code and click '#').

    Code:
    int Course::minimum()
    {
    int minimum = numbers[0];
    numbers is a vector that is local to the function main. Therefore it can't be accessed from class Course. If you want class Course to be able to use numbers, then numbers will need to be made global. However, a better way would be to have vector numbers as a private member of class Course and have additional Course methods to add a score to the vector etc.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center