[SOLVED] User input used as a parameter in a function c++


While I understand why my code is wrong as well as brute-forcing my way around it, I’m wondering if there is way to do it the way I imagined it, so:
(just for context I’m making a simple game called Tower of Hanoi and this is the function I made that works when I manually input my stack as parameters)

void putOn(std::stack<int> &first, std::stack<int> &second){
        std::cout << "Your stack is empty, try again";
    }else if(first.top() && second.empty()){
    }else if(first.top() < second.top()){
    }else if(first.top() > second.top()){
        std::cout << "You can only put larger pieces on top." << std::endl;

Later in my code I’m using switch-case(for input, move towers, etc.) What I’m trying to do is translate my line putOn(x,y) into a variable input like so:

            case 1:
                char a,b;
                std::cout << "Enter the tower u want to move from(x, y, z): ";
                std::cin >> a;
                std::cout << std::endl;
                std::cout << "To(x, y, z): ";
                std::cin >> b;
                std::cout << std::endl;
//              putOn(&a,&b);
//              putOn(a,b);

You can tell where I’m going with this and obviously I get get an error that says: candidate function not viable: no known conversion from 'char *' to 'std::stack<int> &' for 1st argument void putOn(std::stack<int> &first, std::stack<int> &second){
Is there a "python-like" way to do this where my user input directly translates character variables a and b into x,y or z as parameters, thank you for your time


Try this:

std::map<char, std::stack<int>*> m = {{'x', &x}, {'y', &y}, {'z', &z}};
putOn(*m[a], *m[b]);

Answered By – cidermole

Answer Checked By – Robin (BugsFixing Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *