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

Thread: looping infile

Hybrid View

  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

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