-1

I want to store character in the vector. But it's not working when I wanted to push back the matching character.

Error is showing in this line -

v.push_back(arithmetic_operator[i]);

The whole code is -

#include<bits/stdc++.h>
using namespace std;

int main()
{
    vector<char>v;

    char str[100];
    gets(str);

    char *ptr;
    ptr = strtok(str, " ");

    char arithmetic_operator[6][6] = {"+", "-", "*", "/", "%", "="};

    while(ptr !=NULL)
    {   
        // arithmetic operator
        for(int i=0; i<sizeof(arithmetic_operator); i++){
            if(strcmp(arithmetic_operator[i], ptr) == 0)
            {
                v.push_back(arithmetic_operator[i]);
            }
            else
            {
                continue;
            }
        }

        ptr = strtok(NULL, " ");
    }

    for (auto it = v.begin(); it != v.end(); it++)
    {
        cout << *it << " ";
    }

    return 0;
}

When the input is a = b + c, expected output will be =,+

  • 1
    Don't use `gets`. It was previously deprecated and has since been removed from both the C and C++ standards. – NathanOliver Jul 18 '19 at 14:03
  • 1
    It says it in the error message that the type of what you're pushing back it `char[6]`. `v` is a vector of `char`, however. Did you mean to push pack `arithmetic_operator[i][0]` instead (not that I'd recommend storing the operators in this 2D array construct)? – Blaze Jul 18 '19 at 14:04
  • 3
    **Recommended reading:** [Why should I not #include ?](https://stackoverflow.com/q/31816095/560648) – Lightness Races in Orbit Jul 18 '19 at 14:08
  • 1
    `sizeof(arithmetic_operator)` is not doing what you expect it's doing. It returns size of the whole array in bytes, so in this case it returns 36. – Yksisarvinen Jul 18 '19 at 14:38
  • What's your point with `sizeof(arithmetic_operator)`? with `continue;`? – curiousguy Jul 18 '19 at 14:41
  • Actually, I can write 6 instead of `sizeof(arithmetic_operator)` and `continue;` is used because there are more for loop in my code but I didn't mention that code in here. – Rakibul Islam Jul 18 '19 at 15:27

2 Answers2

3
#include<bits/stdc++.h>

Just no. Never. Not even once. Don't do this, and treat any example code which shows this, with extreme caution (ideally, ignore it and find something that isn't rubbish).

vector<char>v;
v.push_back(...);

The documentation for push_back shows that a vector<char> will expect a char as the argument.

char arithmetic_operator[6][6] = {"+", ... };

but obviously arithmetic_operator[i] is nothing like a char. If you print the whole error, it might even tell you what the type really is, but as a clue, arithmetic_operator[i][0] would be a char.

Useless
  • 64,155
  • 6
  • 88
  • 132
0

To answer your question: you are passing a char* to a vector<char>. So, defining v as vector<char*> will solve the error.

In addition, as already pointed out, gets is deprecated in C++11 and will be removed in C++14. For the same functionalities you can use std::fgets. Have a look here.