Solved

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

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying 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

Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
In this post we will learn different types of Android Layout and some basics of an Android App.
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.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

636 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