How to Cin Space in C++

std::cin input with spaces?

You have to use cin.getline():

char input[100];
cin.getline(input,sizeof(input));

How to cin Space in c++?

It skips all whitespace (spaces, tabs, new lines, etc.) by default. You can either change its behavior, or use a slightly different mechanism. To change its behavior, use the manipulator noskipws, as follows:

 cin >> noskipws >> a[i];

But, since you seem like you want to look at the individual characters, I'd suggest using get, like this prior to your loop

 cin.get( a, n );

Note: get will stop retrieving chars from the stream if it either finds a newline char (\n) or after n-1 chars. It stops early so that it can append the null character (\0) to the array. You can read more about the istream interface here.

C++ Input String with Spaces

I can't comment yet, don't have enough points, but did you try adding cin.ignore(); before the getline(cin, name, '\n'); ?

Like this:

string setBusinessName()
{
string name = "";
cout << "The name you desire for your business:";
cin.ignore();
getline(cin, name, '\n');
cout << name;
return name;
}

How does cin differentiate between a space and newline?

It doesn't.

If you need different behaviour, you should use std::getline

https://en.cppreference.com/w/cpp/string/basic_string/getline

Can't get input containing spaces in C++

First, you should know what your original struct means:

struct data
{
char name1[20]; // an array of 20 single characters
string name2[20]; // an array of 20 dynamic strings
string name3; // a single dynamic string
};

Look at the name2 member variable. It is an array of 20 std::string. That means you can have 20 separate dynamic strings, and each string is accessed by name2[0], name2[1], name2[2], up until name2[19].

Given that, since you want to simply enter 3 strings, then the struct above does not reflect what you are trying to accomplish. The correction should be:

struct data
{
string name1;
string name2;
string name3;
};

Having done this, then to fill each string from std::cin, and include spaces within each string, use std::getline 3 times:

data d;

getline(cin, d.name1);
getline(cin, d.name2);
getline(cin, d.name3);

Here is a Live Example.

Cin With Spaces and ,

Your input is:

foo,Hello World,foofoo

Your first line that reads input from std::cin is:

cin >> stringOne;

That line reads everything until it finds the first whitespace character to stringOne. After that line, the value of strinOne will be "foo,Hello".

In the lines

getline(str, stringOne, ',');       
getline(str, stringTwo, ',');

"foo" is assigned to stringOne and "Hello" is assigned to stringTwo.

In the line

getline(str, stringThree);

nothing is assigned to stringThree since there is nothing else left in the str object.

You can fix the problem by changing the first line that reads from std::cin so that the entire line is assigned to stringOne, not the contents up to the first whitespace character.

getline(cin, stringOne);

istringstream str(stringOne);

getline(str, stringOne, ',');
getline(str, stringTwo, ',');
getline(str, stringThree);


Related Topics



Leave a reply



Submit