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.
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
//***************************************************************************
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.
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.
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.