Using a stack


I am having trouble with the code. I was wondering is anyone can see where I am going wrong.
I need to check if the string has the same amount of K's and J's and I can assume there are only K's and J's in the string
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 

bool equal(string s){
    stack<string>K_stack;
    stack<string>J_stack;
    for(int i=0; i<s.length(); i++){
        if(s[i]=='K')
            K_stack.push(s[i]);
        else
            J_stack.push(s[i]);
}
    if(K_stack.size()==J_stack.size())
        return true;
    else
        return false;
}
Last edited on
Is your program:
1) Not compiles
    * Is other code compiles correctly, i. e. is your compiler configured properly?
    * If it is, give us error message and corresponding code part.
2) Not running
    * Are you sure that it is not a problem with automatically closing console?
    * How do you run it?
    * Is there any error messages?
3) Gives an error when run
    * Is it system error message or error reported in console?
    * Give us error message and input which leads to error.
4) Not giving correct results
    * Tell what you entered, what you expected, and what you got.
    * Are you sure that results you expected are correct?
This is the error I get
|20|error: invalid user-defined conversion from 'char' to 'const value_type& {aka const std::basic_string<char>&}' [-fpermissive]|
|20|error: invalid conversion from 'char' to 'const char*' [-fpermissive]|
|22|error: invalid user-defined conversion from 'char' to 'const value_type& {aka const std::basic_string<char>&}' [-fpermissive]|
|22|error: invalid conversion from 'char' to 'const char*' [-fpermissive]|


Does this have to do with how I push onto the stack?
SO there is the problem: you defined stacks as containing strings, but trying to store chars. Think what you reaally want here.
thanks, the function returned one so I know it worked
Topic archived. No new replies allowed.