| biLmLik (27) | |
|
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;//pOinter tOken points tO char tO take tOken fOr strtok() functOn int sal,totalsal=0; //tO caLcuLate(add) saLaries frOm a fiLe infile.open("salin.txt"); outfile.open("salout.txt"); if(!infile) { cout<<"error opening fiLe"; } if(!outfile) { cout<<"error opening 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;//writing the tOtaL salaty in output fiLe infile.close(); //cLosing fiLes outfile.close(); system("pause"); } | |
|
|
|
| Peter87 (3691) | |||
|
Why all the upper case O and L? I think these lines should be inside the loop.
| |||
|
|
|||
| biLmLik (27) | |||
i did it b4 bt nOt working .................;'(
pLz help me guyz............ | |||
|
Last edited on
|
|||
| cire (1853) | |
Line 3 should be:#include <fstream> Line 7 should be: int main()Lines 32-33 should be inside the while loop. If they're outside the loop, totalSal will be equal to the last salary read from the file. You shouldn't be looping on eof(). while (infile.getline(readline, maxchar)) would be a better loop condition. Another possible point of failure: Names generally consist of more than one 'token'. If your salin.txt has first and last names then salaries, you're not parsing the file correctly. | |
|
|
|
| biLmLik (27) | |
| @aramila can u plz seeethat code tOO budddy ......... | |
|
|
|
| biLmLik (27) | |
| help me here in this cOde pLzzzzzzzzzzzz | |
|
|
|