Using an input string as a function name c++ - c++

First time posting so be gentle. I've started to teach myself C++ as I've always had an interest and also it will be useful for work in the future.
Ok so i have written a very basic program that will either Add, Subtract, Multiply or Divide depending on the user input.
My question is can i use an input from the user as string and use that to call a function?
See code below:-
#include <iostream>
#include <string>
using namespace std;
// Addition Function
int Add (int a, int b)
{
int r; //Result
r=a+b; //formula
return r; //return result of formula
}
// Subtraction Function
int Subtract (int a, int b)
{
int r; //Result
r=a-b; //formula
return r; //return result of formula
}
// Multiply Function
int Multiply (int a, int b)
{
int r; //Result
r=a*b; //formula
return r; //return result of formula
}
// Divide Function
int Divide (int a, int b)
{
int r; //Result
r=a/b; //formula
return r; //return result of formula
}
// Main
int main()
{
int ip1, ip2, z;
string option;
cout << "Enter first number: ";
cin >> ip1;
cout << "Enter second number: ";
cin >> ip2;
cout << "What would you like to do?, Please type an option (Options: Add, Subtract, Multiply, Divide)\n";
getline(cin,option);
z = option (ip1,ip2);
cout << "The result is " << z;
}
So i ask the user to type in an option i.e. Add, the program then takes that string(Add) and uses it to call the Add function.
At the moment im getting a 'no match for call to '(std::string {aka std::basic_string}) (int&, int&)' error on compile
Any help would be appreciated
Thanks
Lewis

You can use a pretty simple if conditional tree:
if (option == "Add") z = Add(ip1, ip2);
else if (option == "Subtract") z = Subtract(ip1, ip2);
else if (option == "Multiply") z = Multiply(ip1, ip2);
else if (option == "Divide") z = Divide(ip1, ip2);
Alternatively you can use an std::map to map an std::string to the corresponding function pointer. It possibly cleaner but definitely longer to write:
std::map<std::string, std::function<int(int, int)>> mapping;
mapping["Add"] = &Add;
mapping["Subtract"] = &Subtract;
mapping["Multiply"] = &Multiply;
mapping["Divide"] = &Divide;
if (mapping.find(option) == mapping.end())
// there's no such an option
z = mapping[option](ip1, ip2);
In this particular case you can even do without std::function and just use C function pointers (for non-std::function lovers):
std::map<std::string, int(*)(int, int)> mapping;
On a side note, notice that you can get rid of a lot of lines of code and temporary variables in your function declarations:
int Add (int a, int b) { return a + b; }
int Subtract (int a, int b) { return a - b; }
int Multiply (int a, int b) { return a * b; }
int Divide (int a, int b) { return a / b; }

Related

I am creating a calculator in c++ which it has an undo and clear. Using stacks

For my assignment I need to make calculator using stacks. I got the adding subtracting multiplying part. I keno for undo I just pop the stack and for clear I can just add a 0 to the top of the stack but every time a user enters U or C the program crashes because user input is supposed to be int.
output:
=>1
2
+
=>3
2
-
=>1
9
*
=>9
9
/
=>1
u
=>9
c
=>0
#include <iostream>
#include <sstream>
#include <stack>
using namespace std;
int add(int a, int b)
{
int x;
x = a + b;
return x;
}
int sub(int a, int b)
{
int x;
x = a - b;
return x;
}
int multi(int a, int b)
{
int x;
x = a * b;
return x;
}
int divide(int a, int b)
{
int x;
x = a / b;
return x;
}
int main()
{
stack<int>math;
char ASMD;
int x = 0;
int a = 0;
int b = 0;
bool control = false;
math.push(x);
cout << "=> " << math.top() << endl;
while (control == false)
{
cin >> b;
cin >> ASMD;
a = math.top();
switch (ASMD)
{
case '+':
{
x = add(a, b);
break;
}
case '-':
{
x = sub(a, b);
break;
}
case '*':
{
x = multi(a, b);
break;
}
case '/':
{
if (b != 0)
{
x = divide(a, b);
break;
}
else
{
cout << "error: can't divide my 0" << endl;
break;
}
}
default:
{
control = true;
break;
}
}
math.push(x);
cout << "=> " << math.top() << endl;
}
return 0;
}
If you're looking to implement a calculator, use the Shunting Yard algo. This algorithm works in the same manner as swapping a position in two arrays with the exception that instead of using a single cell as temporary storage, it utilizes a stack. The end result is to transform human readable expressions (infix notation) notation to computer executable expressions (postfix notation). For example, shunting yard will translate this infix expression "1 + 1" to "1 1 +". The postfix expression is stored in a stack and evaluated by poping the elements from the postfix stack until your reach an operator. Take the operator and apply it to the numbers already poped. That's a super brief overview so here's some more:
Rosetta Code: http://www.rosettacode.org/wiki/Parsing/Shunting-yard_algorithm
Wiki: https://en.wikipedia.org/wiki/Shunting-yard_algorithm
I've implemented this a few times so if you need any more help just ask!
You can read a string and then check if it can be converted to an int.
std::string input;
int inputAsInt;
if(std::cin << input) // Just to check that reading was successfull
{
try {
inputAsInt = std::stoi(input);
// conversion succeeded, input was an int
}
catch(const std::exception &) {
// conversion failed, the input was a string
}
}
That's just a basic example, but I hope it points you in the right direction.
You are too eager in evaluating the user input with
char ASMD;
int b = 0;
...
cin >> b;
cin >> ASMD;
You should read it into a string first and then decide the what the next action is.
if the input is 'u' or 'c' then there is no further input.
if the input is convertible to an integer then you need to read the operand.
if the input is 'wrong' then print some sort of error message and continue.
You are also not popping the values off the stack. Here is an improved but not fully functional version
cin >> in;
if( in == "u" ){
std::cout << "undo not implemented\n";
continue;
}
try{
b = stoi( in );
}
catch( std::invalid_argument& ){
std::cout << "invalid input\n";
continue;
}
a = math.top();
math.pop(); // ! need to pop here, well somewhere
cin >> ASMD;
see improved version live

