Sunday, October 4, 2009

Whitespaces..

While programming in c++ the standard i/o object cin ignores the whitespaces.

You can use std::noskipws to disable the whitespace skipping that std::cin does by default:

#include 
#include

int main() {
char c;
std
::cin >> std::noskipws;
while (std::cin >> c) {
if (c == ' ')
std
::cout << "A space!" << std::endl;
}
return 0;
}

3 comments:

  1. sir jee but there's a prob.. u r not holding a string at whole.. what we need is that we can play with the whole string including spaces.. what u r doing is just taking one cahracter at a time.

    ReplyDelete
  2. for strings you can use fgets function and check for newline character at the 0th index of the string inputted

    ReplyDelete
  3. we can try getline function too..
    getline(string,cin,delimeter);

    ReplyDelete