i want tO add the salaries from a fiLe tO the prOgramme aLL the cOde is compiLing but the problem is that output file is not showing total of the salaries..
the ifstream fiLe cOntents are :
ALi 5000
zeshan 7000
biL 4000
mLik 5000
the ofstream fiLe cOntent should b :
totaL salary =21000
bt it is shOwing
total salary=0
here is the cOde of programme :
#include<iostream>
#include<fstream.h>
#include<cstring>
#include<cstdlib>
using namespace std;
main()
{
ifstream infile; //input file
ofstream outfile; //output fiLe
const int maxchar=100; //maximum nO of characters aLLowed
char readline[maxchar]; //char readline decLared tO read frOm Line frOm fiLe
char*token; // take tOken fOr strtok() functOn
int sal,totalsal=0; //tO caLcuLate(add) saLaries frOm a fiLe
while(!infile.eof())
{
infile.getline(readline,maxchar); //getline fOr input frOm fiLe
token=strtok(readline," "); //1st tOken is name
token=strtok(NULL," "); //2nd tOken is saLary
sal=atoi(token); //atOi cOnvrts char tO int
totalsal+=sal; //add salaries
}
outfile<<"tOtaL saLary = "<<totalsal; //tOtaL salaty in output fiLe
Is your lower-case 'o' key broken? Anyway, have you tried using the '>>' operator on the stream, rather than getline() and strtok(), to parse the file?
1 2 3 4 5 6
std::string s;
int i;
while (!infile.eof()) {
infile >> s >> i;
totalsal += i;
}