cin.getline skipping first word in string?

Feb 12, 2011 at 7:15pm
Hi, I'm trying to write a program which prints a file's contents for the user, then allows the user to write a paragraph which then replaces the file's contents. Right now, my problem is correctly sending the user's input to the file. I'm using getline, but for some reason it is skipping the first word of whatever the user types. Also, I'm supposed to allow an empty line between paragraphs, but it's only reading the first paragraph. Can you help? Here is my code:

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
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main () {
   
    ifstream input; 
    ofstream output; 
    
    input.open("ADVICE.TXT");
        
    if (!input){
        cout << "No Dice.";
        exit(1);
    }
    
    string phrase1, phrase;
    
    while (getline(input, phrase1)){
        cout << "The last user wrote: " << phrase1 << endl;
    }
    
    input.close();
    
    cout << "Please enter new code advice: " << endl;
    cin >> phrase;
    
    
    output.open("ADVICE.TXT");
    
    
    while (getline(cin, phrase)){ //This is where the problem occurrs

    output << phrase << endl; //Whenever I look in the output file, it skips the first word in phrase
    cin.ignore();
    exit(1);
    
    }

    
    
    
    
    

    
    
    
    output.close();
    return 0;
    
}


Feb 12, 2011 at 7:31pm
closed account (zb0S216C)
Remove: cin >> phrase; on line 28.
Feb 12, 2011 at 8:12pm
Thank you! That worked perfectly.

Any advice on how to read in more than one line of text from the user?
Feb 12, 2011 at 9:06pm
closed account (zb0S216C)
You could add a EOF marker( http://www.cplusplus.com/reference/iostream/ios/eof/ ). However, a few questions have been raised about the reliability EOF.
Topic archived. No new replies allowed.