|
||||||
| C and C++ C and C++ forum for discussing all forms of C except for C#. These languages are powerful low level languages used for creating Operating Systems, Device Drivers, compilers and much more. |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
| Sponsored Links |
|
|
|
|||||
|
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?
__________________
![]() |
|
|||||
|
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. Code:
#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;
}
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Windows XP Tricks & Tips!!!!..new ones. | pranky | Tutorials | 9 | 08-23-2008 04:22 PM |
| What Programs do you use for web design | TVDinner | Website Design | 113 | 07-17-2008 12:14 PM |
| my c programs | bobwrit | C and C++ | 4 | 10-01-2007 09:46 AM |
| Phone programs | Kaabi | General Programming | 3 | 07-06-2006 07:10 PM |
| Making programs for mobile phones | Crane | General Programming | 2 | 06-26-2006 04:53 PM |
| WingedPanther | ........ | 2753.6 |
| Xav | ........ | 2704 |
| Brandon W | ........ | 1702.32 |
| John | ........ | 1207.73 |
| marwex89 | ........ | 1175.24 |
| morefood2001 | ........ | 966.05 |
| dcs | ........ | 655.75 |
| Steve.L | ........ | 475.59 |
| orjan | ........ | 418.58 |
| Aereshaa | ........ | 383.54 |