Link to home
Start Free TrialLog in
Avatar of jschmuff
jschmuffFlag for United States of America

asked on

classes and a menu system, need help!

Ok bear with my I am just coming up with this since I don't exactly know how to code this yet. I am messing with forms of classes and such for making a simple menu system but all the functions happen in a different class so in main there wouldn't be a whole lot there.

my options.h file:
class options
{
       void displayMenu();
       void displayChoice();
}

my options.cpp file:
void options::displayMenu()
{
       cout << "(A)ddition \n";
       cout << "(S)ubtraction \n";
}
void options::Choice()
{
      char choice;
      cout << "Enter choice: ";
      cin >> choice;
      cout << endl;
}

okay.... now that I got the menu created and the input portion created I would like to create class for the addition and subtraction functions and link that to my user input from class options. Anyone got any ideas on how I would do that. Can anyone help me out maybe an example this is not a school thing so an answer is ok. My term is coming down to the end and I am trying to practice on stuff so coming next term I am not an artard and have to reference to my book all the time for basic stuff. Thank you.
SOLUTION
Avatar of Infinity08
Infinity08
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Give it a try, and post what you have here ...
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The same as a static polymorphic example

NB. Neither case performs any validation on input data -- this was left out on purpose so as not to over complicate the examples.
#include <iostream>
#include <string>
 
struct Add
{
	char const * action() const { return "+"; }; 
	int operator()(int lhs_, int rhs_) const
	{
		return lhs_ + rhs_;
	}
};
 
struct Sub
{
	char const * action() const { return "-"; }; 
	int operator()(int lhs_, int rhs_) const
	{
		return lhs_ - rhs_;
	}
};
 
template <typename mathsT>
void DoMaths()
{
	mathsT maths;
 
	int lhs = 0;
	int rhs = 0;
 
	std::cout << "Enter first number: ";
	std::cin >> lhs;
 
	std::cout << "Enter second number: ";
	std::cin >> rhs;
 
	std::cout
		<< lhs
		<< " "
		<< maths.action()
		<< " "
		<< rhs
		<< " = "
		<< maths(lhs, rhs)
		<< std::endl;
}
 
 
int main()
{
	bool bQuit = false;
 
	do
	{
		std::cout
			<< std::endl
			<< "Choose: -"
			<< std::endl
			<< "   [A]dd"
			<< std::endl
			<< "   [S]ubtract"
			<< std::endl
			<< "   [Q]uit"
			<< std::endl
			<< std::endl
			<< ":> ";
 
		std::string sOpt;
		std::cin >> sOpt;
 
		if(!sOpt.empty())
		{
			switch(sOpt[0])
			{
			case 'a':
			case 'A':
				DoMaths<Add>();
				break;
			case 's':
			case 'S':
				DoMaths<Sub>();
				break;
			case 'q':
			case 'Q':
				bQuit = true;
				break;
			}
		}
	}
	while(!bQuit);
 
	return 0;
}

Open in new window

>>>> this was left out on purpose so as not to over complicate the examples.
That is a homework assignment, and I can't see a valid reason for posting full code samples.
Avatar of jschmuff

ASKER

itsmeandnobodyelse>>

Umm no this is not a homework assignment. I believe I stated that in my original post up top. Thank you.
>>>> this is not a school thing so an answer is ok.
Sorry, I overread that.

Are you satisfied with any of the answers or still waiting for more?

Do you wnat to use classes for adding/subtracting/.... or simple call member functions?

>> That is a homework assignment, and I can't see a valid reason for posting full code samples.

1. Clearly stated in the original question, "Can anyone help me out maybe an example this is not a school thing".
2. The example I posted is NOT directly representative of what the OP is after, it is a working example that can be used to figure out what he is looking to do
3. If you look at other similar posts, others (including moderators) have also given code examples
Avatar of yyyc186
yyyc186

There used to be many C++ frameworks which did this under DOS, WINDOWS, OS/2, MAC, etc.  One used to be Zinc Application Framework (now owned by WindRiver I believe).  Zinc worked on all above mentioned platforms.

Troll Tech has the Qt library, an Open Source version of which is available for most Linux distros.

Under DOS there was the GreenLeaf DataWindows library.

Of course, there is always YACL which comes as full source and there is even a book published on how to use.

A Web search on ASK.COM for "cross platform GUI library" will yield dozens of entries.  Some free, some commercial.  Most of the free ones have menu's where things can be dynamically added.

I don't know your intention with this project.  If you are doing it to write your own GUI library for personal education reasons, then you probably just want to find the YACL book on eBay or Barnes & Noble and read about its design/usage, then develop your own.

If you are trying to write an application which needs this functionality, then pick a library and move forward.

This is alot of information to go over I will thoroughly look over all of it and get back to everyone once I come up with more of a complete code for this. The answer to the question from itsmeandnobodyelse I would like to have them as classes.
Okay I have been reading over all of this and didn't really come up with anything I am looking for a way to use the choice input from class Options to access the class math either add or subtract...

the main would look something like: (I don't want the main to do the actual work in the program just the classes)

int main()
{
         Options options;

         options.displayMenu;
         options.displayChoice;

         return 0;
}
Okay I have a code so far lets just forget the math thing for this example:

my menu.h file:

class Menu
{
public:
      void displayMenu();
      void displayChoice();
};

my menu.cpp file:

#include <iostream>
#include "menu.h"

using namespace std;

void Menu::displayMenu()
{
      cout << "*******MENU*******\n";
      cout << "(A)ddition\n";
      cout << "(S)ubtraction\n";
      cout << "(Q)uit\n";
}

void Menu::displayChoice()
{
      char choice;
      cout << "Choice: ";
      cin >> choice;
      cout << endl;
}

my main.cpp file:

#include <iostream>
#include "menu.h"
#include "menufunctions.h"

using namespace std;

int main()
{
      Menu menu;

      menu.displayMenu();
      menu.displayChoice();

      return 0;
}

***(Remember forget the equations for math)

my file menufunction.h file:

class Menufunctions
{
        void addition();
        void subtraction();
        void quit();
};

my file menufunctions.cpp file:

#include <iostream>
#include "menufunctions.h"

using namespace std;

void Menufunctions::addition()
{
          cout << "You have selected Addition...\n";
}
void Menufunctions::subtraction()
{
          cout << "You have selected Subtraction...\n";
}
void Menufunctions::quit();
{
         (not sure what to put in here!!)
}
Why did you accept the answers if your question isn't solved yet ? Why with a B grade ?

>> Okay I have a code so far lets just forget the math thing for this example:

And ? Any problem with it ?
To be fair, all the answers the OP needs are in this thread -- all the notes, just not all in the right order :)