#include <iostream>
std::string getOperator() {
int operandDecider;
std::string whichOperator;
std::cout << "What operation would you like to compute (sub, add, div, mul)?: ";
std::cin >> whichOperator;
return whichOperator; }
int assignInt(){
int operandDecider;
if (whichOperator == "add") {
operandDecider = 1;
} else if (whichOperator == "sub") {
operandDecider = 2;
} else if (whichOperator == "mul") {
operandDecider = 3;
} else if (whichOperator == "div") {
operandDecider = 4;
}
return operandDecider;
}
void process(){
int num1;
int num2;
std::cout << "Enter num1: ";
std::cin >> num1;
std::cout << "Enter num2: ";
std::cin >> num2;
if (operandDecider == 1)
{
std::cout << num1 + num2;
}
}
int main() {
getOperator()
assignInt()
process();
return 0;
}```
#C++ Calculator
77 messages · Page 1 of 1 (latest)
local variable "whichOperator" does not exist in the context of assignInt
same for process but with operandDecider
you need to either take in an argument or initialize a local variable of the same name in the functions
you honestly don't need those functions the way you wrote main
just put everything in main
how does that look like?
yeah, but im trying to learn functions more
how would code look like?
arguments go in the parantheses
int foo(int arg);
ye
arg is an argument
ok
thats how you create an argument for a function
just make sure to write the datatype and argument name
you want multiple
you can separate them by commas
if you want to use it as an argument, then yeah put that in the parantheses
local variables are just variables that are defined inside of the function
only that function can use them
make sure to also put in the arguments
why?
arguments are things you use when you want a different input to produce a different output
so if i had the function
nani
int addFive(int num)
{
return num+5;
}
int main()
{
addFive(10); //would return 15
addFive(15); //Would return 20
}
you pass in a value into the function call to be used as an argument inside the function
that value can be a variable or just a value like here
btw u should probably check out
https:/learncpp.com?
that's also a good resource
damn its big confusing idk why
bit
that works as well
initialize
oh like define?
please dont use else if, else if, else if, else if
just use a switch statement
thanks
doesn't work with strings
a lookup unordered map is the best option probably
are you familiar with hash tables/maps?
nope
#include <iostream>
int chooseOperator(){
std::string operatorInput;
std::cout << "What operator would you like to use (sub, div, add, mul?: )";
std::cin >> operatorInput;
int operandDecider;
if (operatorInput == "sub")
operandDecider = 1;
else if (operatorInput == "div")
operandDecider = 2;
else if (operatorInput == "add")
operandDecider = 3;
else if (operatorInput == "mul")
operandDecider = 4;
return operandDecider;
}
void process(int operandDecider, int num1, int num2){
switch (operandDecider)
{
case 1:
std::cout << num1 - num2;
break;
case 2:
std::cout << num1 / num2;
break;
case 3:
std::cout << num1 + num2;
break;
case 4:
std::cout << num1 * num2;
break;
default:
break;
}
}
int main(){
int num1;
int num2;
int operandDecider;
std::cout << "Calculator\n";
std::cout << "Enter number 1: ";
std::cin >> num1;
std::cout << "Enter number 2: ";
std::cin >> num2;
chooseOperator();
process(operandDecider, num1, num2);
return 0;
}```
that's not really solving the problem, just moving it to a different place
take a look at std::unordered_map
completly forgor about that