0

Please ELI5 if possible, since i've only been coding for a few days and this is my first program! Below is a portion of my script that is supposed to interpret a single line of input that somebody enters (like "5+5" or something).

I have other operations that I want to add later that are formatted differently, which is why I'm using string instead of a switch function or something.

Anyways.. this isn't working :( So below is my logical process and maybe somebody can point out where I messed up? :)

Thank you in advance!

   if (fork.find("+" && "-" && "x" && "/"))
{
    size_t pos = fork.find("+" && "-" && "x" && "/"); // Defines a position at the operator symbol
    string afterfork = fork.substr(pos + 1); // Cuts a substring at the operator symbol + 1
    size_t beforepos = fork.find_first_of(fork); // Defines a position at the beginning of the string
    string beforefork = fork.substr(beforepos); // cuts a substring at the begninning of the string
    string atfork = fork.substr(pos); // cuts a substring that only has one char (the operator +, -, x, etc)
    int x = stoi(beforefork.c_str()); // converts the first substring to an integer
    int y = stoi(afterfork.c_str()); // converts the third substring to an integer
    string operation = atfork; // converts the middle substring that only has one char to a different name.
    return input(x, operation, y); // will send this information to the input function (which will do the math for the calculator).
}
largeladdy
  • 53
  • 1
  • 6
  • If this is your first program then might I suggest you try something easier. I mean, parsing is tricky even when you've been programming for ages. There are a lot of problems with the program you have. – Indiana Kernick Jan 13 '20 at 04:11
  • Whitespace is free... I'm not sure what `if (fork.find("+" && "-" && "x" && "/")` is supposed to do. Is `fork` a `std::string`? – Retired Ninja Jan 13 '20 at 04:22

1 Answers1

1

To search the string for one of a list of characters, you can use find_first_of. This function returns npos if it didn't find anything.

const size_t operatorPos = input.find_first_of("+-*/");
if (operatorPos == std::string::npos) {
  std::cout << "Couldn't find an operator!\n";
  return;
}

To split the string into two sub-strings, you can use substr. To get a character at a position, use operator[].

const std::string left = input.substr(0, operatorPos);
const std::string right = input.substr(operatorPos + 1);
const char operation = input[operatorPos];

To convert a string to an integer, well, there are a lot of options. I'll use std::stoi for this answer. This function throws an exception that we need to catch when it can't convert the string to an integer.

int leftInt;
try {
  leftInt = std::stoi(left);
} catch (...) {
  std::cout << '"' << left << "\" is not a valid integer!\n";
  return;
}

int rightInt;
try {
  rightInt = std::stoi(right);
} catch (...) {
  std::cout << '"' << right << "\" is not a valid integer!\n";
  return;
}

If exceptions are really confusing (it took me ages to get my head around exceptions!) then you can try another function. My favourite (and IMO best) is std::from_chars. Another option is to just not catch the exception.

const int leftInt = std::stoi(left);
const int rightInt = std::stoi(right);

In that case, you won't get a nice error message like "five" is not a valid integer!. You'll get something like:

libc++abi.dylib: terminating with uncaught exception of type std::invalid_argument: stoi: no conversion
Abort trap: 6

Try running std::stoi("five") and see for yourself!


Please, don't use using namespace std;. Just don't!

Indiana Kernick
  • 5,041
  • 2
  • 20
  • 50