2016-09-14 80 views
-1

编辑后2:首先与CIN只跳过,即使使用cin.ignore

好了,原来我只是哑巴!这完全不涉及代码。我通过一个bash脚本运行了所有的东西,并且由于之前的使用方式(也不需要任何输入),我仍然在最后使用&运行它 - 显然,我无法从中获得任何输入贝壳。


我的程序似乎跳过了线,我尝试接收输入使用cin(它会直接到下一行)。

编辑:请看看那里我把新的代码会发生什么底部。

我已经在这里搜索和Google搜索,我发现了很多问题,人们有同样的问题!据我所知,这个问题几乎总是一个剩余的'\ n' - 但是没有一个解决方案对我有用。这是有问题的代码:

//char input_char; 
std::string input_string; 
//int input_int; 

//std::string line; 

std::cout << "hello. your input: "; 

std::cin.clear(); 
std::cin.ignore (std::numeric_limits<std::streamsize>::max(), '\n'); 

std::cin >> input_string; 
// std::getline(std::cin, input_string); 

std::cout << "input: " << input_int << endl; 

我只需要一个字符或数字。我用字符,整数或字符串尝试过它;我试过cin和getline。我已经为其他类似问题的建议添加了明确和忽略,但我仍然遇到同样的问题。

这是我的主要的开始,所以我没有做这个代码之前任何其他的cout或CIN。但是,这是一个较大项目的一部分,我正在使用ros。在程序到达这个部分之前,还有其他的输出是通过ros来处理的;没有其他输入,但。

我非常感谢你对此的帮助!我觉得我必须错过一些非常明显的东西...

编辑:我现在已经注释掉了与该输入不直接相关的所有内容,并将cin移到主体的最顶端。现在,这是完整的代码(注释部分排除在外):

#include <iostream> 
#include <ros/ros.h> 
#include <vector> 
#include <ros/console.h> 
#include "std_msgs/String.h" 

//#include <SharedMessages/MicroconRequest.h> 

/* ... */ 

//ros::Publisher reqPublisher; 
//SharedMessages::MicroconRequest reqMsg; 


/* ... */ 

int main(int argc, char* argv[]) 
{ 

    char input_test; 
    std::cout << "character: "; 
    std::cin >> input_test; 
    std::cout << input_test; 
    std::cout << "did that work?"; 

    // Handle ROS communication 
    ros::init(argc, argv, "Gestures"); 
    ros::NodeHandle n; 

    //ros::Subscriber joy_sub_ = n.subscribe<sensor_msgs::Joy>("joy", 10, joyCallback, this); 
    //reqPublisher = n.advertise<SharedMessages::MicroconRequest>("MicroconRequest", 10); 

    ros::Rate loop_rate(10); 

    // Infinite control loop 
    while (ros::ok()) 
    { 

     /* ... */ 

     cntLoop++; 

     ros::spinOnce(); 
     loop_rate.sleep(); 
    } 

    // turn off microcontroller 
    return 0; 
} 

现在发生的事情是:查杀程序确实只有经过

$ ./startDemo.bash 

Starting Minimal Example 

$ character: a    # now I have time to input something, but... 
a: Befehl nicht gefunden.  # = command not found] 
           # then it doesn't do anything, so I stop it 
$ ./stopDemo.bash 
killing /Gestures 

did that work?[ WARN] [1473954737.268901991]: Shutdown request received. 
[ WARN] [1473954737.268978735]: Reason given for shutdown: [user request] 
killed 
$ 

输出突然出现。这里发生了什么?我很困惑。

+0

你根本不想要“忽略”......“clear”在开始时也是毫无意义的。 – LogicStuff

+0

使用cin.sync()就够了 – Raindrop7

+0

感谢您的输入!我只是增加了这些,因为这是我能找到类似问题的唯一建议,希望它可以帮助。我试过cin.sync(),但不幸的是它也不会改变任何东西。 –

回答

0

好了,原来我只是哑巴!这与代码无关:

我通过bash脚本运行所有的东西,并且由于之前我的使用方式(也不需要任何输入),我仍在运行它与&在最后 - 很显然,我无法从该外壳得到任何输入。

[注:我不确定回答自己的问题是否是好习惯;起初我只是编辑它。但我认为它会比让它开放更有意义。]

0

我不明白你对跳过一条线,但没有印刷的原因,直到你关机就是你缺少从输出endl原来的问题。我添加了它,输出看起来像你期望的一样?

char input_test; 
std::cout << "character: "; 
std::cin >> input_test; 
std::cout << input_test; 
std::cout << "did that work?" << std::endl; 

输出:

[email protected]:/catkin_ws/devel/lib/stack# ./stack_node 
character: a 
adid that work? 
+0

嗨,感谢您的帮助!原本问题是我没有机会输入任何东西,它会立即跳到代码中的下一行。后来,问题是我有时间输入内容,但它会被解释为终端命令,告诉我“Befehl nicht gefunden”=“命令未找到”。然而,我最终能够解决它 - 毕竟这不是代码问题,这是因为我用它开始的bash脚本......我只是非常愚蠢。 –