Hi all - hope your weekend is going well.

I've got the following code. It compiles, but gives a warning about a stack overflow error in the istream& operator function. When running the program, it just continuously outputs "Enter ID for student."

What am I missing that makes this recursive? Any suggestions or pointers will be much appreciated!
I hope I got the syntax tags right....

#include<iostream>
#include<string>
using namespace std;
class Student
{
   friend ostream& operator<<(ostream&, const Student&);
   friend istream& operator>>(istream&, const Student&);
   private:
      int idNum;
      double gpa;
   public:
       Student(const int = 0, const double = 0.0);
       void setIdTo9s();
       void setGpaToZero();
 };

Student::Student(const int id, const double gp)
{
  idNum=id;
  gpa = gp;
}
void Student::setIdTo9s()
{
   idNum = 9999;
}
void Student::setGpaToZero()
{
   gpa = 0;
}

ostream& operator<<(ostream &out, const Student &stu)
{
     out<<out<<"Student ID #"<<out<<stu.idNum<<
	 " gpa is "<<stu.gpa<<endl;
     return out;
}
istream& operator>>(istream &in, const Student &stu)
{
  const int FOUR_DIGITS = 9999;
  const double MAX_GPA = 4.0;
  cout<<"Enter ID number for student ";
  in>>stu.idNum;
  cout<<"Enter gpa ";
  in>>stu.gpa;
  if(stu.idNum < 0 || stu.idNum > FOUR_DIGITS)
    throw("ID number out of range!");
  if(stu.gpa > MAX_GPA)
     throw(stu.gpa);
  return in;
}

int main()
{
  const int NUM = 5;
  Student stus[NUM];
  int x;
  for(x = 0; x < NUM; x++)
  {
    try
    {
       cin>>stus[NUM];
    }
    catch(const char *msg)
    {
      cout<<msg<<endl;
      stus[x].setIdTo9s();
      stus[x].setGpaToZero();
    }
    catch(const double grade)
    {
      cout<<"The gpa "<<grade<<" is too high"<<endl;
      stus[x].setIdTo9s();
      stus[x].setGpaToZero();
    }
 }
 cout<<endl<<"Student summary:"<<endl;
 for(x = 0; x < NUM; x++)
    cout<<stus[x]<<endl;
}

And get rid of the excess "out<<" in your ostream method. It'll run as is, but give odd results.

I caught that error a couple of minutes before your post. Thanks for the help. However, I still get the warning that the overload operator is recursive on all control paths, and the function will cause stack overflow.

It is probably something simple I am missing, but since the lines are starting to look like they are written in Swahili....I'm missing something.

And get rid of the excess "out<<" in your ostream method. It'll run as is, but give odd results.

I did...same issue though.

I fixed it....

class Student
{
   friend ostream& operator<<(ostream&, const Student&);
   friend istream& operator>>(istream&, Student&);
   // more code .......
};

and here:

istream& operator >> (istream &in, Student &stu)
{
      // code from earlier post .....
}

I think I have a fundamental understanding of what was causing the error, but beings it is late and I'm a bit fustrated - a professional explanation would be appreciated.

In my head, the problem cause by const in the overloaded operator declaration is that it is not a constant. There's 5 students (due to array in main) to capture data for.

Am I on the right track? Trying to learn from my mistakes :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.