How can I convert this to use Functions?

So, I made a calculator using a switch and do/while loops..but now I want to convert it to use Functions and I'm kind of confused on exactly HOW to do this..

Here is what I have so far
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65


#include <iostream>

void main ()

{
    using namespace std;

    int num, newNum;
    char symbol;

    do
    {
        cout << "Please enter a number. 0 to exit\n> ";
        cin >> num;
        if (num != 0)
        {

            do
            {
                cout << "\n> ";
                cin >> symbol;
                if (symbol != 'e' && symbol != 'E')
                {
                    cin >> newNum;

                    switch (symbol)
                    {
                        case '*':
                            num = num*newNum;
                            cout << "\n= " << num;
                            break;
                     
                          

							case '-':
                            num = num-newNum;
                            cout << "\n= " << num;
                            break;
                   

							case '+':
                            num = num+newNum;
                            cout << "\n= " << num;
                            break;
                       


							case '/':
                            num = num / newNum;
                            cout << "\n= " << num;
                            break;

                        default: //if anything else occurs
                            break;


                    }
                }
            } while (symbol != 'E' && symbol != 'e'); 
        }
    } while (num != 0);

  }


I'm trying to Pass in parameters to each function for the values to use and the functions will return the result. Can I Use a function to read in the numbers involved? These numbers will need to be doubles...

Also, I need to write a function that reads in the operator and returns a boolean – true if the operator is valid, false if not valid. This function will have two parameters. First is a string of characters containing the valid operators. The second is a reference parameter where the operator will be placed if the operator entered is valid.

I have the program working as is, its just converting it to using Functions that is killing me =[
Last edited on
Heres what the function would look like to get the number from user


double getNum(double num); // function prototype

double num = 0.0; // the var in main

 
num = getNum(num); // function call from main 


1
2
3
4
5
6
7
double getNum(double num) // function implementation
{
        cout << "Enter a number 0 to exit: ";
        cin >> num;

 return num;
}


the function call to a math function

 
someVar = doMath(num1, num2);


a function to do some math

1
2
3
4
5
6
7
double doMath(double num1, double num2)
{
       double aVar = 0.0;
       aVar = num1 * num2;
  return aVar; // now the variable that called the function from main will have the value of num 1 * num2
}


Hope this helps you solve your problem
Last edited on
Topic archived. No new replies allowed.