Solved

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

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
repeatEnd java challenge 42 83
Turn a spreadsheet into a vba executable. 2 66
Expand data scrubbing tool 13 31
only14 challenge 19 63
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

705 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now