printf字符串输出"Dz↕"getch()C++

kom*_*ihe 1 c++ string printf visual-studio-2010 visual-c++

我希望输出说出用户按下的箭头键.我得到的输出是:

"You pressed the Dz? button."
Run Code Online (Sandbox Code Playgroud)

我写的是:

unsigned int key;
string K;
do
{
    key = getch();
    if (key==72)
        K=" up";
    else if(key==80)
        K="down";
    else if (key==77)
        K="right";
    else if (key==75)
        K="left";
    else
        {}
    if (key!=224)
        printf("You pressed the %s button.\n", K);
    else
        {}
}while(key !='q');

return;
Run Code Online (Sandbox Code Playgroud)

pho*_*xis 6

std::cout << "You have pressed " << K << " button." << std::endl; 你应该有混合string对象类型printf. printf只接受一个char *char []%s格式字符串.

或者您可能希望访问与该string对象关联的c字符串K.c_str ()

printf ("\ntest %s", K.c_str ());