Unable to access options

Jul 23, 2018 at 5:51pm
closed account (9h5X216C)
Hi, I'm new to c++ and really need your help to program as stated by topic.

I can't seem to get my main menu switch case to work. Whenever I select '1', its suppose to do OPTION1 functions... but it keeps showing output: Exiting Program

Please help! Thank you.
Last edited on Jul 31, 2018 at 2:04pm
Jul 23, 2018 at 6:00pm
switch statements need a break after each one or it uses fall-through, which can be desired, but you don't appear to want that 'feature'.
so what happens is that when you type 1, it does all 8 cases because you did not break. (if you put in 3, it would do cases {3,4,5,6,7,8}

put a break in each case statement, and it should clear up.
Last edited on Jul 23, 2018 at 6:00pm
Jul 23, 2018 at 6:22pm
closed account (E0p9LyTq)
You had the correct idea for a menu, you were simply missing a few items:
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
#include <iostream>

// functions
void displayMenu(int&);

int main()
{
   int choice = 0;
   bool exit  = false;

   while (!exit)
   {
      displayMenu(choice);
      std::cout << '\n';

      switch (choice)
      {
      case 1:
         std::cout << "Choice 1.\n\n";
         break;

      case 2:
         std::cout << "Choice 2.\n\n";
         break;

      case 3:
         std::cout << "Choice 3.\n\n";
         break;

      case 4:
         std::cout << "Choice 4.\n\n";
         break;

      case 5:
         std::cout << "Choice 5.\n\n";
         break;

      case 6:
         std::cout << "Choice 6.\n\n";
         break;

      case 7:
         std::cout << "Choice 7.\n\n";
         break;

      case 8:
         std::cout << "Choice 8.\n\n";
         exit = true;
         break;

      default:
         std::cout << "*** Option not found. Please try again. ***\n\n";
      }
   }

   std::cout << "Good-bye.\n";
}

void displayMenu(int& choice)
{
   std::cout << "Welcome to Weather Information Processing System!\n\n";

   // displaying options for the menu
   std::cout << "1)	Read in and process a configuration file\n";
   std::cout << "2)	Display city map\n";
   std::cout << "3)	Display cloud coverage map (cloudiness index)\n";
   std::cout << "4)	Display cloud coverage map (LMH Symbols)\n";
   std::cout << "5)	Display atmospheric pressure map (pressure index)\n";
   std::cout << "6)	Display atmospheric pressure map (LMH Symbols)\n";
   std::cout << "7)	Show weather forecast summary report\n";
   std::cout << "8)	Quit\n\n";

   std::cout << "PLease enter your choice: ";

   std::cin >> choice;
}

Welcome to Weather Information Processing System!

1)      Read in and process a configuration file
2)      Display city map
3)      Display cloud coverage map (cloudiness index)
4)      Display cloud coverage map (LMH Symbols)
5)      Display atmospheric pressure map (pressure index)
6)      Display atmospheric pressure map (LMH Symbols)
7)      Show weather forecast summary report
8)      Quit

PLease enter your choice: 5

Choice 5.

Welcome to Weather Information Processing System!

1)      Read in and process a configuration file
2)      Display city map
3)      Display cloud coverage map (cloudiness index)
4)      Display cloud coverage map (LMH Symbols)
5)      Display atmospheric pressure map (pressure index)
6)      Display atmospheric pressure map (LMH Symbols)
7)      Show weather forecast summary report
8)      Quit

PLease enter your choice: -12

*** Option not found. Please try again. ***

Welcome to Weather Information Processing System!

1)      Read in and process a configuration file
2)      Display city map
3)      Display cloud coverage map (cloudiness index)
4)      Display cloud coverage map (LMH Symbols)
5)      Display atmospheric pressure map (pressure index)
6)      Display atmospheric pressure map (LMH Symbols)
7)      Show weather forecast summary report
8)      Quit

PLease enter your choice: 8

Choice 8.

Good-bye.
Topic archived. No new replies allowed.