Solved

classes and a menu system, need help!

Posted on 2007-11-27
15
237 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

687 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