Can you store arithmetic operators in an array i,e(+, -, *, /) in C++

I want to make a program that takes 4 numbers eg.(a, b, c and d) and checks if using arithmetic operators i can make the first 3 numbers result to the fourth number, like if the input is (3, 4, 5, 23) this will check out true because
3 + 4 * 5 = 23,So i want to make an array that has the operators and use a loop to check every possible combination, Hope i made it clear.
Edit:
Its actually codeforces problem, given 4 numbers. Check whether he could get the fourth number by using the arithmetic operators (+,−,×) between the other three numbers. Knowing that an operator can be used only once. in this format ->(a□b□c=d).My question was if there is a way to make it automatic or do i have to code every possibility manually So sorry for any confusion i may have caused.
You can't store the operators in an array, but you could make wrapper functions for them and store those in an array.
int add(int a, int b) {
return a + b;
}
int sub(int a, int b) {
return a - b;
}
int mul(int a, int b) {
return a * b;
}
int div(int a, int b) {
return a / b;
}
typedef int (*funptr)(int, int);
funptr arr[] = { add, sub, mul, div };
You can then call them like:
(arr[1])(2, 1) // call sub(2, 1)
The parentheses around arr[1] aren't needed in this case, but I like to put them for clarity.
No. You'd have to write a program to work this out. You could store something like function pointers to the arithmetic operators in an array, but I don't think that would help solve your problem. You'd still have to write the code to solve your problem.
Adding onto #CoffeeTableEspresso's answer, you can also put those function pointers into a map.
int add(int a, int b) {
return a + b;
}
int sub(int a, int b) {
return a - b;
}
int mul(int a, int b) {
return a * b;
}
int div(int a, int b) {
return a / b;
}
typedef int (*funptr)(int, int);
std::map<char,funptr> operators = {
{'+', add},
{'-', sub},
{'*', mul},
{'/', div}};
Then you can do
operators['+'](4,7);
Which might be a bit more readable, and you can iterate through these more easily.
I thought I would submit a compete answer. This works for positive numbers. It may take a bit more work to cover all the possibilities. And it does not answer to CoffeeTableEspresso's question about precedence. But it may help with your last question about if statements.
#include <iostream>
namespace {
auto add = [](int a, int b) {return a + b; };
auto sub = [](int a, int b) {return a - b; };
auto mult = [](int a, int b) {return a * b; };
auto divd = [](int a, int b) {return b ? a / b : -1; };
std::vector<int(*)(int, int)> ops = { add,sub,mult,divd };
}
int check(int* params)
{
for (size_t i = 0; i < 4; ++i)
for (size_t j = 0; j < 4; ++j)
{
auto result = ops[i](params[0], ops[j](params[1], params[2]));
if (params[3] == result)
return result;
else
std::cout << result << std::endl;
}
return -1;
}
int main()
{
int params[] = { 3, 4, 5, 23 };
std::cout << check(params);
}
Operators * / have a higher precedence than + -, so operator[i](A, operator[j](B, C)) solution doesn't really work.
You can write a little string calculator, and cycle through char-operators:
#include <iostream>
#include <sstream>
#include <vector>
#include <cmath>
double calculate(std::string str)
{
// calculator there
return -1;
};
int main()
{
std::vector<char> op = {'+', '-', '*', '/'};
std::vector<int> a = { 96, 3, 10, 42 };
for (auto op1: op)
for (auto op2: op)
{
std::stringstream ss;
ss << a[0] << op1 << a[1] << op2 << a[2];
double result = calculate( ss.str());
if (std::abs(a[3] - result) < 1E-6)
std::cout << ss.str() << " = " << a[3];
else
std::cout << ss.str() << " = " << result << " != " << a[3];
}
}

