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..
#include <iostream>
void main ()
{
usingnamespace 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 =[
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
}