0

This is my code:

#include <iostream>
#include <vector>

using namespace std;

int main(){
    int x=-1;
    while(x != 0)
    {
        x = 0;

        cout << "nuevo numero: ";
        cin >> noskipws >> x;
        cout << x << endl;;
    }
}

and the output is:

nuevo numero: 5  // I input that number
5
nuevo numero:    // Here it doesn't wait for an input
0                // I don't know where this come from, guess it's the empty input

I know this is related to the noskipws, but I don't know the exact reason nor how to fix it.

QUESTION: Why the second cin >> noskipws doesn't wait for input? How can I fix it?

Fahed
  • 195
  • 2
  • 10

3 Answers3

1

Why the second cin >> noskipws doesn't wait for input?

Because there is no need to ask for input: your program still have not processed the input it has been given.

When you enter first number, you pressed 5, then enter. It inserts two characters into input stream: '5' and '\n'. First input operation reads '5', it is an acceptable character to have in number, so it consumes it. Then it sees '\n', it is not a valid character in number, so it stops there, leaves '\n' in stream and constructs number from what already has been read.

On next input operation it sees '\n' in input stream. It is not a valid character for a number, so it stops right away. Normally, whitespace characters would be skipped before attempting an input operation (which would lead to input buffer exhaustion and request for more input), but you explicitely asked not to (by setting noskipws flag). So, you got what you asked for.

If you want to imitate default behavior of streams in regard to whitespace skipping, but do not want to disable noskipws flag, you can use std::ws manipulator:

std::cin >> std::ws >> i;

It consumes all characters, until non-whitespace character is found.

Revolver_Ocelot
  • 8,609
  • 3
  • 30
  • 48
  • Ok, I understand the reason. So, How can I reset the pointer's position or delete the content I have or an alternative to that? – Fahed Dec 19 '17 at 11:55
  • `std::ws` has exactly the needed behavior. I also found somewhere else that using `std::cin >> std::noskipws >> i; cin.ignore();` also worked as needed. – Fahed Dec 19 '17 at 18:49
  • @Fahed you should be really careful with `.ignore()`, if you do not know what it actually does. For example, if you type '5'-space-enter, it will not work. – Revolver_Ocelot Dec 19 '17 at 19:57
  • I just read the reference, you are right. So, is `std::ws` the only way to go? – Fahed Dec 19 '17 at 21:05
  • @Fahed if your intention is to skip all whitespace characters before next input, the easiest way is to use specifically created standard library obect. Of course you can replicate its behavior by, say, checking next character with `.peek()` and discarding it if it is non-printable character. – Revolver_Ocelot Dec 19 '17 at 21:14
0

If you use noskipws, then the first step is skipped. After the first read, you are positionned on a whitespace, so the next (and all following) reads will stop immediatly, extracting nothing.

Pankaj Gadge
  • 2,748
  • 3
  • 18
  • 25
  • Ok, thanks, I understand. So, How can I reset the position or delete the content I have or an alternative to that? – Fahed Dec 19 '17 at 11:28
0

What your program failed to do was check whether extraction of the number from the stream is successful.

Update the program to use:

    cout << "nuevo numero: ";
    if ( cin >> noskipws >> x )
    {
       cout << x << endl;;
    }
    else
    {
       cout << "Unable to read.\n";
    }

You will notice the second call fails. It fails because you specified noskipws but there is nothing in the stream to extract an integer.

The noskipws manipulator works for reading characters from a stream but not for reading numbers.

Pete Becker
  • 74,985
  • 8
  • 76
  • 165
R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • The thing is, I need to read empty input, and finish the program if that happens. – Fahed Dec 19 '17 at 11:32
  • @Fahed, you are running into the typical [X/Y problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). – R Sahu Dec 19 '17 at 16:17
  • @R Sahu I just read the link, and actually I'm not in an X/Y. But I will consider for future questions anyway. – Fahed Dec 19 '17 at 18:37