How to do a pow function inside of while? C++

Here is the problem I must do(Just for context): Write a program that will input 2 integers from the user, will then calculate the first to the power of the second and then output the result. The Input, calculation and output should be in three separate subprograms/functions. You must calculate the exponentiation using a WHILE loop and multiplying the first number, the required number of times. For this homework only, you are allowed to use global variables to move information between functions.
Here is my code:
#include <iostream>
#include <cmath>
using namespace std;
double a, b, ans;
int main()
{
cout << "Please enter two whole numbers: ";
cin >> a >> b;
cout << conclusion() << calc();
system("pause");
return 0;
}
int calc()
{
double ans = pow(a, b);
return 0;
}
int conclusion()
{
cout << a << " To the power of " << b
<< " is " << ans;
return 0;
}
So here's what I'm having an issue with, I do online classes. The dude is like, "here's a problem, figure it out and just do it." Which is fine I guess, but when things like this come up its hard to find certain tutorials and questions. Anyway, I got my BASE code down. Now I need a while loop, and have no idea what this means: calculate the exponentiation using a WHILE loop and multiplying the first number, the required number of times.
I figured I could just do a while and do
double ans = pow(a, b);
But that's not the case apparently. That's what my chapter taught me, but not with a while and all this extra stuff you need to do for this. I asked a classmate, she said she had a really difficult time as well, and her example to me was:
int a = 0;
int b = 0;
int c = 1;
cin >> a;
cin >> b;
int powerOp(int a, int b, int c)
{
while (b > 0)
{
c = c * a;
b--;
}
cout << c;
return c;
}
I have been working almost all day and can't figure this out. I don't understand why we need to factorize and set the int = 1. I thought it could simply be
double ans = pow(a, b); //a and b being the 2 numbers the user inputs
Its pretty simple, lets say you have 2^3. You and I both agree that it is the same as doing 2x2x2. You mutiplied you first number A by itself B (your second number) times. Now for your loop, what you want have your second number server as your counter AND loop exit condition. Something like this
double YourPowerFunction(int a, int b)
{
int counter = 0;
double result = 1;
while (counter < b)
{
counter++:
result = result * a;
}
return result;
}
Okay so I'm pretty sure I found the answer. Result is the variable for ans
int calc() //function for calculation
{
//Still not sure how I did this one, after hours of playing around with it
while (b > 0) //This code is adding a 0 in the result. I can't figure it out
{
result = result * a;
b--;
}
return (result);
}
This is just a simple solution, as it wants to add a 0 in the end result.

Recursively counting a number of values that satisfies a condition and return that number

I need to count how many cubes of values between a and b (2 and 9 in this example) end with numbers between 2 and 5. Everything has to be done with recursion.
The output of this code is
part c = recc = 4
32767
0
It does not make sense to me. It calculates the value of n correctly, but then once asked to return it, returns either 0 or 32767, as if it was not defined.
Can anyone pinpoint the issue?
#include <iostream>
#include <string>
using namespace std;
void partb(int a, int b){
if(a<=b){
int p = (a*a*a)%10;
else if(p>=2 && p<=5){
cout<<a*a*a<<" ";
}
partb(a+1, b);
}
}
int recc(int n, int a, int b){
int p = (a*a*a)%10;
if(a>b){
cout<<"recc = " << n << endl;
return n;
}
else if(a<=b){
if(p>=2 && p<=5){
n++;
}
recc(n, a+1, b);
}
}
int partc(int a, int b){
int n = recc(0, a, b);
cout<<endl<< "part c = " << recc(0, a, b) << endl;
return n;
}
int main(){
int n=partc(2,9);
cout << n << endl;
return 0;
}
Not all control paths in your function return a value, so you were getting undefined behaviour when using the return value.
Now, this wasn't helped by the fact that the function itself is needlessly complicated. Let's rewrite it to use common practice for recursion:
int recc(int a, int b)
{
if (a > b) return 0;
int p = (a*a*a)%10;
int n = (p>=2 && p<=5) ? 1 : 0;
return n + recc(a+1, b);
}
Now your function is simpler. The recursion termination condition is right at the top. The function then decides whether a will contribute 1 or 0 to the count. And finally you return that value plus the count for a smaller range.
Notice how return n + recc(a+1, b); has broken the problem into a simple local solution combined with the recursive result of a reduced scope.
The invocation becomes simpler too, because you no longer have to pass in a redundant argument:
int partc(int a, int b)
{
int n = recc(a, b);
cout << endl << "part c = " << n << endl;
return n;
}

