Help on calculator program

Hello,

I need some help with a calculator I need to create in C++ for my coursework.

Firstly, the user needs to be able to select an operator from +,-,*,/,%,n2

Next, they will input number one then number two, and the answer will be given depending on which operator was selected.

I managed to get a basic one working with case/swtich statements (I think thats what they are?) but the problem is, the design says it must include at least
2 procedures.

Can someone try to help me with this? Ill paste my code below of what i've got so far, but I just need to know where to put procedures?

Also, i'm not the best at variables either so I don't know if everything is exactly right.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
using namespace std;

int firstnumber;
int secondnumber;
int result;
char op;

int main()
{
    cout<<"Please select an Operator for the calculation: + or -\n";
    cin>>op;
    cout<<"Please enter the first number: ";
    cin>>firstnumber;
    cout<<"\n";
    cout<<"Please enter the second number: ";
    cin>>secondnumber;
    cout<<"\n";
    
    switch(op)
    {
          case'+':
                  result=firstnumber+secondnumber;
                  cout<<"Answer: "<<result<<endl;
                  break;
          case'-':
                  result=firstnumber-secondnumber;
                  cout<<"Answer: "<<result<<endl;
                  break;
                  }

system("PAUSE");
}


Any help would be greatly appreciated! Thanks guys
Make one procedure that asks the user for input, and another to calculate and/or display the result.
Thanks for the reply,

How exactly do I declare a procedure? Everytime I tried doing that I got a ton of errors.

Could you give me a quick example if you can?

Thanks again
Examples closely matching your needs can be found here:
http://www.cplusplus.com/doc/tutorial/functions/
Usually if someone describes a procedure in C++ terms they're referring to a function that doesn't return anything (i.e. void). Check out fun2code's link.
Last edited on
@iHutch105. I wasn't aware of the void return type implied by "procedure", which is a term I hadn't seen before. In this case the examples in the tutorial wouldn't be so suitable.

I see that the 2nd tutorial on functions
http://www.cplusplus.com/doc/tutorial/functions2/
gets into void returning functions and passing variables by reference.
Ill check out the links, thanks guys, much appreciated!
I think by definition, functions take an input and give an output.
By similar definitions, procedures aren't expected to give any output.

Generally, in C++, the two are synonymously known as functions. I tend to find that an older generation of programmers will often refer to void functions as procedures.
Topic archived. No new replies allowed.