Hey everyone :)

So I'm doing my homework when I get this massive brainfart. It's all done, and everything works, except I need a way to make j [in the function AddStudents()] get bigger without resetting to 0 everytime the function is called (so it moves to the next cell in the array). But I can't figure it out O.o Any ideas?

p.s. I would put it in a for loop but the homework says it should only be called once and then the user should have the option to add another student.

#include <iostream>
using namespace std;

class students {   
      private:
       char name[15];
       int studentID;
       float gpa;
       int classes;
      public:
       void AddStudent();
       void FindStudent();

};

typedef class students STUDENTS;

STUDENTS student[10];

void students::AddStudent()
{
     int j=0;   
     char holdID[10];
     char holdgpa[5];
     char holdclass[5];
     
     cout << "Enter the following information:  \n"; 
     cout << "Name:  ";
     cin >> student[j].name;
     cout << "Student ID:  ";
     cin >> holdID;
     student[j].studentID = atoi (holdID);     
     cout << "GPA:  ";
     cin >> holdgpa;
     student[j].gpa = atof (holdgpa);
     cout << "Classes:  ";
     cin >> holdclass;
     student[j].classes = atoi (holdclass);
     //j++;
}
void students::FindStudent(){
     int ID;  
     cout << "\nWhat is the Students ID number?  ";
     cin >> ID;
     
     for(int i =0; i < amount; i++)
             if (ID == student[i].studentID){
                    cout << "Name:  " << student[i].name << "\n";
                    cout << "Student ID:  " <<student[i].studentID << "\n";
                    cout << "GPA:  " << student[i].gpa << "\n";
                    cout << "Classes:  " <<student[i].classes << "\n";
}
}        
int main()
{
	int add, answer;
	students go;
    cout << "Would you like to add a student? 0 yes, 1 no:  ";
    cin >> add;
    
    if(add == 0){
           go.AddStudent();
           do {
                 cout << "Would you like to add a student? 0 find, 1 quit:  ";
                 cin >> add;
                 
                 if(add == 0) {
                      go.AddStudent();
                                   }
                         } while (add == 0);
                     
                                     
           cout << "Would you like to find a student? 0 find, 1 quit:  ";
           cin >> answer;
           if(answer == 0){
                     go.FindStudent();
                     do {
                         cout << "Would you like to find a student? 0 find, 1 quit:  ";
                         cin >> answer;
                         
                         if(answer == 0) {
                                   go.FindStudent();
                                   }
                         } while (answer == 0);
                     }
}
           
    system("pause");
    return 0;
    
}

If j needs to persist between method calls, it should probably be a private data member instead of a local variable.

Take it out of students::AddStudent and add it as a static data member of class students.

BTW line 16 is a waste of time, you may as well just change line 4 to class STUDENTS {

Thanks!! :) I figured it out :D

@Banfa, I tried doing something like that earlier and I got a compiler error, but it works now :)

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.