Trouble with switch statement and functions

Switch won't execute functions under each case...

Page 1 of 1

3 Replies - 1330 Views - Last Post: 26 April 2009 - 01:04 AM Rate Topic: -----

#1 hk416  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 11-October 08

Trouble with switch statement and functions

Posted 25 April 2009 - 10:05 AM

I'm trying to create a simple 'switch statement' menu system for my program and has thus split it up into multiple files.
If I understand linking correctly, if you include a header file (test.h) in a standalone .cpp file (alone.cpp), and the same header file (test.h) is included in your main cpp file (main.cpp), the files should now be linked together.

Here is what i have:

//FILE: main.cpp

#include <iostream>
#include <string>

#include "FunctionPrototypes.h"
#include "SwitchEnumerations.h"
#include "MenuInformation.h"
using namespace std;

int main() {
	MenuInformation MainMenu;
	MainMenu.DisplayProgramHeadline();
	MainMenu.DisplayProgramInfo();
	MainMenu.DisplayMainMenuOptions();
	MainMenu.RetriveUserMenuInput();
	MainMenuSwitchTree(MainMenu.getUserMenuInput());
	return 0;
	}



//FILE: MenuSwitchTrees.cpp

#include "FunctionPrototypes.h"
#include "SwitchEnumerations.h"

void MainMenuSwitchTree(const int &_MainMenuChoice) {
	//Switch tree
	switch (_MainMenuChoice) {
		case OpenImageFile: {
			//Opens a user-defined image file
			void MainMenuOpenImageFile();
			}break;
		case DisplayOriginalImage: {
			//Displays a user-defined image file
			void MainMenuDisplayOriginalImage();
			}break;
		case ExitProgram: {
			//Exits the program without saving first
			void MainMenuExitProgram();
			}break;
		default: {
			//Displays an error message
			void MainMenuErrorMsg();
			}break;
		}
	}



The problem is when the program reaches the "MainMenuSwitchTree" function. Instead of executing the function the program just ends... The "MainMenuSwitchTree" function is located in the "MenuSwitchTrees.cpp" file and should be linked via the "SwitchEnumerations.h" file, right? The function prototypes is located in a seperate file "FunctionPrototypes.h". I get no error when compiling (VS2005). I've checked that the "getUserMenuInput()" function returns an integer value so that the function should have the right input... Anyone have any ideas to whats wrong?

Minor Update:
It seems that the switch statement is executed when an integer value is passed to the function, but however none of the functions under each case label is run. If I for example add the message "cout << " TEST SWITCH TREE FUNCTION #1" << endl;" under the case label "OpenImageFile", i can see the message on screen but the function "void MainMenuOpenImageFile();" is not run. The function "void MainMenuOpenImageFile();" is declared in the "FunctionPrototypes.h" header and the declaration is in the "MainMenuFunctions.cpp" file wich also has the "FunctionPrototypes.h" header defined...

Any help would be highly appreciated!

This post has been edited by hk416: 25 April 2009 - 03:35 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Trouble with switch statement and functions

#2 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Trouble with switch statement and functions

Posted 25 April 2009 - 04:39 PM

When you call a function you do not include its return type.

Try replacing this
void MainMenuDisplayOriginalImage();
with this
MainMenuDisplayOriginalImage();
(and the same with all your other function calls)

This post has been edited by janotte: 25 April 2009 - 04:40 PM

Was This Post Helpful? 0
  • +
  • -

#3 hk416  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 11-October 08

Re: Trouble with switch statement and functions

Posted 25 April 2009 - 06:46 PM

View Postjanotte, on 25 Apr, 2009 - 03:39 PM, said:

When you call a function you do not include its return type.

Try replacing this
void MainMenuDisplayOriginalImage();
with this
MainMenuDisplayOriginalImage();
(and the same with all your other function calls)


Thank You. That solved my issues. Now I'm going to find a sturdy, solid brick wall to smash my incredible dense melon into. Repeatedly. How in the **** could I've have missed something like that? ARGH! I need to take coffee breakes more often... :)
Was This Post Helpful? 0
  • +
  • -

#4 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Trouble with switch statement and functions

Posted 26 April 2009 - 01:04 AM

Yes it can be frightening how long you can stare at code and not see a problem and then someone walks past, glances at your screen over your shoulder and goes "Why is that, there?".

Good reminder of just how fallible the human mind and human perception really are.

Makes me feel slightly better to know I'm not alone in this :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1