Home > Back-end >  How to get my variables inside the scope?
How to get my variables inside the scope?

Time:03-23

I recently started learning C , and was doing one of the exercises where the question was as follows:

Create a class “Student” with public data member as: marks; and member function as: calculateAverage (float m1, float m2) which is defined outside the class and its return type is float. Use constructor to initialize the marks of students. Calculate the average marks of two students and display the average marks.
Input:
Marks 1: 88.0, Marks 2: 56.0
Output:
Average marks: 72.0

This is my program:

#include<iostream>
using namespace std;

class student
{
public:
  float marks1, marks2; float marks;

  student()
  {
      cout<<"Marks 1:"<<endl;
      cin>>marks1;
      cout<<"Marks 2:"<<endl;
      cin>>marks2;
  }

   void calculateAverage (float m1,float m2);
};

void student :: calculateAverage(float m1,float m2)
{
    m1 = marks1;
    m2 = marks2;
    float avg = (marks1   marks2)/2;
    cout<<"Average is "<<avg<<endl;
}

int main()
{
    student s1;
    s1.calculateAverage(marks1,marks2);
    return 0;
}

I am getting this error:

error: ‘marks1’ was not declared in this scope
28 |     s1.calculateAverage(marks1,marks2);
|                         ^\~\~\~\~\~
main.cpp:28:32: error: ‘marks2’ was not declared in this scope
28 |     s1.calculateAverage(marks1,marks2);
|                                ^\~\~\~\~\~

Please help me out

I was trying to use the constructor to initialize the function and then call it in my main function.

CodePudding user response:

honestly that exercise is terribly described. I have no idea what you are supposed to do. But the reason why your code is giving those errors is because you do

int main()
{
    student s1;
    s1.calculateAverage(marks1,marks2);
    return 0;
}

what does marks1 mean? The compiler has no idea.

The simple solution is

int main()
{
    student s1;
    s1.calculateAverage(88.0,56.0);
    return 0;
}

as per the instructions. But the student class was not involved at all. A very very bad excercise, get a better book

CodePudding user response:

The description of calculateAverage() is a bit misleading for this exercise. It doesn't make sense for it to be a method of the class, unless it is a static method. But the description does not say that, and as a non-static method, the input parameters described don't make sense for it.

But, despite the restrictions outlined, there are other aspects of the instructions that you are simply not following, which makes the process harder on you. A big part of being a developer is following written requirements:

  • you were told to give the Student class 1 data member. You gave it 3 data members.

  • you were told to give calculateAverage() a return value of float, but you gave it void instead.

  • you were told to average 2 students, but you created only 1 student.

With that said, try something more like this:

#include <iostream>
using namespace std;

class Student
{
public:
    float marks;

    Student(float m)
    {
        marks = m;
    }

    float calculateAverage (float m1, float m2);
};

float Student::calculateAverage(float m1, float m2)
{
    return (m1   m2) / 2;
}

int main()
{
    float marks1, marks2;

    cout << "Marks 1:" << endl;
    cin >> marks1;
    cout << "Marks 2:" << endl;
    cin >> marks2;

    Student s1(marks1), s2(marks2);

    float avg = s1.calculateAverage(s1.marks, s2.marks);
    cout << "Average is " << avg << endl; 

    return 0;
}

That being said, an alternative implementation would look like this:

#include <iostream>
using namespace std;

class Student
{
private:
    float marks;

public:
    Student(float m)
    {
        marks = m;
    }

    static float calculateAverage (const student &s1, const student &s2);
};

float Student::calculateAverage(const student &s1, const student &s2)
{
    return (s1.marks   s2.marks) / 2;
}

int main()
{
    float marks1, marks2;

    cout << "Marks 1:" << endl;
    cin >> marks1;
    cout << "Marks 2:" << endl;
    cin >> marks2;

    Student s1(marks1), s2(marks2);

    float avg = Student::calculateAverage(s1, s2);
    cout << "Average is " << avg << endl; 

    return 0;
}
  • Related