The Design of Software (CLOSED)

A public forum for discussing the design of software, from the user interface to the code architecture. Now closed.

The "Design of Software" discussion group has been merged with the main Joel on Software discussion group.

The archives will remain online indefinitely.

noskipws in C++ doesn't work?

I have been having problems getting the cin and cout noskipws command to work.  I want to read everything the user types in into one string.  I know I could use getline, or other functions, but I would prefer to just use cin and cout.  Does anyone know why this program does not do what I want it to?

#include <iostream>
#include <string>
using namespace std;

int main() {
    string mystring;
    cout << "Enter a string with whitespace: ";
    cin >> noskipws >> mystring;
    cout << noskipws << mystring;
}
Steven Gary Noyce Send private email
Saturday, July 01, 2006
 
 
What class is this for?
Art Wilkins
Saturday, July 01, 2006
 
 
Do you mean is it for iostream or string?  It is in the iostream class I beleive.
Steven Gary Noyce Send private email
Sunday, July 02, 2006
 
 
Use gets().
John
Sunday, July 02, 2006
 
 
That requires a char array, doesn't it?  I am using a string from the string class, can that be treated the same way?  I would rather avoid pointers and other include files if at all possible.  Thanks for the help!
Steven Gary Noyce Send private email
Sunday, July 02, 2006
 
 
Don't use gets(), use std::getline(). For example:

  std::string line;
  while (std::getline(std::cin, line))
    std::cout << "line: " << line << '\n';

Will read all lines from std::cin until EOF.

Frankly I have not used noskipws, and I don't have any documentation with me, but I guess that noskipws does not do what you are wanting, if only works when reading chars. But I may be wrong on this one.
Azrael
Monday, July 03, 2006
 
 
or you could:

int main() {
    string mystring;
    cout << "Enter a string with whitespace: ";
    cin >> noskipws >> mystring;
    cout << noskipws << mystring << endl;
}

I think is what you're looking for.
~Eric
Wednesday, July 05, 2006
 
 

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics
 
Powered by FogBugz