-2
#include <iostream>

using namespace std;

int
main ()
{
  int number1, number2, sumatotala, choose, add, multiply;
  add = "add";
  multiply = "multiply";
  
  cin>>choose;
  cin>>number1;
  cin>>number2;
  
  if(choose = add) cout<<number1+number2;
  if(choose = multiply) cout<<number1*number2;

  return 0;
}

This is my code and I have the next error. main.cpp:17:9: error: invalid conversion from ‘const char*’ to ‘int’ [-fpermissive] add = "add"; ^~~~~ main.cpp:18:14: error: invalid conversion from ‘const char*’ to ‘int’ [-fpermissive] multiply = "multiply"; ^~~~~~~~~~

I want to make a little program that lets u choose multiply or add and based on your decision multiply or add the 2 numbers. I don't code that well and if someone could help me I would be very gratefull.

Appler
  • 13
  • 1
  • 2
    What do you think `add = "add";` does exactly? Do you understand that `add` is an integer? – David Schwartz Jul 30 '21 at 17:20
  • Well, I thought that this would give the add variable the "add" string value. – Appler Jul 30 '21 at 17:24
  • Oh you mean add is another kind of function and i shouldnt name it like that ? – Appler Jul 30 '21 at 17:24
  • 1
    Spoiler: `if(choose = add)` doesn't do what you might expect (and that's true for any type you give them). – Scheff's Cat Jul 30 '21 at 17:25
  • @Appler Isn't `add` an `int`? How could it hold a string value? – David Schwartz Jul 30 '21 at 17:28
  • There's not much point in creating a variable to give it one value that it will hold forever. If you want to use `"add"` as a constant, string literal in only one place, why not just use it as a literal in that one place? (PS: Don't get in the habit of putting `using namespace std;` it will not serve you well.) – David Schwartz Jul 30 '21 at 17:33
  • Why isnt using namesplace std good? – Appler Jul 30 '21 at 17:45
  • [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/q/1452721/7478597) In short: It may pollute your global namespace with symbols you don't expect and which may cause surprising error messages if you try to use these names for your own stuff. – Scheff's Cat Jul 30 '21 at 17:55

2 Answers2

1
add = "add";            
multiply = "multiply";

Here you need to either use numbers as values (because these are integer variables) or if you need them as strings, you have to remove them from the above line and write them like this:

std::string add = "add";
std::string multiply = "multiply";
Schnick
  • 51
  • 3
0

You define both "add" and "multiply" as ints but then try to assign them string values. They should be defined separately if you intend for them to be strings.

  int number1, number2, sumatotala, choose;
  string add = "add";
  string multiply = "multiply";
Empyreus
  • 1
  • 1