Trying to do some normalization and keep getting error

I keep getting the error "error: invalid conversion from ‘int*’ to ‘int’ [-fpermissive] " and I do not know why;
I am doing this for homework and we have yet to discuss pointers so using them is out of the question.
here is my code (PS I am new to programming)
#include <iostream>
#include <cmath>
using namespace std;
double normalize (int , int);
double normalize (double,double);
int n=0;
int i=0;
const double SIZE=5;
double mean=0;
double meanDivide;
int main()
{
char dataType;
int norm[5];
int value =1;
cout<<"Which data type do you want (i, d): ";
cin>>dataType;
if (dataType =='i')
{
while(value<5)
{
cout<<"Enter value "<<value << ": ";
cin>> norm[n];
value++;
}
}
else if (dataType=='d')
{
cout<<"Enter value "<<value << ": ";
cin>> norm[n];
value++;
}
cout<<"The mean is: "<<normalize(norm,5)/* The error comes from here and
I do not know what to do or why */
<<endl;
cout<<"The normalized values: "<<endl;
int j=0;
cout<<"norm[1] = "<<norm[j]<<endl;
j++;
cout<<"norm[2] = "<<norm[j]<<endl;
j++;
cout<<"norm[3] = "<<norm[j]<<endl;
j++;
cout<<"norm[4] = "<<norm[j]<<endl;
j++;
cout<<"norm[5] = "<<norm[j]<<endl;
return 0;
}
double normalize(int norm[],int SIZE)
{
while(i<6)
{
meanDivide +=norm[i];
i++;
}
i=0;
while (i<n)
{
norm[i] -=meanDivide;
i++;
}
mean = meanDivide / 5;
return mean;
}
double normalize (double norm[],double SIZE)
{
while(i<6)
{
meanDivide +=norm[i];
i++;
}
i=0;
while (i<n)
{
norm[i] -=meanDivide;
i++;
}
mean = meanDivide / 5;
return mean;
}
This is the output I should be getting.
//For integers:
Which data type do you want (i, d): i
Enter value 1: 0
Enter value 2: 3
Enter value 3: 4
Enter value 4: 8
Enter value 5: 12
The mean is: 5.4
The normalized values:
norm[1] = -5
norm[2] = -2
norm[3] = -1
norm[4] = 2
norm[5] = 6
//For doubles:
Which data type do you want (i, d): d
Enter value 1: 5.5
Enter value 2: 1.23
Enter value 3: 2.02
Enter value 4: 9.99
Enter value 5: 6.32
The mean is: 5.012
The normalized values:
norm[1] = 0.488
norm[2] = -3.782
norm[3] = -2.992
norm[4] = 4.978
norm[5] = 1.308
You are declaring your methods like this :
double normalize (int , int);
double normalize(double*, double);
Yet you are trying to implement them like :
double normalize(int norm[], int SIZE) {/*...*/}
double normalize(double norm[], double SIZE) {/*...*/}
Notice that the argument types are not the same, int, int is not the same as int[], int. This means that your implementation is actually defining a whole new function, unrelated to the ones you declared at the top of your example. When you call your normalize function, only the initial declaration is found, and it tries to match int norm[5] to int which it fails. To fix this, make sure the declaration is correct. Change the declarations to this :
double normalize(int[], int);
double normalize(double[], double);
This will fix the error you are asking about in this question, but your example still has other problems. Some of them are identified in the comments.
You declare prototypes for non-pointer parameters:
double normalize (int , int);
double normalize (double,double);
You call with pointer arguments:
cout<<"The mean is: "<<normalize(norm,5)
The C++ compiler at that point does not care about the implementation with pointer arguments below. Try matching implementation to prototypes.
Also
a) you increment value but use n as index:
cin>> norm[n];
value++;
b) you ignore SIZE
c) SIZE should be unsigned int in both cases
d) your example is not minimal (https://stackoverflow.com/help/mcve)
You might find this generally useful:
https://ericlippert.com/2014/03/05/how-to-debug-small-programs/