CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: looping infile

  1. #1
    Join Date
    Apr 2007
    Posts
    7

    Exclamation looping infile

    Hi everyone,
    I am new here and new to C++ I am taking an intro class and am stuck on an assignment. Heres the thing the code has to read from the file and loop to get the info. The program :
    // reads in the number of salespeople
    // reads in the quota for that salesperson
    // reads in 12 actual monthly sales counts for that salesperson
    // determines number of months the salesperson did or did not meet quota
    // decides whether a campaign should be mounted
    // identifies the worst salesperson for meeting quotas
    The problem I am having is looping so that it will read each line/salesPerson
    Here is what I have so far and it does output the first and second lines from the .dat file


    infile >> salesPerson;
    infile>> quota;
    cout << salesPerson << "\t" << quota << "\t";
    infile >> salesPerson;

    while (infile.good())
    {
    infile >> month;//there are 12 months
    cout << month << " ";
    }

    cout << "\t" << under;
    cout << "\t" << quotaAndAbove;

    system("pause");

    Thanks in advance!

  2. #2
    Join Date
    Jul 2001
    Location
    Otaki, New Zealand
    Posts
    303

    Re: looping infile

    You will not get many replies if you don't use code tags. Also post your whole code and any sample data you are using.

    Just create a loop (either a for or a while) for the number of salespersons you need to read (making sure to check for end of file) and inside that loop, read the 12 quotas as a loop.

    Not very hard really!!

    Regards
    Alan

  3. #3
    Join Date
    Apr 2007
    Posts
    7

    Re: looping infile

    Thanks,
    the whole thing works, reads, calculates BUT
    I cant get it to loop right. But im closer than I was an hour ago.
    I tried while, for, if and i dunno dont work. When I nest the loops the first loop is the one that loops for the number of time it needs to read right? And the end } would go at the very end of the code and not after the loop statement right?

  4. #4
    Join Date
    Jul 2001
    Location
    Otaki, New Zealand
    Posts
    303

    Re: looping infile

    Post your whole code - otherwise no-one can see exactly what you're doing.

    Regards
    Alan

  5. #5
    Join Date
    Apr 2007
    Posts
    7

    Re: looping infile

    Heres the code in full , although I dont know what is ment by code tags....

    //Narrative: This is a program that -
    // reads in the number of salespeople
    // reads in the quota for that salesperson
    // reads in 12 actual monthly sales counts for that salesperson
    // determines number of months the salesperson did or did not meet quota
    // decides whether a campaign should be mounted
    // identifies the worst salesperson for meeting quotas
    //Writen by Lori Asmar
    //Input:
    // Please ref. I/O Figure #1A for Prompt messages
    //Output:
    // Please ref. I/O Figure #1A for display messages
    //*********************************************************************
    //Constants:
    //
    //*********************************************************************
    //Variables:
    // sring salesPeople1,salesPeople2,salesPeople3,salesPeople4
    // int quota
    // int yearsSales
    // int monthSales
    // string numSalesPeople
    //*********************************************************************
    //Operations:
    //step 1. open data file
    // 1a. check data file
    // 1b. if data file is missing
    // 1c. display error mesage:"Can't find file prog3a.dat,
    // and terminate program
    //step 2. read number of sales peopole
    //step 2a. display number of sales people
    //step 3. display header Sales Person, Quota, Monthly Sales, Under, =>
    //step 4. read sales person and quota
    // 4a. display sales person and quota
    //step 5. read months
    // 5a. display months
    //step 6. read month for calculation of under and meet and above quota
    // 6a. calulate under
    // 6b. display under
    //step 7. calculate if meet quota and above
    //step 8. display quota and above
    //step 10. determine if campaign should be mounted and display message
    //step 9. terminate program
    //*********************************************************************
    //I/O Figure #1
    //
    //*********************************************************************
    #include <iostream>
    #include <iomanip>
    #include <fstream>
    #include <string>

    using namespace std;


    int main()
    {
    const MONTH = 12;
    int month = 0;
    int numSalesPeople;
    string salesPerson;
    int quota = 0;
    int under = 0;
    int quotaAndAbove = 0 ;

    ifstream infile("prog3a.dat"); //opens prog3a.dat file

    if(infile.fail()) //checks to see if file was opened
    {
    cout<< "Can't find file prog3a.dat ";
    return 0; // terminate program
    }

    infile >> numSalesPeople;//read number of sales peopole, first line

    cout << " Total Sales People : " << numSalesPeople;//display number of sales people
    cout << "\n ";
    //display Sales Person, Quota, Monthly Sales, Under, =>
    cout << "Sales Person\tQuota\t\tMonthly Sales\t\t\tUnder\t=> ";//format table
    cout << "\n ";


    //read sales person and quota
    infile >> salesPerson;
    infile>> quota;

    for (int salesPerson = 0; salesPerson < numSalesPeople; salesPerson++)//loop for number of sales people
    {

    cout << salesPerson << "\t" << quota << "\t";//display sales person and quota
    infile >> salesPerson;

    while (infile.good())
    {
    infile >> month;//read months
    cout << month << " ";//display months
    //read month for calculation of under and meet and above quota
    if (month < quota)
    under = under ++;
    if(month >= quota)
    quotaAndAbove = quotaAndAbove++;

    }
    }

    cout << "\t" << under;//display under

    cout << "\t" << quotaAndAbove;// display meet quota and above

    system("pause");

    return 0; // terminates program
    }

  6. #6
    Join Date
    Jul 2001
    Location
    Otaki, New Zealand
    Posts
    303

    Re: looping infile

    When you type a message, the toolbar has a 'Code' icon - if you click it it pops up a box showing HTML style tags. Use these around any code that you post and it will keep the formatting - you can also find info on this if you read the FAQ for the forum.

    You look to be on the right lines. You need to check when you read any item from the file that no errors have occurred. You are also trying to read the salesperson twice.

    Regards
    Alan

  7. #7
    Join Date
    Apr 2007
    Posts
    7

    Post Re: looping infile

    Thanks, I really do appreciate your time,
    Next time I will do that. But I cant get it to loop for the number of lines in the file all I have for out put is total sales people:4, the header for the table, and the first line of sales people, Mbanner 35 and so on where do I start the loop,end the loop, and what kind of loop should I use, I have used so many differantcombintions my f5 key is worn out!
    the .dat file contains this:
    4
    MBanner 35 35 35 35 35 35 35 35 34 35 35 35 36
    BHunter 40 20 40 70 35 45 78 34 56 73 15 41 55
    GJorgensen 60 62 47 68 40 53 62 88 18 15 72 12 19
    TSmith 25 66 32 41 89 27 25 29 33 54 27 32 45


    and the output should look like this:
    Total Salespeople: 4
    Salesperson Quota Monthly Sales Under =>
    MBanner 35 35 35 35 35 35 35 35 34 35 35 35 36 1 11
    BHunter 40 20 40 70 35 45 78 34 56 73 15 41 55 4 8
    GJorgensen 60 62 47 68 40 53 62 88 18 15 72 12 19 7 5
    TSmith 25 66 32 41 89 27 25 29 33 54 27 32 45 0 12


    A "Sell IT! OR Else Campaign" must be mounted - 75% did not make yearly quotas
    Consider firing: GJorgensen

    I am so lost ! this was due Sunday April 14th AHHHHH

  8. #8
    Join Date
    Jul 2001
    Location
    Otaki, New Zealand
    Posts
    303

    Re: looping infile

    Here's my attempt - took me about 1 hour

    Code:
    //Narrative: This is a program that -
    // 1) reads in the number of salespeople
    // 2) reads in the name of the salesperson
    // 3) reads the quota for that salesperson
    // 4) reads in 12 actual monthly sales counts for that salesperson
    // 5) determines number of months the salesperson did or did not meet quota
    // 6) decides whether a campaign should be mounted
    // 7) identifies the worst salesperson for meeting quotas
    
    #include <iostream>
    #include <iomanip>
    #include <fstream>
    #include <string>
    #include <vector>
    
    using namespace std;
    
    static const int MONTHS = 12;
    
    // Use a class to store each salespersons info
    class SalesPerson
    {
        private:
            string          m_Name;
            unsigned int    m_TargetQuota;
            unsigned int    m_SalesFigures[MONTHS];
            unsigned int    m_UnderQuota;
    
        public:
            SalesPerson() : m_Name(""), m_TargetQuota(0), m_UnderQuota(0) 
            {
                for (unsigned int i = 0; i< MONTHS; m_SalesFigures[i++] = 0);
            }
    
            string&         Name()              { return m_Name; }
            unsigned int&   TargetQuota()       { return m_TargetQuota; }
            unsigned int *  SalesFigures()      { return m_SalesFigures; }
            unsigned int&   UnderQuota()        { return m_UnderQuota; }
    };
    
    // Get the number of sales people in file
    unsigned int GetNumberSalesPersons(ifstream& file)
    {
        unsigned int nSalesPersons = 0;
        file >> nSalesPersons;
        return ( file.good() ? nSalesPersons : 0 );
    }
    
    // Identify worst sales person
    // NOTE: Does not handle when two salespeople have same UnderQuota!!
    void WorstSalesPerson(int& nCurrentWorst, int nSalesPerson, vector<SalesPerson>& vSalesPersons)
    {
        nCurrentWorst = (nCurrentWorst == -1) ? nSalesPerson :
            ((vSalesPersons[nSalesPerson].UnderQuota() > vSalesPersons[nCurrentWorst].UnderQuota()) ? nSalesPerson : nCurrentWorst);
    }
    
    // Output the results in formatted format
    void OutputResults(vector<SalesPerson>& vSalesPersons)
    {
        unsigned int    NumSales = vSalesPersons.size();
        unsigned int    TotalUnder = 0;
        int             nWorstSalesPerson = -1;
    
        cout << "Total Salespeople: " << vSalesPersons.size() << endl;
        cout << "Salesperson\tQuota\tMonthly Sales                    \tUnder\tOver" << endl;
    
        for (unsigned int nSales = 0; nSales < NumSales; nSales++ ) {
            SalesPerson *pPerson = &vSalesPersons[nSales]; // saves retrieving many times
    
            cout << left << setw(12) << pPerson->Name() << '\t' << setw(5) << pPerson->TargetQuota() << '\t';
                
            for (unsigned int nCount = 0; nCount < MONTHS; nCount++)
                cout << setw(3) << pPerson->SalesFigures()[nCount];
    
            cout << "\t" << setw(5) << pPerson->UnderQuota() << '\t' << 12 - pPerson->UnderQuota() << endl;
    
            TotalUnder += pPerson->UnderQuota();
            WorstSalesPerson(nWorstSalesPerson, nSales, vSalesPersons);
        }
        
        cout << endl;
    
        // Not sure of your exact criteria for mounting a campaign
        // Total Months Under Quota > 3/4 total months figures supplied
        // Hence 4*Total > 3 * figures available
    
        if ( (4 * TotalUnder) > (3 * NumSales * MONTHS) )
            cout << "A \"Sell IT! OR Else Campaign\" must be mounted - 75% did not make yearly quotas" << endl;
    
        // Worst saleperson - of course there could be two
        cout << "Consider firing: " << vSalesPersons[nWorstSalesPerson].Name() << endl;
    }
    
    // Read information from file
    void ProcessFile(ifstream& file, vector<SalesPerson>& vSalesPersons)
    {
        unsigned int    nSalesPersons = GetNumberSalesPersons(file);
    
        for (unsigned int nCount = 0; (nCount < nSalesPersons) && file.good(); nCount++) {
            SalesPerson Person;
    
            file >> Person.Name();
            file >> Person.TargetQuota();
    
            for (unsigned int nMonth = 0; (nMonth < MONTHS) && file.good(); nMonth++) {
                file >> Person.SalesFigures()[nMonth];
                if ( Person.SalesFigures()[nMonth] < Person.TargetQuota() )
                    Person.UnderQuota()++;
            }
    
            if ( file.good() )
                vSalesPersons.push_back(Person);
        }
    
        if ( !file.good() ) {
            cout << "An error occurred reading data file." << endl;
            vSalesPersons.empty();    
        }
    }
    
    // Main
    int main()
    {
        ifstream    inFile("prog3a.dat");
        vector<SalesPerson> vSalesPersons;
    
        if ( inFile ) {
            ProcessFile(inFile, vSalesPersons);
            inFile.close();
    
            OutputResults(vSalesPersons);
    
        } else {
            cout << "Unable to open data file" << endl;
            return -1;
        }
        return 0;
    }
    There are various enhancements that could be made. You also didn't state anywhere what constitutes launching a campaign.

    Don't just copy verbatim - read it and learn from it, otherwise you will learn nothing!!

    Regards
    Alan

  9. #9
    Join Date
    Apr 2007
    Posts
    7

    Smile Re: looping infile

    WOW thanks so much, I will not just copy it, my instructor would never believe that I did that, This is only my 4th program and the programs we are doing are not so complicated. I will read and follow along how you have done yours and now I will have something to follow along to.
    Thanks so very much!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured