2 Replies - 641 Views - Last Post: 22 March 2013 - 12:02 AM Rate Topic: -----

#1 steamheart  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 25-February 12

Calculate Salary.

Posted 21 March 2013 - 06:32 PM

A given input file contain Name of the employee and salary of current month. There is a single space between the name and the salary. Name and salary can not contain spaces. Calculate the total salaries of the employees. Create an output file and write the total salary in that file.


This is the input file.
Aamir 12000
Ahmad 15000
Adil 13500
Ammara 11500
Adnan 10500
Asghar 12500
Ali 16500
Abid 19000


Problem is this program not calculate the total salary. Although it create the file.
I think problem in "STRTOK()" funtion.


#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cstring>
using namespace std ;
int main ()
{
    ifstream infile;
    char salary[] = "salarin.txt" ;
   ofstream outfile ;
    char total_sal[] = "salout.txt" ;
    const int max = 100 ;
    char lin [max] ;
    long sal , total_salary  ;
    sal = 0 ;
    total_salary = 0 ;
  char *ptrtok ;
    infile.open (salary , ios::in );
    outfile.open (total_sal , ios::out ) ;

    if (!infile )
    {

        cout << " Sorry! Can't opened this file " ;
        return 1 ;

    }
    if (!outfile )
    {

       cout << " Sorry! Can't opened this file " ;
        return 1;

    }

    while (!infile.eof() )
    {
        infile.getline (lin , max );
        cout << lin << endl ;
        ptrtok = strtok(lin, " " ) ;
        ptrtok = strtok(NULL, " ") ;
        sal = atol(ptrtok) ;
        total_salary = total_salary + sal ;
    }
    outfile << " The Total salary is " << total_salary ;
    infile.close();
    outfile.close();
    return (0) ;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Calculate Salary.

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3169
  • View blog
  • Posts: 9,595
  • Joined: 05-May 12

Re: Calculate Salary.

Posted 21 March 2013 - 07:02 PM

Out of curiosity, what lead you to choose to use strtok() instead of some other technique for parsing your file?

Back on topic: Are you sure your file is space delimited and not tab delimited?

I just confirmed: your file is tab delimited. This is assuming that you just cut and pasted from your input file to this forum. When I tried to edit your post, I saw that all the salaries were nicely lined up which would indicate that your file is tab delimited.
Was This Post Helpful? 0
  • +
  • -

#3 steamheart  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 25-February 12

Re: Calculate Salary.

Posted 22 March 2013 - 12:02 AM

Thank you. I m very fool. I wasted one to two hour for finding the problem. Yeah it is the tab delimiter. Now i have corrected it.
And curiosity. Just practice of file handling.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1