I am doing a program on payroll before any deductions are made but i seem to be encountering some problems.
Here is the program

# include <stdio.h>
# include <string.h> 
# include <stdlib.h> 
# include <fstream>
# include <iostream>
# include <cstring>

using namespace std;

typedef struct {
        char fname[25];
        char lname[25];
        char date[15];
        int timein;
        int timeout;
        float pay;
        } worker;

const int num_of_workers=10;
int worker_num=0;
ofstream workerfile ("workerfile.txt", ios::app|ios::out);
//ifstream workerfile ("workerfile.txt", ios::app|ios::out); 
  
//************************************************************* 
void readfromfile () 
{
     string line;
     if (workerfile.is_open())
     {
        while (! workerfile.eof() )
        {
              workerfile << line << endl;
              cout << line << endl;
              }
      }
     else cout << "Unable to open file"; 
     scanf("%c");

 }


//*************************************************************
void writetofile(worker tmp_array[]) 
{
     int i;
     int file_num=0;
     char tmp;
     string line;
     cout << "\nPlease wait while array is saved to file.";
     if (workerfile.is_open())
     {
       // while (!workerfile.eof() )
       // {
        //        workerfile << line;    
       // }
         for(i=0;i<=worker_num;i++);
         workerfile << tmp_array[i].fname <<endl;
         workerfile << tmp_array[i].lname <<endl;
         workerfile << tmp_array[i].date <<endl;
         workerfile << tmp_array[i].timein <<endl;
         workerfile << tmp_array[i].timeout <<endl;
         workerfile << tmp_array[i].pay <<endl;
         cout << "\nArray has been saved to file.";
         }
      else cout << "Error saving to file. File was unable to be opened.";
 }
     
//*************************************************************
void bubble_sort(worker tmp_array[]){
      int i, j, flag = 1; 
      char tmpfname[10];
      char tmplname[15];
      char tmpdate[15];
      int tmptimein;
      int tmptimeout;
      float tmppay;             
      
      printf("\nPlease wait while array is sorted.");
      for(i = 1; (i <= num_of_workers) && flag; i++)
      {
          flag = 0;
          for (j=0; j < (num_of_workers -1); j++)
          {
              printf(".");
               if (tmp_array[j+1].pay > tmp_array[j].pay)      
               { 
                    strcpy(tmpfname, tmp_array[j].fname);             
                    strcpy(tmp_array[j].fname, tmp_array[j+1].fname);
                    strcpy(tmp_array[j+1].fname, tmpfname);
                    
                    strcpy(tmplname, tmp_array[j].lname);             
                    strcpy(tmp_array[j].lname, tmp_array[j+1].lname);
                    strcpy(tmp_array[j+1].lname, tmplname);
                    
                    strcpy(tmpdate, tmp_array[j].date);             
                    strcpy(tmp_array[j].date, tmp_array[j+1].date);
                    strcpy(tmp_array[j+1].date, tmpdate);
                    
                    tmptimein = tmp_array[j].timein;             
                    tmp_array[j].timein = tmp_array[j+1].timein;
                    tmp_array[j+1].timein = tmptimein;
                    
                    tmptimeout = tmp_array[j].timeout;             
                    tmp_array[j].timeout = tmp_array[j+1].timeout;
                    tmp_array[j+1].timeout = tmptimeout;
                    
                    tmppay = tmp_array[j].pay;             
                    tmp_array[j].pay = tmp_array[j+1].pay;
                    tmp_array[j+1].pay = tmppay;
                    flag = 1;              
               }
          }
      }
      printf("\nThe array has been sorted.\n");  
}

//*************************************************************
void addworker(worker tmp_array[]){
     bool ok;
     int hrpay = 11;
     int time=0;
     if (worker_num<num_of_workers) {
        printf("\nAdd worker %d\n",worker_num+1);
        printf("\nEnter First Name: ");
        cin >> tmp_array[worker_num].fname;
        printf("\nEnter Last Name: ");
        cin >> tmp_array[worker_num].lname;
        printf("\nEnter date: ");
        cin >> tmp_array[worker_num].date;
        printf("\nEnter Time In (in 24hr clock format): ");
        scanf("%d",&tmp_array[worker_num].timein);
        printf("\nEnter Time Out (in 24hr clock format): ");
        scanf("%d",&tmp_array[worker_num].timeout);
        worker_num=worker_num + 1;
        if (tmp_array[worker_num].timein >= tmp_array[worker_num].timeout)
           {
          int timeout;
          int timein;
          int pay;
          int time;
          int hrspay;
             timeout=tmp_array[worker_num].timeout;
             timein=tmp_array[worker_num].timein;                            
           //time=(tmp_array[worker_num].timeout-tmp_array[worker_num].timein);
           //tmp_array[worker_num].pay=time*hrpay;
           time=timeout-timein;
           pay=time*hrspay;
           pay=tmp_array[worker_num].pay;
           }
        else   
           {
           time=(tmp_array[worker_num].timeout - tmp_array[worker_num].timein);
           tmp_array[worker_num].pay=time*hrpay;      
           }
        //appendtofile(tmp_array);
        }
         else {
             printf("Array Full. No more workers can be added at this time.");
         }
        }
