Reputation: 1
For an assignment, I need to read in multiple sections followed by percentages. For example,
2 80 97
5 69 79 89 99 58
7 60 70 80 90 100 0 59
(2 being the number of percentages in a section, and so forth)
My program reads the first line perfectly fine, but then skips over the 5, reads in "69" as the next amount of percentages in the next section, and completely screws up.
Here is my code:
//------------------------------------------------------------------------
// Name: ******
//
// Course: ********
//
// Purpose: To produce grading summaries by section, sorted by amount of
// each letter grade, highest/lowest percentages and the average, as well
// as the class averages, amount of sections and scores at the end
//
// Input: Prompted without text--the number of scores in a section, and
// then the percentages within the section. Inputs are recieved until EOF
//
// Output: Amount of each letter grade, highest/lowest percentages
// and the average, the class averages, amount of sections and scores
//------------------------------------------------------------------------
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
//Initialize global variables for each letter grade, as well as the
//'reset' variables so the variables can be reset after each section
//is completed computing
const int A = 90, B = 80, C = 70, D = 60, RESET = 0, LSRESET = 100;
//Initializes the Main Function
int main()
{
//Makes sure output does not exceed two decimal places
cout << fixed << showpoint << setprecision(2);
//Initialize all int and float variables
int loopCount = 0, percentage = 0, scoreCount = 0,
sectionSize = 0, sectionCount = 0, aCount = 0, bCount = 0, cCount = 0,
dCount = 0, fCount = 0, lowestScore = 100, highestScore = 0;
float averageScore = 0, total = 0, classAverage = 0;
//------------------------------------------------------------------------
//Primes the outer while loop
cin >> sectionSize;
cout << endl;
//Outer while loop for each section
while (cin)
{
sectionCount++;
cin >> percentage;
//Inner while loop to compute number of each letter grade,
//conditions are until the section size is equal to the loop count
while (loopCount != sectionSize)
{
//Simple if statements to see what letter grade
//the percentage inputted falls into
if (percentage >= A)
{
aCount++;
}
else if (percentage < A && percentage >= B)
{
bCount++;
}
else if (percentage < B && percentage >= C)
{
cCount++;
}
else if (percentage < C && percentage >= D)
{
dCount++;
}
else if (percentage < D)
{
fCount++;
}
//Increase the inner loop count and score count
//so it can be used for future calculations
loopCount++;
scoreCount++;
//Sets percentage to highest score or lowest score
//when applicable
if (percentage > highestScore)
{
highestScore = percentage;
}
if (percentage < lowestScore)
{
lowestScore = percentage;
}
//Compute the total score
total = percentage + total;
//Takes in the next number and sets it to the percentage
cin >> percentage;
}
averageScore = total / sectionSize;
//------------------------------------------------------------------------
//Cout statements to print number of each letter grade,
//as well as low/high scores and the average
cout << "Scores for section " << sectionCount << endl;
cout << "A's: " << aCount << endl;
cout << "B's: " << bCount << endl;
cout << "C's: " << cCount << endl;
cout << "D's: " << dCount << endl;
cout << "F's: " << fCount << endl;
cout << "Lowest Score: " << lowestScore << endl;
cout << "Highest Score: " << highestScore << endl;
cout << "Average Score: " << averageScore << endl << endl << endl;
//Resets variables that need to be reset after the loop is
//finished
aCount = RESET;
bCount = RESET;
cCount = RESET;
dCount = RESET;
fCount = RESET;
lowestScore = LSRESET;
highestScore = RESET;
averageScore = RESET;
total = RESET;
loopCount = RESET;
//Begins to compute the class average
classAverage = classAverage + total;
cin >> sectionSize;
}
//------------------------------------------------------------------------
//Finishes computing the class average
classAverage = classAverage / sectionCount;
//Cout statements with final calculations and termination
cout << "Total number of sections: " << sectionCount << endl;
cout << "Total number of scores: " << scoreCount << endl;
cout << "Class Average: " << classAverage << endl << endl;
cout << "That's all the sections!! Normal Termination." << endl;
return 0;
}
//------------------------------------------------------------------------
I have been working on trying to fix this issue for so long now, someone please help. I've asked my professor and my peers to which the problem still couldn't be solved.
Upvotes: 0
Views: 90
Reputation: 1194
The reason is because you're performin "cin >> percentage;" "sectionSize + 1" times.
So the corrected version would look like:
//------------------------------------------------------------------------
// Name: ******
//
// Course: ********
//
// Purpose: To produce grading summaries by section, sorted by amount of
// each letter grade, highest/lowest percentages and the average, as well
// as the class averages, amount of sections and scores at the end
//
// Input: Prompted without text--the number of scores in a section, and
// then the percentages within the section. Inputs are recieved until EOF
//
// Output: Amount of each letter grade, highest/lowest percentages
// and the average, the class averages, amount of sections and scores
//------------------------------------------------------------------------
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
//Initialize global variables for each letter grade, as well as the
//'reset' variables so the variables can be reset after each section
//is completed computing
const int A = 90, B = 80, C = 70, D = 60, RESET = 0, LSRESET = 100;
//Initializes the Main Function
int main()
{
//Makes sure output does not exceed two decimal places
cout << fixed << showpoint << setprecision(2);
//Initialize all int and float variables
int loopCount = 0, percentage = 0, scoreCount = 0,
sectionSize = 0, sectionCount = 0, aCount = 0, bCount = 0, cCount = 0,
dCount = 0, fCount = 0, lowestScore = 100, highestScore = 0;
float averageScore = 0, total = 0, classAverage = 0;
//------------------------------------------------------------------------
//Primes the outer while loop
cin >> sectionSize;
cout << endl;
//Outer while loop for each section
while (cin)
{
sectionCount++;
// cin >> percentage;
//Inner while loop to compute number of each letter grade,
//conditions are until the section size is equal to the loop count
while (loopCount != sectionSize)
{
cin >> percentage;
//Simple if statements to see what letter grade
//the percentage inputted falls into
if (percentage >= A)
{
aCount++;
}
else if (percentage < A && percentage >= B)
{
bCount++;
}
else if (percentage < B && percentage >= C)
{
cCount++;
}
else if (percentage < C && percentage >= D)
{
dCount++;
}
else if (percentage < D)
{
fCount++;
}
//Increase the inner loop count and score count
//so it can be used for future calculations
loopCount++;
scoreCount++;
//Sets percentage to highest score or lowest score
//when applicable
if (percentage > highestScore)
{
highestScore = percentage;
}
if (percentage < lowestScore)
{
lowestScore = percentage;
}
//Compute the total score
total = percentage + total;
//Takes in the next number and sets it to the percentage
// cin >> percentage;
}
averageScore = total / sectionSize;
//------------------------------------------------------------------------
//Cout statements to print number of each letter grade,
//as well as low/high scores and the average
cout << "Scores for section " << sectionCount << endl;
cout << "A's: " << aCount << endl;
cout << "B's: " << bCount << endl;
cout << "C's: " << cCount << endl;
cout << "D's: " << dCount << endl;
cout << "F's: " << fCount << endl;
cout << "Lowest Score: " << lowestScore << endl;
cout << "Highest Score: " << highestScore << endl;
cout << "Average Score: " << averageScore << endl << endl << endl;
//Resets variables that need to be reset after the loop is
//finished
aCount = RESET;
bCount = RESET;
cCount = RESET;
dCount = RESET;
fCount = RESET;
lowestScore = LSRESET;
highestScore = RESET;
averageScore = RESET;
total = RESET;
loopCount = RESET;
//Begins to compute the class average
classAverage = classAverage + total;
cin >> sectionSize;
}
//------------------------------------------------------------------------
//Finishes computing the class average
classAverage = classAverage / sectionCount;
//Cout statements with final calculations and termination
cout << "Total number of sections: " << sectionCount << endl;
cout << "Total number of scores: " << scoreCount << endl;
cout << "Class Average: " << classAverage << endl << endl;
cout << "That's all the sections!! Normal Termination." << endl;
return 0;
}
//------------------------------------------------------------------------
However, learning to use a debugger would be really useful. Or, an even simpler process: you could put prints in your code.
Upvotes: 1