Please help with getline()!

Hello!
I am working on a simple C++ program to make an MLA work citer. Here is my current 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <iostream>
#include <string>
#define constrain(amt,low,high) ((amt)<(low)?(low):((amt)>(high)?(high):(amt)))
using namespace std;
int main(){
  int numAuthors;
  int numEditors;
  bool datePublished;
  string author[5];
  string editor[5];
  string date;
  string title;
  string dateAc;
  string terminate;
  string url;
  cout << "Welcome to Fang's MLA Citation Service!" << endl;
  cout << "Number of known authors : ";
  cin >> numAuthors;
  cout << endl;
  if (numAuthors > 0){
    cout << "Please enter the first author's name \"LAST, FIRST M.\"" << endl << "and other author's name \"FIRST M. LAST\"..." << endl;
    for(int i = 0; i < numAuthors; i++){
      cout << "Author " << (i+1) << ": ";
      cin.ignore(256, '\n');
      getline(cin, author[i]);
      cout << endl;
    }
  }
  else author[0] = "";
  cout << "Website title: ";
  //cin.ignore(256, '\n');
  getline(cin, title);
  cout << endl << "Number of Website Editors: ";
  cin >> numEditors;
  cout << endl;
  if (numEditors > 0){
    cout << "Please enter the editor's name(s) \"FIRST M. LAST\"..." << endl;
    for(int i = 0; i < numEditors; i++){
      cout << "Editor " << (i+1) << ": ";
      cin.ignore(256, '\n');
      getline(cin, editor[i]);
      cout << endl;
    }
  }
  else editor[0] = "N.p.";
  cout << "Is there a date published? ";
  cin >> datePublished;
  cout << endl;
  if (datePublished){
    cout << "Date \"DATE MONTH YEAR\": ";
    cin.ignore(256, '\n');
    getline(cin, date);
  }
  else date = "N.d.";
  cout << endl << "Enter date accessed \"DATE MONTH YEAR\": ";
  //cin.ignore(256, '\n');
  getline(cin, dateAc);
  cout << endl << "Enter URL: ";
  //cin.ignore(256, '\n');
  getline(cin, url);
  cout << endl << "Finished Citation:" << endl;
  for(int i = 0; i < numAuthors; i++){
    cout << author[i];
    if(i < numAuthors-1) cout << ", ";
    if (i == numAuthors-1 && numAuthors > 1) cout << "and ";
  }
  cout << ". <i>" << title << ".</i>";
  if(numEditors > 0) cout << " Ed. ";
  for(int i = 0; i < numEditors; i++){
    cout << editor[i];
    if(i < numEditors-1) cout << ", ";
    if (i == numEditors-1 && numEditors > 1) cout << "and ";
  }
  cout << ". " << date << ". Web. " << dateAc << ". <" << url << ">." << endl << endl << "Type something random to end the program: ";
  cin >> terminate;
  return 0;
}

I want the command prompt to ask for info to create an MLA citation for a website (e.g. author's name(s), title of page). The end result should be something like this:

Liang, Henry B., John S. Chen, and Daniel R. Tsui. <i>Yo Wazzup</i>. Ed. Martin B. Maccoroni and Shawn Z. Benz. N.p., 9 Feb. 2012. Web. 6 June 2012. <http:/www.bros.ca/>.

This is the actual command prompt screen:
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
C:\Windows\system32>mla
Welcome to Fang's MLA Citation Service!
Number of known authors : 3

Please enter the first author's name "LAST, FIRST M."
and other author's name "FIRST M. LAST"...
Author 1: Liang, Henry B.

Author 2: John S. Chen


Author 3: Daniel R. Tsui


Website title: Yo Wazzup

Number of Website Editors: 2

Please enter the editor's name(s) "FIRST M. LAST"...
Editor 1: Martin B. Maccoroni

Editor 2: Shawn Z. Benz


Is there a date published? 1

Date "DATE MONTH YEAR": 9 Feb. 2012

Enter date accessed "DATE MONTH YEAR": 6 June 2012

Enter URL: http://www.bros.ca/

Finished Citation:
Liang, Henry B., , and . <i>Yo Wazzup.</i> Ed. Martin B. Maccoroni, and . 9 Feb.
 2012. Web. 6 June 2012. <http://www.bros.ca/>.

Type something random to end the program: okay

C:\Windows\system32>

I made an environment variable on Windows 7. I don't think that I am doing the citation organization correctly, but that is not important. The problem is that the strings author[1], author[2], editor[1] did not contain what I have entered! Also, where you see 2 enters in a row, I have to press Enter twice to get it to spit out the next statement! That happened in:

1
2
3
4
Editor 2: Shawn Z. Benz


Is there a date published? 1


1
2
3
4
Author 3: Daniel R. Tsui


Website title: Yo Wazzup


1
2
3
4
Author 2: John S. Chen


Author 3: Daniel R. Tsui

How can I fix that? Am I using cin.ignore() and getline() improperly? How can I get the bad strings to contain the right value? How can I fix this whole thing?
Any help / lesson is appreciated!
Am I using cin.ignore() and getline() improperly?
I think so.

Start with the << operator. << does not extract the '\n'. This is troublesome for getline(), because it has a third argument that defaults to '\n'. So, you need to put a ignore() between the two. Otherwise the first thing getline sees is a '\n' and stops.

I would say to put ignore(256, '\n') directly after any << operation, not before the getline(), this way you know you cin buffer is empty for anything you might throw at it.

getline() however, does extract the '\n' (or whatever deliminator you give it as a third argument). So when you do getline(cin, str); cin.ignore(256, '\n'); your program is actually waiting for you to either give it 256 characters or a new line. That's why you have to press enter.
Last edited on
LowestOne wrote:
I would say to put ignore(256, '\n') directly after any << operation, not before the getline(), this way you know you cin buffer is empty for anything you might throw at it.

I don't get that. I tried removing all cout << endl; and cin.ignore(), but the results are the same. It is fine if there is just one author, and editor, but not if there is more than two. What is happening?
The strings author[1], author[2], editor[1], and editor[2] contain a " " space, when it should contain the name I entered! Is there another way to get string input with spaces?
Last edited on
You have endls within the loop, so every time it loops it's going to make a new line plus the new line that you make when you hit enter.

1
2
3
4
5
6
7
8
9
10
11
12
if (numAuthors > 0)
{
  cout << "Please enter the first author's name \"LAST, FIRST M.\"" << endl
       << "and other author's name \"FIRST M. LAST\"..." << endl;
  for(int i = 0; i < numAuthors; i++)
  {
    cout << "Author " << (i+1) << ": ";
    getline(cin, author[i]);
    // cout << endl; Not here
  }
  cout << endl; // Here
}
Last edited on
Topic archived. No new replies allowed.