//*****************************************************************
void findworker(worker tmp_array[]){
     char searchlname[20];
     bool found;
     int diff;
     int i;
     
     printf("\n************FIND worker************");
     printf("\nEnter workers lname:");
     scanf("%s",searchlname);
     found = false;
     
     for(i=0; i<num_of_workers; i++){
              diff = strcmp(searchlname,tmp_array[i].lname);
              if(diff== 0){
                        found=true;
                        printf("\nFirst Name: %s",tmp_array[i].fname);
                        printf("\nLast Name: %s",tmp_array[i].lname);
                        printf("\nDate: %s",tmp_array[i].date);
                        printf("\nTime In: %d :00 hrs",tmp_array[i].timein);
                        printf("\nTime Out: %d :00 hrs",tmp_array[i].timeout);
                        printf("\nPay: $%f",tmp_array[i].pay);
                        }
              }
     
     if (found==false){
                       printf("\nNo Matches Found.");
                       }
     }
//****************************************************************
void displayAll(worker tmp_array[]){
     int i;
     printf("\n************DISPLAY ALL************\n");
     for(i=0; i<worker_num; i++){
              printf("\nFirst Name: %s",tmp_array[i].fname);
              printf("\nLast Name: %s",tmp_array[i].lname);
              printf("\nDate: %s",tmp_array[i].date);
              printf("\nTime In: %d :00 hrs",tmp_array[i].timein);
              printf("\nTime Out: %d :00 hrs",tmp_array[i].timeout);
              printf("\nPay: $%f",tmp_array[i].pay);
              printf("\n*************************************************");
              }
     }

//*********************************************************************
int main(void){
    
    worker worker_array[num_of_workers];
    char choice;
    printf("\n");
    printf("\n****************MAIN MENU**************");
    printf("\n Choose One of the Following:");
    printf("\n (1) Add Worker");
    printf("\n (2) Find Worker");
    printf("\n (3) Display All");
    printf("\n (4) Sort by Pay");
    printf("\n (5) Save to File");
    printf("\n (6) EXIT\n");
    
    scanf("%c",&choice);
    
    if (choice=='6'){
        workerfile.close();
        exit(0);
        }
        
    while (choice!='6'){
          switch(choice){
                         
          case'1': addworker(worker_array);
                   break;
          case'2': findworker(worker_array);
                   break;
          case'3': displayAll(worker_array);
                   break;
          case'4': bubble_sort(worker_array);
                   break;         
          case'5': writetofile(worker_array);
                   break;                   
          case'6': workerfile.close();
                   exit(0);
                   break;
          }
          printf("\n****************MAIN MENU**************");
          printf("\n Choose One of the Following:");
          printf("\n (1) Add Worker");
          printf("\n (2) Find Worker");
          printf("\n (3) Display All");
          printf("\n (4) Sort by Pay");
          printf("\n (5) Save to File");
          printf("\n (6) EXIT\n");
          cin >> choice;
          }
    workerfile.close();      
    return 0;
    
}

Recommended Answers

All 3 Replies

>>but i seem to be encountering some problems.
Do you mind explaining what problems? Do are we supposed to guess?

oh sorry forgot about that
first problem is despite i put in the calculation for the payroll it never seems to work out properly and whenever i sort then use the display all function i get a bunch of random gibberish

this is the code i entered for the calculation
#
void addworker(worker tmp_array[]){
#
bool ok;
#
int hrpay = 11;
#
int time=0;
#
if (worker_num<num_of_workers) {
#
printf("\nAdd worker %d\n",worker_num+1);
#
printf("\nEnter First Name: ");
#
cin >> tmp_array[worker_num].fname;
#
printf("\nEnter Last Name: ");
#
cin >> tmp_array[worker_num].lname;
#
printf("\nEnter date: ");
#
cin >> tmp_array[worker_num].date;
#
printf("\nEnter Time In (in 24hr clock format): ");
#
scanf("%d",&tmp_array[worker_num].timein);
#
printf("\nEnter Time Out (in 24hr clock format): ");
#
scanf("%d",&tmp_array[worker_num].timeout);
#
worker_num=worker_num + 1;
#
if (tmp_array[worker_num].timein >= tmp_array[worker_num].timeout)
#
{
#
int timeout;
#
int timein;
#
int pay;
#
int time;
#
int hrspay;
#
timeout=tmp_array[worker_num].timeout;
#
timein=tmp_array[worker_num].timein;
#
//time=(tmp_array[worker_num].timeout-tmp_array[worker_num].timein);
#
//tmp_array[worker_num].pay=time*hrpay;
#
time=timeout-timein;
#
pay=time*hrspay;
#
pay=tmp_array[worker_num].pay;
#
}
#
else
#
{
#
time=(tmp_array[worker_num].timeout - tmp_array[worker_num].timein);
#
tmp_array[worker_num].pay=time*hrpay;
#
}
#
//appendtofile(tmp_array);
#
}
#
else {
#
printf("Array Full. No more workers can be added at this time.");
#
}
#
}

cin >> tmp_array[worker_num].date;
printf("\nEnter Time In (in 24hr clock format): ");
scanf("%d",&tmp_array[worker_num].timein);

Pick either C or C++
C/C++ (what you posted) is a disaster area.

There is at least one place where you've used scanf so badly that it WILL trash memory you don't own.

There there's the gratuitous global variables for no reason, and the mis-named readfromfile() function which seems to be writing to a file.

> Can anyone help me with this program?
http://www.catb.org/~esr/faqs/smart-questions.html#prune

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.