I am having a problem getting my program to loop so that it will read the next l
ID: 3626832 • Letter: I
Question
I am having a problem getting my program to loop so that it will read the next line.The program is suppose to output 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
but my program is only giving me 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
A "Sell IT! OR Else Campaign" must be mounted - 75% did not make yearly quotas
Consider firing: GJorgensen
here is my program:
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
int main()
{
int month = 0;
int numSalesPeople;
string salesPerson;
int quota = 0;
int under = 0;
int quotaAndAbove = 0 ;
ifstream infile("prog3a.txt"); //opens prog3a.dat file
if(infile.fail()) //checks to see if file was opened
{
cout<< "Can't find file prog3a.txt ";
return 1; // terminate program
}
infile >> numSalesPeople; //read number of sales peopole, first line
cout << " Total Sales People : " << numSalesPeople;//display number of sales people
cout << " ";
//display Sales Person, Quota, Monthly Sales, Under, =>
cout << "Sales Person Quota Monthly Sales Under => ";//format table
cout << " ";
//read sales person and quota
infile >> salesPerson >> quota ;
cout << salesPerson;
cout << " " << quota << " ";//display sales person and quota
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 << " " << under;//display under
if (quota=quotaAndAbove)
cout << " " << quotaAndAbove; // display meet quota and above
if (quota=under)
cout<< endl<<endl<<endl<<"A "Sell IT! OR Else Campaign" must be mounted - 75% did not make yearly quotas." << endl;
cout <<endl<< endl<< "Consider firing: GJorgensen " << endl;
infile.close();
system("pause");
return 0; // terminates program
}
Explanation / Answer
Alright, so a couple of problems that i found is: Trying to emulate the output does not make it correct. Meaning that when/if someone changes the input file the suggested fired person will always be GJorgensen and 75% will always have undersold, even if this is not the case. So with that being wrong we must also note that when in an if statement you must always make sure you use a == instead of a = because this will ALWAYS return TRUE! This is a big problem and obviously changes the results of the program. So now that that is out of the way, the reason why you were only printing out the first line of the persons sales is because that was all you were reading in. Now what i did was fixed your errors in the if statement. I also replace your one while loop with a 2 for loops to go through the 12 months of sales and the number of sales people. I also added in an IF statement to catch if there is more people said to be sales then are in the file so as to not get any duplicate vales. So the code is in the link below http://codepad.org/tW5aRCOg PLEASE RATE!
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.