C# Menu and SubMenu on Console-Troubles on Menu Return

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



C# Menu and SubMenu on Console-Troubles on Menu Return



I am new at programming and I'm trying to create a Menu that has inside Submenus. The main Menu has all the options to procede while the submenu has a CRUD for each options inserted.



However the submenu I've done(Animals)doesn't work neither of the visualize by id methods and it goes like this to the following crud methods for the submenus. Also when a submenu complets its tasks it should return to the initial Submenu options, allowing to return to main Menu by pressing zero. Which is not happening, though I tryied doing things differently. I'm not sure if it's the switch statement which wrong or if it's the methods call.



Sorry if the code is a little bigger than usual:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace menu

class Program

public static int id = 1;
enum animalHeader id, name, client_name, type_animal ;
enum clientHeader id, name, client_surname, adrress ;
static void Main(string args)

string[,] animal = new string[20, 4];
string[,] client = new string[20, 6];

do MenuOptions(animal); while (true);


static void MenuOptions(string[,] animal)
userChoice > 2);

Console.Clear();

switch (userChoice)

case 1:
menuAnimal(animal);
menuReturn(animal);
break;
case 2:
//menuClient(client);
mainMenu();
break;
case 0:
Environment.Exit(0);
break;
default:
Console.WriteLine("Try again!!");
break;


static void menuAnimal(string[,] animal)

static void mainMenu()

Console.Clear();
Console.ReadKey();


static void menuReturn(string[,] animal)

Console.Clear();

do menuAnimal(animal); while (true);



static int generateId()

return id++;

static int getInsertIndex(string[,] matrix)

for (int j = 0; j < matrix.GetLength(0) - 1; j++)

if (string.IsNullOrEmpty(matrix[j, 0])) return j;


return -1;

static void insertData(string[,] matrix)

int id = generateId();
int n = getInsertIndex(matrix);

matrix[n, 0] = Convert.ToString(id);

for (int j = 1; j < matrix.GetLength(1); j++)

do

Console.Write($"Insert Enum.GetName(typeof(animalHeader), j): ");
matrix[n, j] = Console.ReadLine();
while (String.IsNullOrEmpty(matrix[n, j]));


static int searchId(string[,] matrix)

int choosenId, index = -1;

do

Console.Write("Insert ID to continue: ");

while (!int.TryParse(Console.ReadLine(), out choosenId));


for (int i = 0; i < matrix.GetLength(0); i++)

if (Convert.ToString(choosenId) == matrix[i, 0])

index = i;



return index;

static void visualizeByid(string[,] matrix)

int pos = searchId(matrix);

if (pos != -1)

for (int i = pos; i < pos + 1; i++)

for (int j = 0; j < matrix.GetLength(1); j++)

Console.Write($"matrix[i, j]t");

Console.WriteLine();


else Console.WriteLine("Wrong Id");

static void updateById(string[,] matrix)

int pos = searchId(matrix);
if (pos != -1)

for (int i = pos; i < pos + 1; i++)

for (int j = 1; j < matrix.GetLength(1); j++)

Console.Write($"Insert Enum.GetName(typeof(animalHeader), j): ");
matrix[i, j] = Console.ReadLine();

Console.WriteLine();


else Console.WriteLine("Id does not exist");

static void deleteByid(string[,] matrix)

int pos = searchId(matrix);
if (pos != -1)

for (int i = pos; i < pos + 1; i++)

for (int j = 0; j < matrix.GetLength(1); j++)

matrix[i, j] = null;



else Console.WriteLine("Id does not exist");

static void listData(string[,] matrix)

for (int i = 0; i < matrix.GetLength(0); i++)

for (int j = 0; j < matrix.GetLength(1); j++)

Console.Write($"tmatrix[i, j]t");

Console.WriteLine("nt");








1 Answer
1



The first problem I can spot is in the menuAnimal method. Check what happens when you enter 0, for example by stepping through the code with debugger.


menuAnimal



When user enters 0, the first do-while condition passes (as 0 is a number and is not smaller than 0 and not larger than 5), but in the following switch statement you do not have a case for 0, so the method stops executing and just redraws, because execution continues to menuReturn(animal); in MenuOptions.


do-while


switch


case


menuReturn(animal);


MenuOptions



The next main problem here is the menuReturn method. Observe that once you get in this method, you can never "escape". It always redraws the animals menu whatever happens and while(true) ensures that it will happened indefinitely. Because there is no break condition inside, it will just keep on going.


menuReturn


while(true)


break



I will suggest a different approach here, although there are many options you have, so you are free to use a different solution that suits you.



First get rid of the menuReturn method and put this "menu output loop within the menu methods themselves. In pseudo code this would be:


menuReturn


static void MenuMethod()

while (true)

userInput = read user input for example using do..while as in original code

goBack = false
switch (userInput)

case optionA:
doSomething();
goBack = true; //set go back to true if you want go up a level
break;
case optionSubmenu:
submenu(); //go to a submenu, which will start its own loop
//after submenu is closed (goBack = true), execution will return to this level
break;

if (goBack) return; //end execution of this menu




So briefly in your case it could look like this:


static void MenuOptions(string[,] animal)

while (true)
userChoice < 0



Similar goes for animals menu:


static void MenuAnimal(string[,] animal)

while ( true )







By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard