Classes and Objects

Sort by

recency

|

414 Discussions

|

  • + 0 comments
    class Student
    {
    
    private: 
        vector<int> scores; 
    
    public: 
        void input()
        {
            int a, b, c, d, e; 
            cin >> a >> b >> c >> d >> e; 
            scores = { a, b, c, d ,e }; 
        };
        int calculateTotalScore()
        {
            return accumulate(scores.begin(), scores.end(), 0);
        }; 
    
    };
    
  • + 0 comments
    1. include

      using namespace std; class Student{ private: int q1,q2,q3,q4,q5; public: void input(){ cin >> q1 >> q2 >> q3 >> q4 >> q5; } int calculateTotalScore(){ int score; score += q1+q2+q3+q4+q5; return score; }}; int main() { int n; // number of students cin >> n; Student *s = new Student[n]; for(int i = 0; i < n; i++){ s[i].input(); } int kristen_score = s[0].calculateTotalScore(); int count = 0; for(int i = 1; i < n; i++){ int total = s[i].calculateTotalScore(); if(total > kristen_score){ count++; } } cout << count; return 0; }
  • + 0 comments

    Mine wors in my compiler, but when I put the exact same code in the Hackerank compiler, it gives me the wrong answer. Very bizarre:

    class Student
    {
        std::vector<int> scores{};
    
    public:
        void input()
        {
            // If std::cin buffer has '\n', then clear the buffer
            if (std::cin.peek() == '\n') {
                std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
            }
    
            std::string str;
    
            //read input and store it in a string "str", note that this includes the newline character '\n';
            std::getline(std::cin, str); //a way to remove the newline from the string
    
            std::stringstream iss( str );
    
            //convert the numbers on the string to an integers and store it as elements of vector str_int
            int score;
            while (iss >> score)
            {
                scores.emplace_back(score);
            }
        }
    
        int calculateTotalScore()
        {
            int total{0};
            for (const auto &num: scores)
            {
                total += num;
            }
            return total;
        }
    };
    
  • + 0 comments

    No locked code int C++20

    That i lost time implementing the parsing and the reading. Fix that shit

  • + 1 comment
    // Write your Student class here
    class Student {
    private:
        vector<int> scores;
    
    public:
        void input() {
            for (int i = 0; i < 5; i++) {
                int score;
                cin >> score;
                scores.push_back(score);
            }
        }
    
        int calculateTotalScore() {
            int total = 0;
            for (int i = 0; i < 5; i++) {
                total += scores[i];
            }
            return total;
        }
    };