好了,原来我只是哑巴!这完全不涉及代码。我通过一个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
$
输出突然出现。这里发生了什么?我很困惑。
你根本不想要“忽略”......“clear”在开始时也是毫无意义的。 – LogicStuff
使用cin.sync()就够了 – Raindrop7
感谢您的输入!我只是增加了这些,因为这是我能找到类似问题的唯一建议,希望它可以帮助。我试过cin.sync(),但不幸的是它也不会改变任何东西。 –