Solved

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

Posted on 2004-05-02
5
242 Views
Last Modified: 2013-11-20
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!
0
Comment
Question by:SinSual
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 10973492
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;

}

0
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 10973509
Oh, and if you don't use '#' as a delimiter, the code works with

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


//*******************************************************************
short loadArray(string name[], int test1[], int test2[], int test3[])
{
    int x = 0;
    char dummy;
    ifstream inFile;
    string first, last;
    inFile.open("student.dat", ios::in);
    if (inFile.is_open())
    {
         while (x < 7 && !inFile.eof())
         {
              inFile >> first;
              inFile >> last;
              name[x] = first + string(" ") + last;
              inFile >> test1[x];
              inFile >> test2[x];
              inFile >> test3[x];

// For testing purposes, uncomment this line:
//       cout << name[x].c_str() << " " << test1[x] << " " << test2[x] << " " << test3[x] << endl;
              x = x + 1;
         }//end while
         inFile.close();

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

    return 0;
}
0
 

Author Comment

by:SinSual
ID: 10973606
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!
0
 
LVL 86

Expert Comment

by:jkr
ID: 10973931
Have you tried the above input file format? '#' isn't really a good delimiter...
0
 

Author Comment

by:SinSual
ID: 10974361
Yes I tried it, it did the same thing
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question