Jump to content

A way to combine programs...

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1
Unknown2dark

Unknown2dark

    Newbie

  • Members
  • PipPip
  • 18 posts
I was just wondering, can't i make a program that in the beginning ask me which program i want, something like i have a + Calculator and a - one, so how about if i can make a program that at the beginning says

"Choose the program you want:-

Addition = h
Subtraction = g

Enter the letter of your program:- ...."

Can that happen?

Posted Image


#2
v0id

v0id

    Retired

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,936 posts
What do you mean?
Do you want to make some kind of menu in your console application, or are you trying to start up other applications/processes?

#3
Unknown2dark

Unknown2dark

    Newbie

  • Members
  • PipPip
  • 18 posts
Dunno the difference but, i just want when i open the program, instead of only having one function, it can be used for many functions, and that's done by a list in the beginning, is that easier to understand, or not?

Posted Image


#4
v0id

v0id

    Retired

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,936 posts
Hmm, I think you mean a menu. You've to give the user multiple choices of what to do, right? For example if it's a console-based calculator, you want to have the user to choice between addition, subtraction, etc.

A little example, using switch-statement.
An if-construction could had been used to.
#include <iostream>

void addition()
{
	int a, b;
	
	std::cout << "Input two numbers. " << std::endl;
	std::cout << " Number 1: ";
	std::cin  >> a;
	std::cout << " Number 2: ";
	std::cin  >> b;
	std::cout << " Result:   " << a+b << std::endl;
}

void subtraction()
{
	int a, b;
	
	std::cout << "Input two numbers. " << std::endl;
	std::cout << " Number 1: ";
	std::cin  >> a;
	std::cout << " Number 2: ";
	std::cin  >> b;
	std::cout << " Result:   " << a-b << std::endl;
}

int main()
{
	char choice = 0;
	
	while(choice != 'Q')
	{
		std::cout << std::endl;
		std::cout << " ---- " << std::endl;
		std::cout << std::endl;
		
		std::cout << "What do you want to do?" << std::endl;
		std::cout << " 1. Addition" << std::endl;
		std::cout << " 2. Subtraction" << std::endl;
		std::cout << " Q. Exit" << std::endl;
		std::cout << ">> ";
		std::cin  >> choice;
		
		switch(choice)
		{
			case '1':
				addition();
				break;
			case '2':
				subtraction();
				break;
			case 'Q':
				break;
			default:
				std::cout << "Invalid option, try again!" << std::endl;
				break;
		}
	}

	return 0;
}

Tell me if that weren't what you meant.

#5
Unknown2dark

Unknown2dark

    Newbie

  • Members
  • PipPip
  • 18 posts
That was just what i want, really many thanks, i will try to figure out how to make my own now, thanks btw, you've been a great help since i joined..

Posted Image