Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,105 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,703 people online right now. Registration is fast and FREE... Join Now!




Employee payroll

 
Reply to this topicStart new topic

Employee payroll, print employees i alphabetical order

cmps_sciloser
15 Oct, 2007 - 03:36 AM
Post #1

New D.I.C Head
*

Joined: 16 Apr, 2007
Posts: 4


My Contributions
i am trying to use a sort function to sort the names of employees from a data file in alpha order. when i compile..there are no probs. however, when i run the progm with the data file, i get a bus error, core dumped result.

here is the code:
CODE


#include <iostream>
#include <iostream>
#include <string>

using namespace std;

void sort_last(string[], string[],  int);
void sort_first(string[], string[], int);

main()
{
string fname[50];
string lname[50];
int hrs, n=0;
float gross, rate, net, deductions;

cout<<"\tName"<<"\tHours"<<"\tRate"<<"\tGross"<<"\tNet"<<"\tDeductions\n";
cout<<endl;

        while(cin>>fname[50]>>lname[50])
{
  cin>>hrs>>rate;
  gross=hrs*rate;
  net=gross-deductions;
  deductions=9.10+25.65+.0375+.175;

  cout<<fname[n]<<" "<<lname[n]<<"\t"<<hrs<<"\t"<<rate<<"\t"<<gross<<"\t"<<net<<
"\t"<<deductions;
  cout<<endl;
}

sort_last(fname, lname, n);

for (int i=0; i<50; i++)
{
  cout<<fname[i]<<" "<<lname[i]<<endl;
}

return 0;


}



void sort_last(string fname[], string lname[], int n)
{
string save;

for (int i=0; i<n; i++)

for (int j=i; j<n; j++)
if (fname[i]>lname[j])

{
  save=fname[i];
  fname[i]=lname[j];
  fname[j]=save;

}
}





This post has been edited by cmps_sciloser: 15 Oct, 2007 - 03:45 AM
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Employee Payroll
15 Oct, 2007 - 03:31 PM
Post #2

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,858



Thanked: 49 times
Dream Kudos: 550
My Contributions
Well there are several problems I see here, but I think the main one you are having is the line:

while(cin>>fname[50]>>lname[50])

if you dimensioned these two arrays with:

string fname[50];
string lname[50];

then the only valid elements are 0 - 49. So for example you could use the line:
while (cin>>fname[49]>>lname[49])
if you wanted to.

I think what you MEANT was something like:

CODE
while (n++ < 50)
{
    cin >> fname[n] >> lname[n];
    ...
}


Strings are not usually stored in arrays like that. You are entering into the wonderful world of STL and I bet you would find containers such as a vector much more convenient for your purposes.

if for no other reason than the string class overloads the [ ] operator which can be confusing: people (me) keep expecting stringvar[5] to point to the 6th char of the string stringvar, and so it can be confusing when stringvar[5] is the 6'th string in the array stringvar, and stringvar[5][5] is the 6'th char of the 6'th string of the array stringvar.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 09:08PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month