2

I'm trying to make a program in C++ in which the number of mathematical signs are counted. I am using isdigit to figure this out, but when I pass the value of my string, it gives me a warning.

This is the code that I have. The line digito[i] = entrada[i] is where I think the problem lies, but I do not understand why.

cout << "Input the operation" << endl;
cin >> input;
string digit[] = { "" };
string sign[]={""};
int cn = 0, cs = 0;
for (int i = 0; i < input.size(); i++) {
    if (isdigit(input[i])) {
        cout << "There is a digit in position " << i << endl;
        cn += 1;
        digit[i] = input[i];
    }
    else {
        cout << "There is a sign in position " << i << endl;
        // sign[i] = input[i];
        cs += 1;
        sign[i] = input[i];
    }
}

It takes me to this code as the problem:

static _CONSTEXPR17 void assign(char& _Left, const char& _Right) noexcept
{ // assign an element
    _Left = _Right;
}
3
  • 1
    English, please. Commented Feb 5, 2019 at 1:20
  • 1
    Hector, lo he convertido a inglés, pero es posible que desees comprobarlo, ya que usé Google Translate, que es notoriamente poco confiable. Alternativamente, hay un sitio SO en español si prefiere su idioma nativo: es.stackoverflow.com Commented Feb 5, 2019 at 1:32
  • I translated it to English! @paxdiablo (Spanish, my native language). Commented Feb 5, 2019 at 9:38

1 Answer 1

2

Those two strings are problematic. You've unnecessarily declared them as arrays with one element each, and initialized each string to empty.

string digito[] = { "" };
string signo[]={""};

Yet afterwards, you're indexing them with non-zero indices:

digito[i] = entrada[i];

This line is problematic because of two reasons; going beyond the array bounds, and incompatible types.

digito[i] is the type of std::string (because digito is std::string[]), while entrada[i] is char (assuming entrada is std::string). std::string has an overload of its operator= that allows assigning to a single character, but that's not what you want here, I assume.

As for the second problem, std::string requires you to enlarge it before you random-access it at a given index. The best way to do this in this case would be during construction, dropping the erroneous array use:

std::cin >> entrada;
std::string digito(entrada.size(), ' ');
std::string signo(entrada.size(), ' ');

That being said, I'm not sure if this code does what you want it to. Given an input string of:

2+2/3

You'll get two such strings:

digito = "2 2 3"
signo  = " + / "

If your actual goal was to tokenize the input (divide into numbers and operators), a much better way would be to use either two std::vector<char>s, or, even better:

using Number = int;
enum class Operator { Plus, Minus, Div, Mul };
using Token = std::variant<Number, Operator>
using Sequence = std::vector<Token>;

A consistent, strongly-typed data model will make it much easier to write correct code that produces it and operates on the results afterwards.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.