1

I don't know if this is even possible.

I have an assignment to translate words and phrases into pig Latin in C++. the fastest way to do this would be to have the user hit enter after each word, but this would make entering a continuous phrase impossible without hitting enter instead of the space bar.

your
text
would
be
entered
like
this

The your output could easily be:

youway exttay ouldway ebay enteredway ikelay histay

But still putting the info in would be weird.

Instead I would like to force the program to treat the space bar as though it were the enter key (carriage return).

your text would be entered like this

That way each word would enter my array separately from the string, the user only having to hit enter 1 time.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
David
  • 95
  • 8
  • 1
    You're fundamentally asking the wrong question. You will get much simpler answers to the question "how do I read a bunch of words separated by spaces into an array". – CrazyCasta Oct 02 '12 at 06:26

3 Answers3

2

You could do something like:

  1. Read a line of text from user input (which may have multiple words)
  2. Split the line into words
  3. Translate each word into Pig Latin
  4. Print the words out with spaces between them

Rather than thinking of this in terms of "how can I change these keys to mean something else", think of it in terms of "how can I best work with what the user is expecting to type". If the user is expecting to type spaces between words (makes sense), then design your program so that it can handle that kind of input.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
0

You can have the user input data as a single line, since that seems natural.

If you want some help in parsing the words to operate on the one at a time, then try this other question.

Community
  • 1
  • 1
CallMeNorm
  • 2,299
  • 1
  • 16
  • 23
0

Here's the cheap-o way to do it:

std::string in;
while (std::cin >> in)
  std::cout << piglatin(in) << char(std::cin.get());

std::cin >> in skips any leading whitespace in the input stream, and then fills in with the next whitespace-terminated word from the input stream, leaving the whitespace termination in the input stream. char(std::cin.get()) then extracts that terminator (which might be a space or a new line). The while loop is terminated by an end-of-file.

You can use that provided you understand it.

Added:

Here's a better way to find whether the word read was terminated with a space or a new-line:

#include <cctype>
char look_for_nl(std::istream& is) {
  for (char d = is.get(); is; d = is.get()) {
    if (d == '\n') return d;
    if (!isspace(d)) {
      is.putback(d);
      return ' ';
    }
  }
  // We got an eof and there was no NL character. We'll pretend we saw one
  return '\n';
}

Now the hack looks like this:

std::string in;
while (std::cin >> in)
  std::cout << piglatin(in) << look_for_nl(std::cin);
rici
  • 234,347
  • 28
  • 237
  • 341
  • I kinda understand, and thanks for not being sarcastic like everyone else, it is appreciated. Could you use this to enter one word into an array at a time. – David Oct 03 '12 at 02:45
  • You can do anything you like with the words you read in :) The tricky part is recognizing the end of the line, which my little hack does slightly imperfectly (it assumes the user never types a space just before hitting enter). It demonstrates the difference between *formatted* input (eg. cin >> s) and unformatted input (cin.get()). Formatted input skips whitespace before the thing it reads, and stops reading when it hits whitespace (which it puts back on the input stream). Unformatted input just gives you what's there. So after cin>>s, you either have whitespace or an eof waiting for you. – rici Oct 03 '12 at 03:09