I created a program to calculate a cumulative grade by entering points earned out of points total. My function branch() produces odd behavior when called. Sometimes, tt fails to recognize the first input. Here is the section of my program that is problematic.
...
void displayAllData()
{
std::printf("Earned\tTotal\tGrade\n--------------------------\n");
for (unsignedlongint number = 0; number < count; number++)
{
std::cout << numeratorsArray[number] << "\t" << denominatorsArray[number] << "\t" << (scoresArray[number] * 100) << '%' << std::endl;
}
std::printf("--------------------------\n");
std::cout << "Points: " << numeratorsSum << " out of " << denominatorsSum << std::endl;
std::cout << "Grade: " << (numeratorsSum / denominatorsSum) * 100 << '%' << std::endl;
branch();
}
void branch()
{
std::printf("\n\nEnter \"go\" to input more data.\nEnter \"data\" to display collected data.\nEnter \"exit\" to close this program.\n");
std::string selection;
std::cin.clear();
while (std::cin.get() != '\n')
continue;
//PROBLEM AROUND HERE
std::getline(std::cin, selection);
if (selection == "go")
{
getData();
}
elseif (selection == "data")
{
displayAllData();
}
elseif (selection == "exit")
end();
else
{
std::printf("ERROR: INVALID SELECTION\n");
branch();
}
}
...
Excerpt of program output with sample data:
Earned Total Grade
--------------------------
99 100 99%
80 85 94.1176%
--------------------------
Points: 179 out of 185
Grade: 96.7568%
Enter "go" to input more data.
Enter "data" to display collected data.
Enter "exit" to close this program.
go
go