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) ;
}

New Topic/Question
Reply



MultiQuote




|