Click here to Skip to main content
15,559,455 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
#include <iostream>
#include <string.h>
/*me thn xrhsh constructor emfanizw to onoma to am ton meso oro ton megisto kai ton elaxisto*/



using namespace std;

class Student
{
	private:
		string name;
		string am;
		int marks[50],avg,max,min, i;
	public:
		Student(string name, string am,  int avg, int max, int min):name(name), am(am), avg(avg), max(max), min(min)
		{
			
		 } 
  	string get_name() { return name; }
  	string get_am() { return am; }
	int get_avg() { return avg; }
	int get_max() { return max; }
	int get_min() { return min; }		
};

int main()
{
	  	string name, am;
  		int marks, avg, max, min, sum=0, mx, m, i;
  		
  			cout<<"Enter Name:";
			cin >>name;

			cout<<"Enter Am:";
			cin >>am;

			cout<<"Enter 5 marks:\n";
			min=marks[i];
			min=marks[i];
			for(i=0;i<5;i++)
			{
				do{
					cout<<"Give "<<i+1<<"o: ";
					cin>>marks[i];
				}while(marks[i]<0 || marks[i]>10);
				
				sum=sum+marks[i];
				
				if(mx<marks[i])
				{
					mx=marks[i];
				}
				if(m>marks[i])
				{
					m=marks[i];
				}													
			}
			
			avg=sum/5;
			max=mx;
			min=m;
			
			Student s(name, am, avg, max, min);
			
			cout<< s.get_name() << s.get_am() << s.get_avg() << s.get_max() << s.get_min() ;
  		
  		
}


What I have tried:

i dont know what to do "
[Error] invalid types 'int[int]' for array subscript
"
Posted
Updated 8-Jun-21 21:23pm
Comments
Joe Woodbury 8-Jun-21 23:34pm    
marks in main() is not an array.
i is undefined before the for loop; if marks was an array it too would be undefined.
mx and m are undefined.
Why have both max and mx and min and m?
Also note that in C++, you should define variables as close as possible to where they are used.
I also suggest gathering the marks, passing them to Student and have Student do the calculation (separating the interface from the rest.)

When you write a function, any variables you create locally "hide" those outside the function with the same name:
C++
int x = 666;
void foo()
   {
   cout << x << endl;
   }
void bar()
   {
   int x = 333;
   cout << x << endl;
   }
void main()
   {
   int x = 111;
   foo();
   bar();
   }
Will print
C++
666
333
because the bar function hides the external variable x, and so does the main function.

So your main function declares a new local variable called marks :
C++
int main()
{
	  	string name, am;
  		int marks, avg, max, min, sum=0, mx, m, i;
which "hides" the external one. All references to marks inside the main function will refer to the local version which is not an array.

Worse, you then start trying to use the data in it without actually filling it with anything so even if you remove the local definition, your code still won't work properly!

I think you need to sit down and think carefully about what you are trying to do before you leap into code - that shows all the hallmarks of "diving straight in and coding" before working out what you need to do!

Have a look here, it may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
 
Share this answer
 
You must understand the scope rules in C. Read this fine scope tutorial to fully understand the "hiding" effects.
 
Share this answer
 
Your code is using marks like an array, but you have declared it (single) int.
Do you realize you don't need an array at all?
Try
C++
class Student
{
  private:
    string name;
    string am;
    double avg;
    int max, min;
  public:
    Student(string name, string am,  double avg, int max, int min) : name(name), am(am), avg(avg), max(max), min(min)
    {
    }
    string get_name() { return name; }
    string get_am() { return am; }
    double get_avg() { return avg; }
    int get_max() { return max; }
    int get_min() { return min; }
};

int main()
{
  const int Items = 5;
  string name, am;
  int max, min, sum;
  double avg;

  cout<<"Enter Name:";
  cin >>name;

  cout<<"Enter Am:";
  cin >>am;

  cout<<"Enter " << Items << " marks:\n";
  sum = 0;
  min = 11;
  max = -1;

  for( int i=0; i<Items; i++ )
  {
    int marks;
    do
    {
      cout<<"Give "<<i+1<<"o: ";
      cin>>marks;
    } while ( marks < 0 || marks > 10);

    sum += marks;

    if ( max < marks)
      max = marks;

    if ( min > marks)
      min = marks;
  }
  avg = (double) sum / Items;

  Student st(name, am, avg, max, min);

  cout << st.get_name() << ", " <<  st.get_am() << ", " << st.get_avg() << ", " << st.get_max() << ", " <<  st.get_min() << endl;

}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900