# Calculate Salary.

Page 1 of 1

## 2 Replies - 2055 Views - Last Post: 22 March 2013 - 12:02 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=316267&amp;s=8259a7fc4a59a58158601b3d6dbf3048&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 steamheart

• New D.I.C Head

Reputation: 0
• 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

• Code herder

Reputation: 6160
• Posts: 21,236
• 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

• New D.I.C Head

Reputation: 0
• 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

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }