Solved

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

Posted on 2004-05-02
5
238 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
  • 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Folder Comparison 12 54
Whole sheet autoscrub still needed 19 51
no14 challenge 14 72
Authenticate using sesu from script 7 57
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
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.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

828 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