Switch statement not executing in proper order.

Hello, my switch statement is not responding to what the proper input should be and it's switching around my menus. I have no idea why it's doing this, and I'm beyond frustrated.

Here's my main followed by two headers.

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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <fstream>
#include "menuOptions.h"
#include "mathFunctions.h"

using namespace std;
// function prototypes
int mainMenu(string prompt);
int yearlyInformationMenu(string prompt);
int historicalInformationMenu(string prompt);

int main ()
{

    // variable declarations :
    int option;
    int menuoption;
    int yearInfoMenuOption;
    int hisInfoMenuOption;
    int year;
    double temp;

    ifstream in_stream;   //create a file object
    in_stream.open("RocTempDataV1.txt");


 //check if file has been opened
    if( ! in_stream.is_open( ) )

    {   //file not being opened
        cout << "Unable to open file - does not exit in current directory" << endl;
        exit(0);
    }



   mainMenu(menuoption);
    // switch statement for menu options.
       switch (menuoption)
    {
        case 1:
        // choice for menuoption 1
        yearlyInformationMenu(menuoption);
        break;

        case 2:
    // choice for option 2
        historicalInformationMenu(menuoption);
        case 3:
    //exits the main menu
    exit(menuoption);

        break;


        default:
        // if invalid number is entered.
        cout << " Error Invalid number this is the switch default";

    }

   yearlyInformationMenu(menuoption);

    switch (menuoption)
    {
        case 1:
    // choice for option 1

        break;

        case 2:
    // choice for option 2

        case 3:
    // choice for option 3
        break;

        case 4:
    // choicd for option 4

        break;

        case 5:
    // choice for option 5
       mainMenu(menuoption);
        break;

        default:
        cout << " Error Invalid number ";

    }

 historicalInformationMenu(menuoption);
    switch (menuoption)
    {
        case 1:
    // choice for option 1

        break;

        case 2:
    // choice for option 2

        case 3:
    // choice for option 3
        break;

        case 4:
    // choice for option 4
       mainMenu(menuoption);

        break;



        default:
        cout << " Error Invalid number ";

    }


}



Header 1 :
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
77
78
79
80
81
82
83
84
85
86
87
88
 #include <string>
using namespace std;
//------------------------
//Name: mainMenu
//Purpose: lists main menu options
//Parameters: menuoption.
//Returns:  integer for menu selection.
//------------------------
int mainMenu(int& prompt)
{

    cout <<setw(12) << "Options" << setw(32) << " Description" <<  endl;
    cout <<setw(9)<< "1" << setw(52) << " Calculate yearly information" << endl;
    cout <<setw(9)<< "2" << setw(56) << " Calculate historical information" << endl;
    cout <<setw(9)<< "3" << setw(40) << " Exit the program" << endl;

    cin >> prompt;
    return (prompt);
}
//------------------------
//Name: mainMenuOption1
//Purpose: lists main menu options for yearly information
//Parameters: prompt
//Returns:  integer for menu selection.
//------------------------
int yearlyInformationMenu(int& prompt)
{

    cout <<setw(12) << "Options" << setw(32) << " Description" <<  endl;
    cout <<setw(9)<< "1" << setw(52) << " Display the monthly temperature for all months in the year" << endl;
    cout <<setw(9)<< "2" << setw(50) << " Calculate average temperature for the entire year" << endl;
    cout <<setw(9)<< "3" << setw(40) << " Display highest monthly temperature for the year"  << endl;
    cout <<setw(9)<< "4" << setw(40) << " Display lowest monthly temperature for the year"   << endl;
    cout <<setw(9)<< "5" << setw(10) << " Return to Main Menu" << endl;


      cin >> prompt;
        // error checking for valid input
     if (prompt <=0 || prompt >=5)
    {

    cout << "Error invalid input\n" << endl;
    cout << "Please enter another option : " << endl;
    cin  >> prompt;

    }


    return (prompt);
}
//------------------------
//Name: mainMenuOption2
//Purpose: lists main menu options for historical information.
//Parameters: prompt
//Returns:  integer for menu selection.
//------------------------
int historicalInformationMenu(int& prompt)
{

    cout <<setw(12) << "Options" << setw(32) << " Description" << endl <<  endl;

    cout << setw(9) << "1" << setw(71) << " Find the year with lowest average temperature for all 12 months. " << endl;
    cout << setw(5) << " " << setw(69) << " Display the monthly temperature for all months in the year." << endl << endl;

    cout << setw(9)  << "2" << setw(60) << "Find the lowest monthly temperature for all the years." << endl;
    cout << setw(15) << " " << setw(30) <<"Display the year and monthly temperature." << endl << endl;

    cout << setw(9)  << "3" << setw(64) << " Find the highest monthly temperature for all of  the year."<< endl;
    cout << setw(15) <<" "  << setw(33) << "Display the year and monthly temperature." << endl<< endl;

    cout <<setw(9) << "4" << setw(25) <<  "Return to Main Menu"  << endl << endl;


      cin >> prompt;

      // error checking for valid input
     if (prompt <=0 || prompt >=4)
    {

    cout << "Error invalid input\n" << endl;
    cout << "Please enter another option : " << endl;
    cin  >> prompt;

    }


    return (prompt);
}


Header 2

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
#include <string>
#include <math.h>
using namespace std;

//------------------------
//Name: calcAvgTmp
//Purpose: Calculate average temperature for the entire year
//Parameters: Monthly tempeatures for given year
//Returns:  average tempt for year
//------------------------
double calcAvgTmp (int monthTmp )
{
    // Variable Declaration
  int const NUMBEROFMONTHS = 12;
    double avgMonthTmp = 0;
    double sumtemp = 0;

    // must figure out how to add up all temps for loop?

    // Calculates average for tmp.
    avgMonthTmp = sumtemp / NUMBEROFMONTHS;
    cout << "Average monthly temp is : " << avgMonthTmp << endl;

}
What order is it going through the menus? And if you stepped through with a debugger you might be able to find the problem yourself.
It starts off in the main menu where you're listed 3 options, then no matter what you enter it skips to the yearly menu then historical.
Well, according to how you've written your program, you will always, without exception, at least visit the main menu (on line 42), then the yearly (on 67), then the historical (on 98). I suspect what you meant to do is to do those second sets of checks (for the sub-menus) after entering one of the cases from the main menu. Then put a loop around that to return to the main menu so long as the user hasn't asked to exit.
You're correct sir. Any comment on how to fix this?
Line 53 of mainMenu, you have no break statement after you call historicalInformationMenu, therefore you will always exit the program after calling historicalInformationMenu.

At lines 90 and 115, you have recursive calls to mainMenu.

Also, after you fall out of each switch statement, you fall through to the next menu.

In main, you want a loop that continues until the user chooses to exit the program.
In each of the submenu, you want to loop until the user chooses to return to the main menu. When you exit out of the submenu, simply return out of the function. Don't nest a call to mainMenu.


Topic archived. No new replies allowed.