Is this the right way to use getline()?

I posted this before but I've done a lot of changes so I hope this is right even though it isn't done. My code compiles, but when I run it, it won't let me enter the string I would like to encode. (Line 20 is where my problem is I believe). I previously used cin>>phrase; there but I couldn't put in spaces. Otherwise when I used cin>>phrase; my program worked exactly as I needed it to.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;


class Encoder
{
      private:
              string phrase;
              string encodedphrase;
              int key;
              
      public:
             

             void StorePhrase()
             {
                  cout << "Enter a the phrase you would like to encode:" << endl;
                  getline(cin, phrase);
             }
             
	         void StoreKey()
	         {
                  cout << "Enter an integer shift key between 0 and 20: " << endl;
                  cin >> key;
             }
             
             void EncodePhrase()
             {
                   int wlen = phrase.length();
                   cout << "Your encoded phrase is:" << endl;
                   for(int j=0; j<wlen; j++) {
	               phrase.at(j) = phrase.at(j) + key;
                   }
                   cout << phrase << endl;
             }
             
};               
                  
int main()
   {
          
   Encoder one;
   
   
   string command, Quit;
   
   cout << "Welcome to the Caeser Encoder/Decoder!" << endl;
   cout << "Would you like to Encode or Decode or Quit?" << endl;
   cin >> command;
   
   if (command == "Encode")

   {
   one.StorePhrase();
   one.StoreKey();
   one.EncodePhrase();
   }
   else 
	cout << endl;
   
   system("pause");
   return 0;
   }
When you use >> to read input, it leaves the newline (enter press) at the end of the buffer, so when you later go to use getline(), it reads that and thinks you pressed enter (resulting in an empty string). You can use the ignore() method throw away that newline character if you want.
So would I use cin.ignore();?
And would I use it on the line before getline? [line20] or the line after cin>>command;? [line51]

(Sorry for the questions, I've never used an ignore() before) Thanks!
Topic archived. No new replies allowed.