Create A Menu Using A Switch Statement | C Programming Example

Поділитися
Вставка
  • Опубліковано 26 вер 2021
  • Example of creating a menu using a switch statement in C. Source code: github.com/portfoliocourses/c.... Check out www.portfoliocourses.com to build a portfolio that will impress employers!
  • Навчання та стиль

КОМЕНТАРІ • 24

  • @lordrodrig
    @lordrodrig Рік тому +3

    I was 2 days trapped in this and you came here with this video and save me thank you !

  • @naboulsikhalid7763
    @naboulsikhalid7763 Рік тому

    Hi, very thankful to your generosity, I have no word. THANK YOU

    • @PortfolioCourses
      @PortfolioCourses  Рік тому

      You're very welcome Naboulsi, thank you for your support! :-)

  • @Eduardo-iz4lo
    @Eduardo-iz4lo 2 роки тому +5

    Much Thanks, helped for my homework

    • @Eduardo-iz4lo
      @Eduardo-iz4lo 2 роки тому

      I am runnning in to errors, my code is not return up to the while so it can ask for my choice again

  • @ilovekyries05
    @ilovekyries05 2 місяці тому

    hey im 1 year late but i was wondering why you had to include ' int choice = 0' at the start instead of just int choice since choice was going to be a stored value

  • @rheamaebumanglag8809
    @rheamaebumanglag8809 2 роки тому +2

    What should I do if I clicked the wrong button, and I want to repeat the process again. What code should I use or add in the program?

    • @PortfolioCourses
      @PortfolioCourses  2 роки тому +1

      That's a good question, I think I would try to give the user an option to "go back" after selecting a different option. So for example entering a deposit amount of -1 to go back, as in this below (modified) code for that case:
      case 1:
      printf("Enter Amount (-1 to go back): ");
      scanf("%lf", &amount);
      if (amount != -1) balance += amount;
      break;

  • @janing2800
    @janing2800 Рік тому

    but how to write a condition that won't accept an entered amount in withdraw option if it will make the balance a negative number?

    • @PortfolioCourses
      @PortfolioCourses  Рік тому

      We could use an if-statement or maybe a do-while loop and if-statement inside the withdraw case, for example this video goes over using a do-while loop for input validation: ua-cam.com/video/IDIJXsZRqP4/v-deo.html

  • @runyuyu1426
    @runyuyu1426 Рік тому

    is there a way to stay on one option and require the user to input control to return to the menu page?

    • @PortfolioCourses
      @PortfolioCourses  Рік тому

      Great question! :-) Yes, we could create a similar loop below the "case" that we want the user to stay in, and only stop that loop once the user enters some particular value.

  • @azwanazman5115
    @azwanazman5115 2 роки тому

    why if I use exit(0), it says the exit was not declared? and how to exit from the switch, cause I want to ask whether the user wants to go the menu back or exit the program

    • @PortfolioCourses
      @PortfolioCourses  2 роки тому

      You need to include the stdlib.h library at the top of the file for exit to work, as it is defined in that library. The code for the problem is here: github.com/portfoliocourses/c-example-code/blob/main/switch_menu.c. And here is some documentation of the exit function being in the stdlib.h library: www.tutorialspoint.com/c_standard_library/stdlib_h.htm.

    • @azwanazman5115
      @azwanazman5115 2 роки тому

      @@PortfolioCourses thank you vary much : )

  • @mashalmokarar4817
    @mashalmokarar4817 2 роки тому

    Awesome

  • @ilyassilyass9608
    @ilyassilyass9608 Рік тому

    What does the while(true) means or does?

    • @PortfolioCourses
      @PortfolioCourses  Рік тому

      Great question! :-) So a while loop will run so long as the condition is true, so for example:
      int x = 0;
      while (x < 5)
      {
      x++;
      }
      will run so long as the condition "x < 5" is true. Given that we increment x in the loop body with x++, the loop condition will eventually be false.
      But with "while (true)" we have a loop with the *condition* "true". This means the condition will NEVER be false, it will always be true because it's literally just the expression "true". So this type of loop will execute "forever" unless we use the break keyword in the loop body which breaks control flow out of the loop. See this video on the break keyword: ua-cam.com/video/02ZCvvC2GME/v-deo.html.

    • @ilyassilyass9608
      @ilyassilyass9608 Рік тому

      @@PortfolioCourses thank you

    • @PortfolioCourses
      @PortfolioCourses  Рік тому

      @@ilyassilyass9608 You're welcome! 🙂

    • @Username92381
      @Username92381 9 місяців тому

      While (true) basically means suicide

  • @Meiyourenn
    @Meiyourenn 2 роки тому +1

    is there anyway you can do this but with these choices?
    Enter 3 grades
    Show average (with the 3 grades) and letter grade
    Show highest grade
    Show lowest grade
    Exit