Link to home
Start Free TrialLog in
Avatar of SinSual
SinSual

asked on

Loading file into arrays....Functions not working.....Is file opening a loading correctly?

After fixing the errors and executing this program, it appears that the searchFor and displayClass functions arent working properly.  I have checked them and as far as I cant tell they are written correctly.  I am wondering if the Student.dat file is opening and loading correctly, when I search for a name that is in the file I get the "Student name was not found" message.  How can I tell if the file is opening and loading corectly?
The student.dat file has 7 students and thier grades listed in it as follows
Lisa Burden#100#80#50
Lucy Carter#100#95#90
Brad Emmons#80#70#75
Tom Hoover#60#50#20
Carry Johnson#75#80#90
Ron Love#100#100#100
Russell Snyder#70#60#30

 
//displays and averages students grades

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

//function prototype
short loadArray(string [], int [], int [], int []);
char getSelection ();
void displayClass(string [], int [], int [], int []);
void searchFor (string [], int [], int [], int []);

void main()
{
     char selection = ' ';



     //declare arrays
     string name[7] = {""};
     int test1[7] = {0};
     int test2[7] = {0};
     int test3[7] = {0};

     selection = getSelection();
     while (selection != '0')
     {
          switch (selection)
          {
          case '1': displayClass(name, test1, test2, test3);
               break;
          case '2': searchFor (name, test1, test2, test3);
               break;
          default: cout << "Invalid selection" << endl;
          }
          selection = getSelection();
     
     }
     cout << "Program has ended, thank you, and goodbye!" << endl;

}

//*******************************************************************
short loadArray(string name[], int test1[], int test2[], int test3[])
{
     int x = 0;
     ifstream inFile;
     inFile.open("student.dat", ios::in);
     if (inFile.is_open())
     {
          while (x < 7 && !inFile.eof())
          {
               inFile >> name[x];
               inFile.ignore();
               getline(inFile, name[x], '#');
               inFile >> test1[x];
               inFile >> test2[x];
               inFile >> test3[x];
               x = x + 1;
          }//end while
          inFile.close();

     }else
          cout << "Error in opening file." << endl;

     return 0;
}

//********************************************************************
char getSelection()
{
     char selection = ' ';
     system ("cls");
     cout <<"                         Student Menu                      " << endl;
     cout <<"1. Display class names, grades, and averages" << endl;
     cout <<"2. Search by student name" << endl;
     cout <<"0 to Exit" << endl;
     cin >> selection;
     cin.ignore();
     return selection;
} // end of getSelection function

//*********************************************************************
void displayClass(string name[], int test1[], int test2[], int test3[])
{
     int x = 0;
     float average = 0.0;

     average = (test1 + test2 + test3) / 3;

     while (x < 7)
     {
          cout << name[x] << "\t" << test1[x] << "\t" << test2[x] << "\t" <<test3[x] << "\t" << average << endl;
          x = x + 1;
     }//end while
     
} //end of displayClass function

//**********************************************************************
void searchFor(string name[], int test1[], int test2[], int test3[])
{
     float average = 0.0;
     string studentName = " ";
         
     cout << "Enter the student name. (1 to return to menu): ";
     getline(cin,studentName);

     while (studentName != "1" )
     {
          int x = 0;
          while (x < 7 && name[x] != studentName)
               x = x + 1;
          //end while

          if (x < 7)
          {
               average = (test1 + test2 + test3) / 3;
               cout << name[x] << "\t" << test1[x] << "\t" << test2[x] << "\t" <<test3[x] << "\t" << average << endl;
          }
          else
               cout << "Student name was not found" << endl;
          // end if
     }
     if (studentName == "1")
     cout << "Press Enter to return to the Menu";
     getSelection();

     
}// end searchFor function

I appreciate your help!
Avatar of jkr
jkr
Flag of Germany image

You are not calling 'loadArray()' in 'main()' - make that read

void main()
{
    char selection = ' ';



    //declare arrays
    string name[7] = {""};
    int test1[7] = {0};
    int test2[7] = {0};
    int test3[7] = {0};

    loadArray ( name, test1, test2, test3);

    selection = getSelection();
    while (selection != '0')
    {
         switch (selection)
         {
         case '1': displayClass(name, test1, test2, test3);
              break;
         case '2': searchFor (name, test1, test2, test3);
              break;
         default: cout << "Invalid selection" << endl;
         }
         selection = getSelection();
   
     }
    cout << "Program has ended, thank you, and goodbye!" << endl;

}

ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of SinSual
SinSual

ASKER

Thanks for your help, I have made the necessary changes but I am still having the same problem.  When I execute the program and choose number 1 to show the class list it flashes on the screen for a split second but it only has the first student on the list the rest are 0's.  And when I search for a name I am getting the error msg "student name not found" and it is running in a continuos loop.  I'm not sure what the problem is but I'll keep at it!  Thank you for your help!
Have you tried the above input file format? '#' isn't really a good delimiter...
Avatar of SinSual

ASKER

Yes I tried it, it did the same thing