It doesn't matter if you put your functions before or after main... but prototypes are a good idea.
A way to simplify your program:
Group the inputting of the first two numbers into a seperate function (or int main, if you prefer), instead of one copy in addition and one copy in subtraction. ie:
Code:
// Calc.cpp
// CompiledMonkey
#include "stdafx.h"
void add();
void subtract();
void input_two_numbers(float&, float&);
void input_two_numbers(float& x, float& y)
{
std::cout << "Enter first number: ";
std::cin >> x;
std::cout << "Enter second number: ";
std::cin >> y;
}
void add()
{
float x, y;
input_two_numbers(x,y);
std::cout << x << " + " << y << " = " << (x + y) << "\n";
}
void subtract()
{
float x, y;
input_two_numbers(x,y);
std::cout << x << " - " << y << " = " << (x - y) << "\n";
}
int main()
{
bool flag = true;
int option = 0;
do
{
// Display menu and get option
std::cout << "\n1: Add\n2: Subtract\n3: Exit\n\nPick an operation: ";
std::cin >> option;
switch(option)
{
case 1:
add();
break;
case 2:
subtract();
break;
case 3:
flag = false;
break;
default:
std::cout << "Bad choice!\n\n";
break;
}
}
while(flag);
return 0;
}