GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 07-Dec-2007, 21:30
ShadowTH ShadowTH is offline
New Member
 
Join Date: Dec 2007
Posts: 1
ShadowTH is on a distinguished road

Employee program segmentation error.


Hi, I'm having trouble with this C++ homework assignment. The program is supposed to read from three files. The first file contains the employee information to save in a struct (first, last name, ID number, job status,pay rate, hours worked). The first part adds the information into a struct array, and prints it out alphabetically (this part works). The second part is supposed read a file with the ID number of the employee and changes to the employee's information. There are two command codes S (job status) and P (payrate) (I get a segmentation fault when I return the search), and the 3rd part is supposed to calculate the hours worked (I haven't gotten there yet).

Here is the second part of my program
working is the employee array, n is the number of employees added to the array.
CPP / C++ / C Code:
void commandcode (employees working[], int n)
{
  ifstream infile2;
  string fname2;
  int idnum; //id number
  char ccode; //command code
  double payr; //pay rate
  char jobst; // job status
  int i;

  cout << "Enter name of second data file" << endl;
  cin >> fname2;
  infile2.open(fname2.c_str());

  infile2 >> idnum;
  infile2 >> ccode;
  while (!infile2.eof())
    {
      search (working, n, idnum, i);
      if (i == -1)
        {
          cout << "ID# " << idnum << "is invalid" << endl;
          infile2.ignore(80, '\n');
        }
      if (ccode == 'P')
        {
          infile2 >> payr;
          working[i].hoursr = payr;
          cout << "ID# " << working[i].id << " hourly pay rate changed to " << working[i].hoursr << endl;
        }
      if (ccode == 'S')
        {
          infile2 >> jobst;
          working[i].jobs = jobst;
          cout << "ID# " << working[i].id << " status changed to " << working[i].jobs << endl;
        }
      if (ccode != 'S' && ccode != 'P')
        {
          cout << "Incorrect command letter used: " << ccode << endl;
          infile2.ignore(80, '\n');
        }

    }
  infile2 >> idnum;
  infile2 >> ccode;
}


int search (employees working[], int n, int idnum, int i)
{
  for (i = 0; i < n; i++)
    {
      if(idnum == working[i].id)
        {
          cout << i;
          return i;
        }
    }
  i = -1;
  return i;
}

Thanks for the help.
I have to turn it in midnight PST time zone.
  #2  
Old 08-Dec-2007, 14:28
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,217
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Employee program segmentation error.


CPP / C++ / C Code:
.
.
.
    int i; // i is unitialized, so its value is undefined
.
.
.
    search (working, n, idnum, i); // the value of i is still undefined after this
.
.
.
    working[i].hoursr = payr;     // Addressing what element? undefined behavior
.
.
.
    working[i].jobs = jobst;      // Addressing what element? undefined behavior
.

int search(employees working[], int n, int idnum, int i) // <--- i is passed by value
{
        //Nothing in this function will affect the value if i back in the calling function
}

Segmentation faults are typically caused by attempting to access memory that doesn't belong to a program. For example, accessing an element beyond the bounds of an array.

Since nothing in your main() program gives a value to i, accessing array elements with index i results in undefined behavior, which could result in a segmentation error. (And even if it didn't crash, it couldn't possibly do anything useful with i, since its value is never defined in main() is never defined.)

Regards,

Dave
 
 

Recent GIDBlogToyota - 2009 May Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Two-Tier data dissemination code installation problem nidhibansal1984 Computer Software Forum - Linux 6 16-Sep-2007 11:13
Linked Lists advice request promsan C Programming Language 74 23-May-2007 09:29
Major newbie problem cynack MS Visual C++ / MFC Forum 1 08-Apr-2007 12:25
Help with syntax errors PeteGallo C Programming Language 7 08-Aug-2005 21:30
What is "Ambigious symbol" ??*( a compilation error) small_ticket C++ Forum 2 07-Jan-2005 22:10

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 07:09.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.