First & only cin is skipped, even after using cin.ignore

Zaunkönig

Edit 2:

Well, turns out I was just being dumb! It wasn't anything to do with the code at all. I was running everything via a bash script, and because of how I was using it before (which also didn't require any input) I was still running it with & at the end - so obviously I couldn't get any input from that shell.


My program seemingly skips the line were I try to receive input using cin (it goes right to the next line).

Edit: Please look at the bottom where I put the new code and what happens.

I've searched here and googled, and I've found a lot of questions where people had the same problem! As far as I understand, the problem was almost always a leftover '\n' - but none of the solutions have worked for me sofar. This is the problematic code:

//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;

I only need one character or number. I've tried it with a character, int, or string; and I've tried cin and getline. I've added clear and ignore as was suggested for other similar questions, but I still have the same problem.

This is at the beginning of my main, so I'm not doing any other cout or cin before this code. However, this is part of a larger project, and I'm using ros. Before the program gets to this part, there are other outputs which are handled through ros; no other input though.

I'd very much appreciate you help with this! I feel like I must be missing something really obvious...

Edit: I've now commented out literally everything that isn't immediately related to this input, and moved cin to the very top of the main. This is now the complete code (commented parts left out):

#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;
}

Now what happens is the following:

$ ./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
$ 

Only after killing the program does the output suddenly appear. What is happening here? I'm so confused.

Zaunkönig

Well, turns out I was just being dumb! It wasn't anything to do with the code at all:

I was running everything via a bash script, and because of how I was using it before (which also didn't require any input) I was still running it with & at the end - so obviously I couldn't get any input from that shell.

[Note: I wasn't sure if it was good practice to answer one's own question; at first I just edited it. But I figured it'd make more sense than to just leave it open.]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

C++ cin gets skipped, even after I use cin.ignore()

cin.ignore not working: further inputs get skipped even with clear()

Why is cin.ignore() necessary when using "getline" after "cin", but is not required when using "cin" multiple times?

C++: Ignoring input after first cin.ignore

Cin getting skipped after being used in for loop

cin statement being skipped

Issue When Clearing cin Buffer After Using cin.getline()

Get only the first value from std::cin

cin not allowing input after first use of function

Cin not waiting for input despite cin.ignore()

How to move cursor to end of text output after cin.clear() and cin.ignore()?

How to use cin after using system("pause")?

Weird behavior when using cin.ignore and cin.clear CPP

function CIN gets skipped every time

Input using getline(cin,n); is not printing first input and i am not using cin>> to take input anywhere

Not waiting for std::cin.ignore()

Why Is My Program Skipping The cin > > after the first cycle of the For Loop?

CIN validation using cin.fail

How to keep the buffer of std::cin after using std::cin.getline()

Using cin with arrays

Using cin function

How to properly use cin.clear an cin.ignore

cin.clear() and cin.ignore() won't work

How does cin.ignore work with cin and getline?

Using cin for keyboard input after processing redirected file input with getline

How to fix reading from file after using std::cin function

Why cin is not getting buffer data after using getline?

What cin.ignore() does exactly?

What's the equivalent of cin.ignore() in C?