tags:

views:

493

answers:

4

Say we have a code:

int main()
{
   char a[10];
   for(int i = 0; i < 10; i++)
   {
       cin>>a[i];
       if(a[i] == ' ')
          cout<<"It is a space!!!"<<endl;
   }
   return 0;
}

How to cin a Space symbol from standard input? If you write space, program ignores! :( Is there any combination of symbols (e.g. '\s' or something like this) that means "Space" that I can use from standard input for my code?

+2  A: 

Use cin.get() to read the next character.

However, for this problem, it is very inefficient to read a character at a time. Use the istream::read() instead.

int main()
{
   char a[10];
   cin.read(a, sizeof(a));
   for(int i = 0; i < 10; i++)
   {
       if(a[i] == ' ')
          cout<<"It is a space!!!"<<<endl;
   }
   return 0;
}

And use == to check equality, not =.

Marcelo Cantos
A: 

Using cin's >> operator will drop leading whitespace and stop input at the first trailing whitespace. To grab an entire line of input, including spaces, try cin.getline(). To grab one character at a time, you can use cin.get().

Odrade
sbi
@sbi: Shouldn't the last parameter be `char c = '\n'`?
tiftik
The >> operator will drop leading white space read the target object using white space as a delimiter (so for standard PODs will stop reading the target at the first space). (So it does not 'terminate' at the first white space). Also note, be very careful in the usage of the term 'terminate' it has a very explicit meaning when talking about C++
Martin York
@tiftik: Indeed. I'm sorry I blew that.
sbi
+3  A: 
#include <iostream>
#include <string>

int main()
{
   std::string a;
   std::getline(std::cin,a);
   for(std::string::size_type i = 0; i < a.size(); ++i)
   {
       if(a[i] == ' ')
          std::cout<<"It is a space!!!"<<std::endl;
   }
   return 0;
}
sbi
I want to use char, but not std::string. I am interrested to do what I want with char[]. Any way, thanks.
Narek
you can still get to the characters via the c_str() function or via the [] operator which is displayed in this answer. string will also provide you with a bunch of helpful functions: http://www.cplusplus.com/reference/string/string/
Default
@Narek: I hope you have a very good reason to want to deal with naked `char` buffers and I hope you know more about them than you know about iostreams.
sbi
I use that in purposes of study only.
Narek
@Narek: Then rcollyer's answer is what you need to read individual characters. It's got my vote.
sbi
+7  A: 

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.

rcollyer