Want to make cin.get accept chars after enter is pressed

Hi, I will admit that this is a project that I working on for class, however, I have spend 10+ hours on this project and almost completely done. The problem I have run across is with cin.get(). I have a program that is supposed to encrypt a message input by the user. Before I was using a for loop that looked like this:
for (index = 0; index < messageLength; index++)
cin >> message[index];

but I need to use a statement that allows spaces to be processed as well, so I changed that to cin.get(message, messageLength + 1). My program was working perfectly (except that it didn't read spaces) until I changed the for loop statement to the get() statement. Now as soon as I get to the get statement, my program enters an infinite loop. I have tried using cin.clear(), cin.ignore() in different places around cin.get, and cin.ignore() works, but the problem is as soon as I press enter, it encrypts everything I input, and then changes the unused chars to null, which makes sense, but I need it to still accept chars after I press enter, until the array size(messageLength) is full. I can't figure out for the life of me how to do that. Any help on this would be greatly appreciated. Here is my main() where the problem is occurring:

int main()
{
int messageLength = 15;
int menuChoice = 0;
char message[MAX_MESSAGE_SIZE];

while (menuChoice != 3)
{
int counter = 0;

menuChoice = printMenu();

if (menuChoice == 3)
{
exitProgram();
}

messageLength = getMessageLength();
cout << "Enter the message below (newlines are ignored!):" << endl;

cin.get (message, messageLength + 1);

if (menuChoice == 1)
{
while (counter < messageLength)
{

cout << "Encrypting letter: ";
cout << message[counter] << endl;
stateOfRotors();
message[counter] = encryptMessageR1(message[counter]);
rotateRotors();
counter++;
}

cout << "Encrypted Message: ";
for (index = 0; index < messageLength; index++)
cout << message[index];
cout << endl;
resetRotors(messageLength);
}

if (menuChoice == 2)
{

while (counter < messageLength)
{
cout << "Decrypting letter: ";
cout << message[counter] << endl;
stateOfRotors();
message[counter] = decryptMessageR2(message[counter]);
rotateRotors();
counter++;
}


cout << "Decrypted Message: ";
for (index = 0; index < messageLength; index++)
cout << message[index];
cout << endl;
resetRotors(messageLength);
}
}

return (0);
}

If you need to see any of my functions I would be more than happy to post them. Thank you to anyone who attempts to help, even if it is not successful.
All user input is followed by an ENTER key press.
Therefore, you must eat a newline everytime you get input.

However, using >> does not eat them unless they are in the way.

For example, the program
1
2
3
4
5
6
7
8
9
#include <iostream>
using namespace std;
int main()
  {
  cout << "How old are you? " << flush;
  double age;
  cin >> age;
  return 0;
  }
D:\prog\foo> a.exe
What is your age? 


24
D:\prog\foo>
when compiled, will accept any number of ENTER key presses before you type in a valid number. But it will not read the ENTER key you pressed after you typed in a valid number!

So, if you chage the program to read
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>
using namespace std;
int main()
  {
  cout << "How old are you? " << flush;
  double age;
  cin >> age;
  cout << "What is your name? " << flush;
  string name;
  getline( cin, name );
  cout << "Hello " << name
       << ", you are " << (int)age
       << " years old!\n";
  return 0;
  }
D:\prog\foo> a.exe
What is your age? 24
What is your name?
Hello , you are 24 years old!
D:\prog\foo>
you will notice that the program never gives you a chance to enter your name.

That's because you didn't get rid of the newline (ENTER key press) from when you entered an age.

Modify the program once more to get rid of that pesky ENTER key press from the age line.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <limits>
#include <string>
using namespace std;
int main()
  {
  cout << "How old are you? " << flush;
  double age;
  cin >> age;
  cin.ignore( numeric_limits <streamsize> ::max(), '\n' );
  cout << "What is your name? " << flush;
  string name;
  getline( cin, name );
  cout << "Hello " << name
       << ", you are " << (int)age
       << " years old!\n";
  return 0;
  }
D:\prog\foo> a.exe
What is your age? 24
What is your name? Jennifer
Hello Jennifer, you are 24 years old!
D:\prog\foo>
Now both of your inputs (lines 9-10 and line 13) read and ignore the ENTER key the user pressed after typing her age and name.

And that is the cause and solution to your problem. Make sure that every time you use >> you follow it by a line like line 10 (don't forget to #include <limits> at the top of your program too). Then things like cin.get() and getline() will start behaving the way you expect -- because the input buffer is synchronized with your brain.

Hope this helps.
Last edited on
Yes this does help explain why I couldn't enter input after cin.get() was executed. Thank you very much. In the example output given to us, the program allows you to press enter and continue entering input until the array is full, and it ignored the newline character and didn't count it as part of the array size. I can make it so it accepts more input after enter is pressed if I use cin.read() but then it counts the newline as part of the array size. It's quite frustrating :-P
Topic archived. No new replies allowed.