counter isnt counting right

This is a discussion on counter isnt counting right within the C++ Programming forums, part of the General Programming Boards category; The counter counts right the first time. But not the next 3 times. A little confused on how this happened. ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    12

    counter isnt counting right

    The counter counts right the first time. But not the next 3 times. A little confused on how this happened.

    Code:
     #include <iostream>
       #include <string>
       #include <fstream>
       #include <iomanip>
       
       using namespace std;
       
       int main()
       
       {
                 //Difine variables
                 int totalPeople; //from inputfile
                 string salesName; //from input file
                 int quota;  //from input file
                 int underCounter = 0;
                 int overCounter = 0;
                 int quotaPercent;
                 double quotaDecimal;  //decimal before turning until percent
                 int yearlyUnder; //if under is greater than zero add to this counter
                 
                 //Individual months
                 int monthJan;
                 int monthFeb;
                 int monthMar;
                 int monthApr;
                 int monthMay;
                 int monthJun;
                 int monthJul;
                 int monthAug;
                 int monthSep;
                 int monthOct;
                 int monthNov;
                 int monthDec;
                 
                 ifstream ifile("prog3a.dat");          //opens file3a.dat
                 
                 if(ifile.fail())  //checks to see if file was able to open
                 {
                                 cout << "Can't find file prog3a.dat" <<endl;  //displays if file cannot be found
                 system("PAUSE");
                 return(0);
                 }
                 
                 ifile >> totalPeople;
                 
                 cout << "Total Salespeople: " << totalPeople << endl;
                 cout << endl << "Salesperson\tQuota\t\tMonthly Sales\t\t\tUnder\t=>" << endl;
                 
                 ifile >> salesName >> quota >> monthJan >> monthFeb >> monthMar >> monthApr >> monthMay >> monthJun >> monthJul >> monthAug >> monthSep >> monthOct >> monthNov >> monthDec;
                 
                 while(!ifile.eof())
                 {
                                    
                  underCounter = 0;
                  overCounter = 0;
                  
                                    if (quota < monthJan) 
                                    { 
                                       underCounter++;
                                    }   
                                    else
                                    { 
                                       overCounter++;
                                    }
                                    if (quota < monthFeb)
                                       underCounter++;
                                    else
                                       overCounter++;
                                       
                                    if (quota < monthMar)
                                       underCounter++;
                                    else
                                       overCounter++;
                                       
                                    if (quota < monthApr)
                                       underCounter++;
                                    else
                                       overCounter++;
                                       
                                    if (quota < monthMay)
                                       underCounter++;
                                    else
                                       overCounter++;
                                       
                                    if (quota < monthJun)
                                       underCounter++;
                                    else
                                       overCounter++;
                                       
                                    if (quota < monthJul)
                                       underCounter++;
                                    else
                                       overCounter++;
                                       
                                    if (quota < monthAug)
                                       underCounter++;
                                    else
                                       overCounter++;
                                       
                                    if (quota < monthSep)
                                       underCounter++;
                                    else
                                       overCounter++;
                                       
                                    if (quota < monthOct)
                                       underCounter++;
                                    else
                                       overCounter++;
                                       
                                    if (quota < monthNov)
                                       underCounter++;
                                    else
                                       overCounter++;
                                       
                                    if (quota < monthDec)
                                       underCounter++;
                                    else
                                       overCounter++;
                                       
                                    if (underCounter > 0) 
                                    {
                                       yearlyUnder++;
                                    }
                                    
                      //Displays output
                      
                      cout << "\n" << salesName;
                      
                      if(salesName.length() < 8)
                      {
                               cout << "\t\t";
                      }
                      else
                      {
                               cout << "\t";
                      }                
                 
                      cout << quota << "\t" << monthJan << " " << monthFeb << " ";
                      cout << monthMar << " " << monthApr << " " << monthMay << " " << monthJun << " ";
                      cout << monthJul << " " << monthAug << " " << monthSep << " " << monthOct << " ";
                      cout << monthNov << " " << monthDec << "\t" << underCounter << "\t" << overCounter;
                      
                      ifile >> salesName >> quota >> monthJan >> monthFeb >> monthMar >> monthApr >> monthMay >> monthJun >> monthJul >> monthAug >> monthSep >> monthOct >> monthNov >> monthDec;
                        
               }          
               
               
                                    quotaDecimal = yearlyUnder / totalPeople;
                                    
                                    if (quotaDecimal > .20)
                                    {
                                       quotaPercent = quotaDecimal * 100;
                                       cout << "\n\nA Sell IT! OR Else Campaign must be mounted - " << yearlyUnder;
                                       cout << "% did not make yearly quotas" << endl;
                                    }
      
      system("PAUSE");
      
      return(0);

  2. #2
    Registered User
    Join Date
    Nov 2005
    Posts
    12
    This is what the .dat file looks like

    Code:
    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
    Any help would be great. Thanks.

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    85
    You should only read in ONE value before the eof() loop.
    ifile >> salesName
    That before the loop, then the rest in the while loop and fix up the calculations for this change

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    The basic rule for reading from a file is: the read statement should be a while loop conditional, e.g.
    Code:
    while( inputFile>>input)
    {
    	...
    	...
    }
    inputFile>>input calls a function that reads data into input, and then the function returns what's on the left side of the read statement, which in this case is inputFile, which converts the while conditional to:

    while(inputFile)

    If any errors occur that will prevent you from reading data from the file, the inputFile object will evaluate to false in the while conditional. End of file(eof) is considered an error, so the while loop will terminate correctly when you reach the end of the data in a file. However, there are other possible errors that can occur while reading from a file. If one of those errors occurs, then a loop such as:
    Code:
    while(!inputFile.eof())
    {
    
    }
    will try to keep reading data because it hasn't encountered eof yet--but the error will prevent the read statement from reading any data. So the loop will keep looping indefinitely, not being able to read in any data because of the error and never reaching eof.

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    12
    Quote Originally Posted by jlf029
    You should only read in ONE value before the eof() loop.
    ifile >> salesName
    That before the loop, then the rest in the while loop and fix up the calculations for this change
    i keeps looping the same name and data. i must've put it in the wrong spot.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,497
    > int monthJan;
    > int monthFeb;
    Consider
    int months[12];
    as a means of simplifying the code with a few well placed loops.

  7. #7
    Registered User
    Join Date
    Nov 2005
    Posts
    12
    ok. I finished up the code finally, but i'm still having the same problem. I don't see how these counters aren't working right. If someone can help me out i'd appreciate it. thanks.

    Code:
    /* Program Name: prog3
       Author: Javier Guzmanv
       Narrative: Determine number of months the salesperson did or didn't meet quota.
                  If more than 20% didnt meet quota, a campaign should be implemented.
                  Identify the worst salesperson and consider firing him/her.
       Inputs: prog3a.dat - input file
               Format: number of salespeople - int
                       salesperson name - string
                       quota - int
                       12 individual months - int
       Output: Ref. Fig3
       Operations:
                  1) Attach to file prog3a.dat
                  2) If unsuccessful - display error message
                  3) Get number of salespeople
                  4) For every salesperson
                     a) If quota is less thn any monthly sales, add one to the under
                        counter.
                     b) If quota is equal to or greater than monthly sales, add one to the 
                        over counter.
                     c) Continue steps a & b until all 12 months are used.
                     d) If under is greater than zero add 1 to the yearly quota.
                  5) a) Divide yearly quotas by total salespeople to get yearly percentage.
                     b) If yearly percentage is greater than 20% display a "Sell it
                        or Else" campaign.
                  6) Display which sales person is the worst and consider firing him/her.
       */
       
       #include <iostream>
       #include <string>
       #include <fstream>
       #include <iomanip>
       
       using namespace std;
       
       int main()
       
       {
                 //Difine variables
                 int totalPeople; //from inputfile
                 string salesName; //from input file
                 int quota;  //from input file
                 int underCounter;
                 int overCounter;
                 int quotaPercent;
                 double quotaDecimal;  //decimal before turning until percent
                 int yearlyUnder = 0; //if under is greater than zero add to this counter
                 
                 int maxunderCounter = 0;
                 string maxsalesName;
                 
                 //Individual months
                 int monthJan;
                 int monthFeb;
                 int monthMar;
                 int monthApr;
                 int monthMay;
                 int monthJun;
                 int monthJul;
                 int monthAug;
                 int monthSep;
                 int monthOct;
                 int monthNov;
                 int monthDec;
                 
                 ifstream ifile("prog3b.dat");          //opens file3a.dat
                 
                 if(ifile.fail())  //checks to see if file was able to open
                 {
                  cout << "Can't find file prog3b.dat" <<endl;  //displays if file cannot be found
                  system("PAUSE");
                  return(0);
                 }
                 
                 ifile >> totalPeople;
                 
                 cout << "Total Salespeople: " << totalPeople << endl;
                 cout << endl << "Salesperson\tQuota\t\tMonthly Sales\t\t\tUnder\t=>" << endl;
                 
                 ifile >> salesName >> quota >> monthJan >> monthFeb >> monthMar >> monthApr >> monthMay >> monthJun >> monthJul >> monthAug >> monthSep >> monthOct >> monthNov >> monthDec;
                 
                 while(!ifile.eof())
                 {
                  
                  underCounter = 0;
                  overCounter = 0;
                  
                                    if (quota < monthJan) 
                                    { 
                                       underCounter++;
                                    }   
                                    else
                                    { 
                                       overCounter++;
                                    }
                                    if (quota < monthFeb)
                                       underCounter++;
                                    else
                                       overCounter++;
                                       
                                    if (quota < monthMar)
                                       underCounter++;
                                    else
                                       overCounter++;
                                       
                                    if (quota < monthApr)
                                       underCounter++;
                                    else
                                       overCounter++;
                                       
                                    if (quota < monthMay)
                                       underCounter++;
                                    else
                                       overCounter++;
                                       
                                    if (quota < monthJun)
                                       underCounter++;
                                    else
                                       overCounter++;
                                       
                                    if (quota < monthJul)
                                       underCounter++;
                                    else
                                       overCounter++;
                                       
                                    if (quota < monthAug)
                                       underCounter++;
                                    else
                                       overCounter++;
                                       
                                    if (quota < monthSep)
                                       underCounter++;
                                    else
                                       overCounter++;
                                       
                                    if (quota < monthOct)
                                       underCounter++;
                                    else
                                       overCounter++;
                                       
                                    if (quota < monthNov)
                                       underCounter++;
                                    else
                                       overCounter++;
                                       
                                    if (quota < monthDec)
                                       underCounter++;
                                    else
                                       overCounter++;
                                    
                                    if (underCounter > 0)
                                    {
                                         yearlyUnder++;
                                    }               
                      //Displays output
                      
                      cout << "\n" << salesName;
                      
                      if(salesName.length() < 8)
                      {
                               cout << "\t\t";
                      }
                      else
                      {
                               cout << "\t";
                      }                
                 
                      cout << quota << "\t" << monthJan << " " << monthFeb << " ";
                      cout << monthMar << " " << monthApr << " " << monthMay << " " << monthJun << " ";
                      cout << monthJul << " " << monthAug << " " << monthSep << " " << monthOct << " ";
                      cout << monthNov << " " << monthDec << "\t" << underCounter << "\t" << overCounter;
                      
                      ifile >> salesName >> quota >> monthJan >> monthFeb >> monthMar >> monthApr >> monthMay >> monthJun >> monthJul >> monthAug >> monthSep >> monthOct >> monthNov >> monthDec;
                               
                      if (maxunderCounter < underCounter)
                      {
                           maxsalesName = salesName;
                      }     
               }          
         
                       quotaDecimal = yearlyUnder / totalPeople;
                                    
                       if (quotaDecimal > .20)
                       {
                       quotaPercent = quotaDecimal * 100;
                       cout << "\n\nA Sell IT! OR Else Campaign must be mounted - " << quotaPercent;
                       cout << "% did not make yearly quotas" << endl;
                       cout << "\nConsider Firing: " << maxsalesName << endl;
                       }
                       
                                
      cout << "\n\n"; 
      system("PAUSE");
      return(0);
    }
    output looks like this:

    Code:
    Total Salespeople: 6
    
    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     7       5
    GJorgensen      60      62 47 68 40 53 62 88 18 15 72 12 19     5       7
    LLewis          35      56 37 40 49 37 28 39 43 54 37 42 45     11      1
    JMathers        25      31 38 25 43 53 26 26 33 27 45 38 31     11      1
    TSmith          25      66 32 41 89 27 25 29 33 54 27 32 45     11      1
    
    A Sell IT! OR Else Campaign must be mounted - 100% did not make yearly quotas
    
    Consider Firing: TSmith
    
    
    Press any key to continue . . .

  8. #8
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    1)Where do you have your read statement as a while loop conditional?

    2) Where do you have a months[] array instead of 12 separate variables?

  9. #9
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    1) 1) Always initialize your variables:
    Code:
    int totalPeople; //from inputfile
    string salesName; //from input file
    int quota;  //from input file
    int underCounter;
    int overCounter;
    int quotaPercent;
    double quotaDecimal;  //decimal before turning until percent
    int yearlyUnder = 0; //if under is greater than zero add to this counter
    
    int maxunderCounter = 0;
    string maxsalesName;
    2) Consider converting all these statements:
    Code:
    if (quota < monthJan) 
    { 
       underCounter++;
    }   
    else
    { 
       overCounter++;
    }
    to ones using the conditional operator:
    Code:
    (quota < monthJan) ? underCounter++: overCounter++;
    That will reduce 50 lines of code to 10 lines. Furthermore, if you put all the months in an array, like Salem suggested, then you can put that conditional statement inside a for-loop. Inside the for-loop, the name of your months array will have a subscript after it, e.g.
    Code:
    (quota < months[0]) ? underCounter++: overCounter++;
    If you make the index of the months array a variable--and the same variable as the for-loop counter--then as the for-loop increments the loop counter, that will cause the index of the months array to increment as well. The for-loop will end up comparing quota to every month in the array. That will enable you to convert 50 lines of code to only 3 lines!
    Last edited by 7stud; 11-24-2005 at 11:24 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Promblem with code
    By watchdogger in forum C Programming
    Replies: 18
    Last Post: 01-31-2009, 05:36 PM
  2. Page File counter and Private Bytes Counter
    By George2 in forum Tech Board
    Replies: 0
    Last Post: 01-31-2008, 02:17 AM
  3. Flowchart Question
    By dmkanz07 in forum C Programming
    Replies: 1
    Last Post: 04-08-2007, 11:33 AM
  4. Counter Heap Sort
    By Achillles in forum C++ Programming
    Replies: 1
    Last Post: 10-09-2002, 12:17 PM
  5. how to obtain first character of every other word
    By archie in forum C++ Programming
    Replies: 8
    Last Post: 02-18-2002, 12:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21