-1

When this runs I want to not be able to enter any spaces in cin what format specifier can I use for this?

Is their anything in <string> that lets me do this?

#include <iostream>
#include <string>
using namespace std;
int main(){
    string yy;
    cin >> string;

    /*when this runs I want to not be able to enter any spaces in `cin`
    what format specifier can I use for this? is their anything in      `<string>` that lets me do this?
    */
    return 0;
}
Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
Tristan
  • 3
  • 1
  • `C++` has no control over what the user is allowed to enter via the standard input (`std::cin`). All you can do is complain if they give you spaces or maybe ignore them depending on what it is you are trying to do. – Galik Aug 21 '16 at 01:41
  • I'm trying to ignore the whitespaces. My program is behaving unexpectedly when spaces are between 2 strings. Unexpectedly unwanted behavior. So I want to ignore the spaces and get only the 1st string to pass to my other functions. – Tristan Aug 21 '16 at 02:33

1 Answers1

1

You can use std::noskipws to disable automatic skipping of whitespace prior to formatted input:

if (std::cin >> std::noskipws >> yy) {
    std::cout << "read '" << yy << "'\n";
}

If the user enters spaces prior to the string it will be an error as no word could be successfully read.

Dietmar Kühl
  • 150,225
  • 13
  • 225
  • 380