This project is based on the banking system, UTT Insurance Limited. Each month the company calculates commission for its employee. UTT Insurance Limited has 5 employees and their names are Denver, Garry, Cindy, Samdaye and Nalinee. Each employee has their respective sales for the month which is recorded in an external file, “sales.txt”. It the total sales is more than one million the commission is 10% of the sales otherwise the commission is 5% of the sales. The external file, “sales.txt” has all the sales for one employee before recording another employee. The end of recording of each teller is noted by ‘E’. For example
Denver 100000 250000 50000 30000 60000 10000 E Garry 100000 40000 50000 120000 300000 50000 80000 120000 E
Write a C++ program that opens the external file, “sales.txt” and reads the data and writes to an external file, “commission.txt” the following information:

Denver Total sales 500000 Commision 25000
Garry Total sales 1210000 Commision 121000

Nice little problem you posted ... there are many ways to code a solution.

Some nice and neat way would be best with clear logic flow.

Probably using a C++ struct ( or class ) would help quite a bit.

Please show us what code you have tried so far.

Then we can see where you are 'stuck' and how to best get you 'un-stuck'.

Also provide a copy of the data file you are using.

If you know about using C++ struct ... here is an hint to a start:

struct Agent
{
    string name;
    vector< int > sales; // dollars in a month //
} ;

And ...
if you know about using stringstream objects ...
hint hint ...
use them here to ease your coding.

Edited 1 Year Ago by David W

This is what I did since my code is not outputting the correct total i dont know how to move foward.The code is supposed to output each person's total sales for the monthe and i also don't know how to do that.

Attachments
#include <iostream>
#include <fstream>
using namespace std;

int main ()
{
    ifstream file;
 
    
    int total = 0, limit,num, a=0;
    double commission;
    int arr [15];
    
    file.open("sales.txt");
    
    file >> limit;
    file >> total ;
    
    while (  a < limit);
    {
        file >> num;
        total = total + num;
        a ++ ;
     }
       arr [a] = total ;
      if(total > 1000000)
      commission = 0.10 * total ;
      else
      commission = 0.05 * total;
    
	for (a=0; a < 15 ; a ++);
    cout << total << endl; 
    
	//system ("pause");
    
    return 9;
}
5

Denver 100000 50000 70000 550000 350000 120000 E
Garry 100000 600000 59000 67000 450000 300000 E
Cindy 100000 560000 750000 80000 290000 65000 E
Samdaye 100000 250000 680000 35000 250000 300000 E
Nalinee 100000 780000 340000 55000 96000 900000 E

Ok ... It looks like you want to do something like the following ...
(... and it seems also that you do not need to store input lines in an array ... so ... no array needed ...)

open input file as fin
open output file as fout

if fin and fout ok
    While still a next data line in input file 
        read data on line from input file 
        process data in line
        write processed data to output file 

Or more C++ like:

open input file as fin
open output file as fout

if fin and fout ok
     While next data line reads in ok  
        process data in line
        write processed data to output file 

Is this what you wish to do ?

Try to code in the C++ code ... using the ideas above ... and see what you can do and show us what you have ... if you get stuck?

P.S.
Things you will want to get clear in your mind before you start ...
* Does you data file always begin with an int (integer) value, that indicates how many line follow?
* Is there a blank (empty file) line immediately after that int?
* Do all the data records have their own line ... and each line ends with " E" (singlespaceE) character sequence?
* Are all names single names i.e. WITHOUT spaces in the name?
* Is there always 6 numbers after the name and before the " E" on each data file line? (Note that in the example you provided at the top, one record had 6 numbers, the next had 8 numbers.)

Edited 1 Year Ago by David W

This article has been dead for over six months. Start a new discussion instead.