Clearing Bad Input Produces Odd Behavior

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
...
void displayAllData()
{
	std::printf("Earned\tTotal\tGrade\n--------------------------\n");
	for (unsigned long int 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();
	}
	else if (selection == "data")
	{
		displayAllData();
	}
	else if (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
Last edited on
Topic archived. No new replies allowed.