#Expected type specifier error

47 messages · Page 1 of 1 (latest)

wide spire
#
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

class Student {
public:
    vector<int> scores(5,0); // Initialize with 5 elements

    void input() {
        int val;
        for (int i = 0; i < 5; i++) {
            cin >> val;
            scores.push_back(val);

        }
    }

    int calculateTotalScore()  {
        int sum = 0;
        for (int score : scores) {
            sum += score;
        }
        return sum;
    }
};
int main() {
    int n; // number of students
    cin >> n;
    Student* s = new Student[n]; // an array of n students

    for (int i = 0; i < n; i++) {
        s[i].input();
    }

    // calculate kristen's score
    int kristen_score = s[0].calculateTotalScore();

    // determine how many students scored higher than kristen
    int count = 0;
    for (int i = 1; i < n; i++) {
        int total = s[i].calculateTotalScore();
        if (total > kristen_score) {
            count++;
        }
    }

    // print result
    cout << count;

    return 0;
}

Hi, I am trying to solve this problem from hackerrank : https://www.hackerrank.com/challenges/classes-objects/problem?isFullScreen=true

I am getting an error at line 10 , when I try to create a vector in this way: vector<int> scores(5,0); Kindly help me to fix the issue.

sharp dragonBOT
#

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.

native pollen
#

Default initializers for class members can't use (...), you must use = ... or {...}

#

In your case, vector<int> scores = vector<int>(5);

sharp night
#

By doing vector scores( it's seeing this as a method declaration, so it gets confused when the arguments are just numbers

native pollen
#

...But since you're using push_back, you don't need to set the vector size to before that, you can keep it as vector<int> scores; (empty)

#

Also if you're aware of vectors, why do you use new in main sadge

wide spire
wide spire
thin lily
#
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <cassert>
using namespace std;

// Write your Student class here

class Student {
public:
    vector<int> scores; // Initialize with 5 elements

    void input() {
        int val = 0;
        for (int i = 0; i < 5; i++) {
            cin >> val;
            scores.push_back(val);

        }
    }

    int calculateTotalScore()  {
        int sum = 0;
        for (int score : scores) {
            sum += score;
        }
        return sum;
    }
};

int main() {
    int n; // number of students
    cin >> n;
    Student *s = new Student[n]; // an array of n students
    
    for(int i = 0; i < n; i++){
        s[i].input();
    }

    // calculate kristen's score
    int kristen_score = s[0].calculateTotalScore();

    // determine how many students scored higher than kristen
    int count = 0; 
    for(int i = 1; i < n; i++){
        int total = s[i].calculateTotalScore();
        if(total > kristen_score){
            count++;
        }
    }

    // print result
    cout << count;
    
    return 0;
}
thin lily
formal heathBOT
#
Program Output
1
wide spire
thin lily
wide spire
#

there is a vector names scores but there are not methods named scores. zero pointer said "By doing vector scores( it's seeing this as a method declaration, so it gets confused when the arguments are just numbers"

thin lily
#

u cant define vector element size in a class declaration

wide spire
#

okay.

#

Why is that?

wide spire
thin lily
wide spire
#
#include <iostream>
#include <vector>
using namespace std;
class Student {
public:
    void test() {
        vector<int> scores(5);

        for (int score : scores)
        {
            cout << score << endl;
        }

        for (int i = 0; i < 5; i++)
        {
            cin >> scores[i];
        }


        for (int score : scores)
        {
            cout << score << endl;
        }
    }
};
int main()
{
    Student s1;
    s1.test();
    
}

#

This code didn't throw any errors

#

Why it didn't throw any errors here but threw error in the first one?

wide spire
neat lagoon
wide spire
thin lily
wide spire
wide spire
neat lagoon
#

that's irrelevant. the thing is type name() is interpreted as function declaration

#

you'll need either type name = something or type name{}

wide spire
#

This language is crazy

thin lily
#

lol

#

not realy wen u start reading in english with some math

#

think like c/c++ compiler very dummy, u need explain (declare / define / foward) all to it xd

#

also intelisense tell u about some sintax errors 😄

wide spire
#

It didn't give any meaningful error message. ( Or at least I don't have the knowledge right now to understand what it's saying )

#

!solved

sharp dragonBOT
#

Thank you and let us know if you have any more questions!

This thread is now set to auto-hide after an hour of inactivity

sharp night
wide spire
native pollen
#

Because you have ( after the name