views:

253

answers:

3

I need to overload the stream extraction operator. I need to do this by allowing a user to input a string of characters at a prompt, say "iamastring", and then the operator would extract each character from the string and test whether or not it is whitespace and if it is not whitespace store it in a character array which is then passed to an object.

@Chip et al. For example output I am not expecting it to output anything to the screen. Instead after a user types in a string and hits enter the user should be prompted again to enter a new menu selection. As it stands right now the user inputs a string hits enter and then a "bad read error" is displayed and the prompt comes back up awaiting new input.

+3  A: 

Is there any reason you are not using std::string and std::getline? You should think twice or even thrice before writing your own extraction operator - formatted input is not a particularly useful feature of C++ (or of C, come to that).

anon
Im always dubious of any training course that prevents you from using the simplest approach to the problem. I've been writing C++ code for over 20 years now, and I can count on one hand the number of times I've implemented a stream extraction operator.
anon
Yes well we all have to pay the piper. I guess some would call the exercise a bit on the pedantic side but there is always something to learn from banging your head repeatedly over and over again on your keyboard...I think.
nmr
+3  A: 

It's quite funny - your name is like mine, but reversed :)

How about:

char buffer[buffSize+1]; // no need for dynamic allocation here
unsigned i = 0;
while(std::cin && !std::isspace(std::cin.peek()) && i < buffSize)
  buffer[i++] = std::cin.get();
buffer[i] = '\0'; // null termination can be important.
// i now contains the length btw

It's exactly your own code, just a little refactored - I removed all unneccesary stuff etc, nothing more..

Edit: Now fixed to verify stream status & prevent stack overflow :)

Edit II: Changed std::cin.good() && !std::cin.eof() to std::cin. Btw: why does cin have a conversion to void* and not to a bool?

rmn
That code is also broken – see my answer above.
Konrad Rudolph
fixed! :) thanks.
rmn
This was helpful and clean. I ended up with a variation on this code.
nmr
Konrad Rudolph
Konard, you are absolutly correct - I just wanted to stay as close to the given code as possible. Now edited though.
rmn
You can make this take in any length my using a `vector` + `push_back`, too.
GMan
+1  A: 

In any case, the code is broken. It doesn’t handle input failures, which may be fatal since your code can enter an infinite loop. If you encapsulate stream reading operations, you must take care to test whether the stream is in a valid state.

Konrad Rudolph
so would I want to test that ios::goodbit == true and then enter my loop?
nmr
That’s not how stream bits work. `ios::goodbit` is simply a constant. Additionally, `== true` is simply redundant. See @rmn’s updated answer for a correct (albeit uncanonical) way to accomplish this.
Konrad Rudolph