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.
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <fstream>
#include "menuOptions.h"
#include "mathFunctions.h"
usingnamespace 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 ";
}
}
#include <string>
#include <math.h>
usingnamespace 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
intconst 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;
}
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.
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.