#C++ Calculator

77 messages · Page 1 of 1 (latest)

ember island
#
#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;
}```
#

Red lines, how do I fix?

azure plank
#

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

ember island
ember island
azure plank
#
int foo(int arg);
ember island
#

ye

azure plank
#

arg is an argument

ember island
#

ok

azure plank
#

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

ember island
#

i need to do std::string whichOperator?

#

oh ok

azure plank
#

local variables are just variables that are defined inside of the function

#

only that function can use them

ember island
#

like this?

azure plank
#

yes

#

when you call the functions

ember island
#

i get red at bottom btw

#

ye

azure plank
#

make sure to also put in the arguments

ember island
#

why?

azure plank
#

arguments are things you use when you want a different input to produce a different output

#

so if i had the function

ember island
azure plank
#

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

azure plank
#

btw u should probably check out

#
ember island
#

https:/learncpp.com?

azure plank
#

that's also a good resource

ember island
#

bit

azure plank
#

so i initialize a function

#

addFive

#

and i give it an argument it can take

#

num

ember island
#

call function addFive

#

?

ember island
#

oh like define?

azure plank
#

or not really initialize

#

but define

#

yeah

ember island
#

ok

#

oh ok I will check this link

#

thanks for help!!!

#

i understand it better

hushed smelt
#

please dont use else if, else if, else if, else if

#

just use a switch statement

#

thanks

nova shuttle
#

a lookup unordered map is the best option probably

ember island
#

o ye

#

what to do?

nova shuttle
#

are you familiar with hash tables/maps?

ember island
#

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;
}```
nova shuttle
#

that's not really solving the problem, just moving it to a different place

#

take a look at std::unordered_map

hushed smelt