Solved

classes and a menu system, need help!

Posted on 2007-11-27
15
232 Views
Last Modified: 2010-04-01
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.
0
Comment
Question by:jschmuff
  • 4
  • 4
  • 3
  • +2
15 Comments
 
LVL 53

Assisted Solution

by:Infinity08
Infinity08 earned 50 total points
ID: 20357731
This is a bit artificial (ie. not realistic), but I'll go with it ;)

You could create an Adder class that has a getTerms method that asks the user to input the two values to be added, and that has an add method which adds the two values together and returns the sum. You'll of course have to have two value members, one for each term.

Similar for subtracting.

Once you've created the classes, you can call their methods from the Options class.

Note that I said Options, and not options ... It's common to have an uppercase first letter for class names.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 20357736
Give it a try, and post what you have here ...
0
 
LVL 39

Assisted Solution

by:itsmeandnobodyelse
itsmeandnobodyelse earned 100 total points
ID: 20358206
Some remarks to the code above, first.

>>>>     char choice;

That must be a member variable of class options (or you have to return it from options::Choice).


>>>> class options
>>>> {
>>>>        void displayMenu();
>>>>        void displayChoice();
>>>> }

There is a semicolon missing at end of class definition. Then, if you have only private member functions, you won't able to call these functions. Better make the functions public (or add a public function 'void run();' which calls the  displayMenu and displayChoice). The 'char choice' should be a private member.

>>>> I would like to create class for the addition and subtraction functions
Better use only member functions like:

     void add();
     void subtract();

which ask for operands and show the results. You can use a helper function

    double getOperand(const char *);

which could be used by either add or subtract

   void options::add()
   {
         double d1 = getOperand("Left Operand:   ");
         double d2 = getOperand("Right Operand: ");
         // now make the addition and display operands plus result
   }

Regards, Alex

0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 40

Accepted Solution

by:
evilrix earned 200 total points
ID: 20358978
A dynamic polymorphic example code that you might find useful: -
#include <iostream>
#include <string>
 
struct Maths
{
	virtual char const * action() const = 0; 
	virtual int operator()(int lhs_, int rhs_) const = 0;
};
 
struct Add : Maths
{
	char const * action() const { return "+"; }; 
	int operator()(int lhs_, int rhs_) const
	{
		return lhs_ + rhs_;
	}
};
 
struct Sub : Maths
{
	char const * action() const { return "-"; }; 
	int operator()(int lhs_, int rhs_) const
	{
		return lhs_ - rhs_;
	}
};
 
 
void DoMaths(Maths const & 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

0
 
LVL 40

Expert Comment

by:evilrix
ID: 20359015
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

0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 20359319
>>>> 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.
0
 

Author Comment

by:jschmuff
ID: 20359819
itsmeandnobodyelse>>

Umm no this is not a homework assignment. I believe I stated that in my original post up top. Thank you.
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 20360166
>>>> 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?

0
 
LVL 40

Expert Comment

by:evilrix
ID: 20360179
>> 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
0
 
LVL 1

Expert Comment

by:yyyc186
ID: 20360757
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.

0
 

Author Comment

by:jschmuff
ID: 20362024
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.
0
 

Author Comment

by:jschmuff
ID: 20362623
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;
}
0
 

Author Comment

by:jschmuff
ID: 20362738
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!!)
}
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 20363800
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 ?
0
 
LVL 40

Expert Comment

by:evilrix
ID: 20363867
To be fair, all the answers the OP needs are in this thread -- all the notes, just not all in the right order :)
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
C++ get user from AD  (VS6) 7 72
Focus not getting shifted out of  editbox 2 62
please insert disk adobe master collection6 9 874
Precision Problem in C++ 7 31
When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question