Reputation: 65
I am using nested switch statements in my program. For some reason, the inner switch statements don't seem to work.
Expected Behaviour: Upon starting the program, the user is presented with 5 options; A: SUBJECTS, B: STUDENTS, C: ENROLMENT, D: GRADES_RECORD E: EXIT
. Of these options, A,B,C and D contain further switch statements. Lets take for example case A: SUBJECTS
. Once case A has been selected, the user is presented with a further 3 options; A: ADD_SUBJECTS, B: EDIT_SUBJECTS and C: DELETE_SUBJECTS
. The user then selects one of these options after which some code is executed. Lets say for example the user selects A, he will be taken through the procedure to add a subject to the subject pool.
Actual Behaviour: Upon starting the program, the initial prompt A: SUBJECTS, B: STUDENTS, C: ENROLMENT, D: GRADES_RECORD E: EXIT
is displayed prompting the user to select a case. Once the user selects the case, he is shown the second prompt which is relevant to the option that has been selected. E.g. If the user selected A at the initial prompt, the second prompt displayed is A: ADD_SUBJECTS, B: EDIT_SUBJECTS and C: DELETE_SUBJECTS
. Perfect, that's how I want this to work. But here's the problem. When the user now selects an option, no matter what option has been selected, the program acts as if the ADD_SUBJECT
case was chosen and goes through the code for that. That's for the parent case being A: SUBJECTS
. As for the rest of the parent cases, the relevant child prompt is shown but any input/selection of the child case results in the initial prompt A: SUBJECTS, B: STUDENTS, C: ENROLMENT, D: GRADES_RECORD E: EXIT
being shown, it doesn't access any of the inner cases at all.
Please refer to code below:
static void Main(string[] args)
{
Console.Title = "Students Record";
LoadJson();
Run();
}
static void Run()
{
while (true)
{
var consoleInput = ReadFromConsole(MainPrompt);
if (string.IsNullOrWhiteSpace(consoleInput)) continue;
Execute(consoleInput);
}
}
public static string ReadFromConsole(string prompt)
{
Console.Write(prompt);
return Console.ReadLine();
}
static void Execute(string userChoice)
{
switch(userChoice.ToUpper())
{
case A:
{
ReadFromConsole(SubjectPrompt);
switch (userChoice.ToUpper())
{
case "A"/*ADD_SUBJECT*/:
{
AddSubject();
break;
}
case "B"/*EDIT_SUBJECT*/:
{
Console.WriteLine("edit subject");
break;
}
case "C"/*DELETE_SUBJECT*/:
{
Console.WriteLine("delete subject");
break;
}
}
break;
}
case B:/*{some code}*/
case C:/*{some code}*/
case D:/*{some code}*/
case E:/*{some code}*/
}
}
Cases B,C and D are of the same format.
Can anyone see why for parent case A it always prompts to add a subject regardless of the what case is selected and for the rest of the parent cases it just displays the initial prompt?
Thanks in advance
Upvotes: 0
Views: 651
Reputation:
The variable userChoice is still equal to "A" in the second switch statement, just erase its value or create a new variable and pass that new value to the second switch:
...
var newInput = ReadFromConsole(SubjectPrompt);
switch (newInput.ToUpper())
{
...
}
Upvotes: 1
Reputation: 8868
Method ReadFromConsole
return a string
but you aren't assign it to userChoice
, that maintain the value it has when entering the method.
case "A":
{
userChoice = ReadFromConsole("Subject");
switch (userChoice.ToUpper())
case "A"/*ADD_SUBJECT*/:
{
AddSubject();
break;
}
case "B"/*EDIT_SUBJECT*/:
{
Console.WriteLine("edit subject");
break;
}
case "C"/*DELETE_SUBJECT*/:
{
Console.WriteLine("delete subject");
break;
}
}
break;
}
Upvotes: 3