using cin.ignore() correctly

Hello, I am trying to get rid of the stored value from my cin>> statement. My program is using a nested while loop to access different menu's; when the user selects 'B' to go back to the previous menu it will exit the loop and go back to the previous while loop.

My issue:
while (courseMgmtChoice != 'B')

The above while loop will not be accessed if user input is 'B'. Inside this loop is the menu I need to access. That is why I need to clear out the stored value of 'B'. Here is my program.

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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
  int main()
{
	//variables
	string tempCourseNum;		//stores the course number to manage
    char courseMgmtChoice = ' ';	//stores the user choice from the course management menu
    char stuMgmtChoice = ' ';	//stores the user choice from the student management menu
    string classTitle1,		
		   classTitle2,
		   classTitle3;
    
    //constuctors
    course course1("CS361", "Control Structures", MAX_CAPACITY);  //constructor with parameters
	course course2("CS362", "Data Structures", 10);  			  //constructor with parameters
    course course3; 	//default constructor
    
	course3.setCourseNum1();		//prompt and validate user for course number
	course3.setCourseTitle1();	//prompt user for course title
	
	//Choose Course Menu
	while (tempCourseNum != "E")
	{
		
		cout << "Choose a course to manage:" << endl;
		classTitle1 = course1.printCourse();
		classTitle2 = course2.printCourse();
		classTitle3 = course3.printCourse();
		cout << "Enter E to exit" << endl;
		tempCourseNum = courseNumPrompt();			//prompts for user selection and validates
		
		if (tempCourseNum == "E" || tempCourseNum == "e")					//exit if 'E'
			return 1;
			
			cout <<courseMgmtChoice << endl;
 			
		while (courseMgmtChoice != 'B')				//continue to loop until 'B' is entered, which goes back to previous menu
		{	
			if(courseMgmtChoice == 'S')				//if 'S' is selected, goto Student Management Menu
			{
				
				while(stuMgmtChoice != 'B')			//continue to loop until 'B' is entered, which goes back to previous menu	
				{
					stuMgmtChoice = stuMgmtMenu();	//prompt for student management menu
					studentManagementSelections(stuMgmtChoice);	//function to access other function based on user choice (switch statement)
				}//end outer while	
			}
			else
			{ 
				courseMgmtChoice = courseMgmtMenu();	//prompt for course management menu	
				//function to access other function based on user choice (switch statement)
				course1.courseManagementSelections(course1, course2, course3, courseMgmtChoice, tempCourseNum);
			}			
		}//end middle while	
							
	}//end inner while
	 		
    return 0;
}//end main 


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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
//***************************************************************************
string courseNumPrompt()
{
	//variables
	int cnt1 = 0;
	bool goodID = true;
	string id = "";
	
	do
	{
		cout << endl << endl;
		cout << "Enter course number (e.g. CT234): ";
	    cin  >> id;
	    cin.ignore(26, '\n');
	    //returns ID and exits program from main
	    if (id == "E" || id == "e")
    		return id;
    	//validates course number
	    goodID = courseNumValidity(id);
	    //if incorrect number, than re-prompt and re-validate
	    while (!goodID)
	    {
		    cout << endl;
	        cout << "INVALID ID FORMAT" << endl;
	        cout << "Must start with two characters (A to Z) and end with three digits" << endl;
	        cout << "Enter course number: ";
		    cin  >> id;
		    cin.ignore(26, '\n');
		    goodID = courseNumValidity(id);
		}
		//toupper all characters
    	for (cnt1; cnt1 < id.length(); ++cnt1)
        	id[cnt1] = toupper(id[cnt1]);
	}while (!goodID);
//*****************************************************************************
char stuMgmtMenu()
{
	char choice;
	
	cout << endl;
	cout << "**************************************" << endl;
	cout << "Student Management Menu\n\n"
		 << "\tP - Print Student IDs\n"
		 << "\tA - Add one student\n"
		 << "\tD - Drop one student\n"
		 << "\tB - Back to Course Management menu\n"
		 << "**************************************" << endl;
	cout << "Please enter the letter choice: ";
	cin >> choice;
	choice = toupper(choice);
	cin.clear();
	cin.ignore(100, '\n');
	//choice validation
	stuMgmtValidation(choice);
	
	return choice;
}
//************************************************************************************
char courseMgmtMenu()
{
	char choice;
	cin.clear();
	cout << endl;
	cout << "**************************************" << endl;
	cout << "Course Management Menu\n\n"
		 << "\tP - Print course data\n"
		 << "\tN - Modify course number\n"
		 << "\tT - Modify course title\n"
		 << "\tC - Modify course capacity\n"
		 << "\tI - Increment course capacity\n"
		 << "\tS - Student management\n"
		 << "\tB - Back to Choose Course Menu\n"
		 << "**************************************" << endl;
	cout << "Please enter the letter choice: ";
	cin >> choice;
	choice = toupper(choice);
	cin.clear();
	cin.ignore(100, '\n');
	//choice validation
	choiceMgmtValidation(choice);
	
	return choice;
}

outcome:
When I access the second menu (courseMgmtMenu()) and input 'B', it goes back to the //Choose Course Menu on line 23 of main. I choose a course and I stay in that loop without going to the next menu.

My thinking is that if I use cin.ignore(100, '\n') it will clear my choice so I can start again. Am I doing this wrong?

Please help, I have been stuck on this for a while. Thank you.
Last edited on
Have you tried just resetting the relevant selection variables after the while loops?
Alternatively try using do{) while( condition ); instead of while {}. This ensures that you process what's in the loop at least once. The problem is that the values are being held from the previous call in the variables. Either clearing the variable values, or insuring the loop is called (as the menu functions are called within the loops) should work.

You'll need to make the statement after the call to the menu function conditional, so as to only call if not 'B' etc.
Last edited on
I changed the while loops in main to a do-while and it fixed the process of going back and forth from "choose Course Menu" and courseMgmtMenu. When I select 'S' to go to the third menu, it only loops within that menu, even if you select 'B'.

Are you talking about resetting the variables within main? Like, using cin.ignore() within the main() loops? if so, I have tried that too but no luck. Maybe I'm putting my cin.ignore() in the wrong spots.
Topic archived. No new replies allowed.