getline function

Hi Programmers ^_^,

I always face same problem with getline. Could you please explain to me what I'm doing wrong. I would be very grateful.

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
#include <iostream>
#include <string>
using namespace std;

 struct node
 {
  node* next;
  string word;
  string mean;
 }*p;
 
 void append(string a, string b)
 {
  node *q,*t;
  if( p == NULL )
  {
   p = new node;
   p->word = a;
   p->mean = b;
   p->next = NULL;
   cout<< "\nIt's the first word in the Dictionary!" << endl;
  }
  else
  {
   q = p;
   while( q->next != NULL )
   q = q->next;
   t = new node;
   t->word = a;
   t->mean = b;
   t->next = NULL;
   q->next = t;
  }
 }

//----------- Main -------------
int main()
{
 char opt;
 char cont;
 string w;
 string m;
 
 do
 {
     cout<< " 1. Add word"
     <<endl<< " \n Your option is: "; cin>> opt;

     switch (opt)
     {
         case '1':
             cout<<"\n Enter word: "; getline(cin,w);
             cout<<" Enter the meaning of "<< w <<": "; getline(cin,m);
             append(w,m);
             break;
     }
     cout << "\nPress \"y\" to continue or any key to exit... "; cin >> cont;
     system("cls");
 }   while (cont == 'Y' || cont == 'y');
}


I can't enter value for "w".
The output will be like this:

 Enter word: Enter the meaning of: 
Last edited on
I always face same problem with getline.
Which is...?
1
2
     <<endl<< " 1. Add word"
     <<endl<< " \n Your option is: "; cin>> opt;


This does not make any sense. << is an operator on a stream object. use instead

1
2
3
4
     cout << endl << " 1. Add word";
     cout << endl << " \n Your option is: "; 
     cin >> opt;
     cin.ignore(numeric_limits<streamsize>::max(), '\n');


And include

1
2
#include <cstdlib> // system("cls")
#include <limits>   // numeric_limits<>::max 
Oops, sorry I added the problem now.
I am very irritated. Your source code you have given here can not compile on any system. You use << at nothing..

Even so, all your probs should be solved by the solution i provided.

Maikel
Don't combine std::getline() and std::cin >>. They just don't get along.
maikel: Now it's work. Thank you :). Can you show what this code does:

cin.ignore(numeric_limits<streamsize>::max(), '\n');

helios: I want to store a word into "w" and the meaning of that word into "m". I tried to use "cin>>" for "w" since it's just one word but then I couldn't enter values for both "w" and "m".
So then just move completely to std::getline(). There's nothing std::cin >> can do that std::getline() can't do better.
But I want to store just one word into "w"! how can I do that?
Aseel:

 
cin.ignore(numeric_limits<streamsize>::max(), '\n');


In fact this piece of code is it, why helios say >> and getline are not getting along.
The problem is that you readin a single character:

 
cin >> opt;


After this, if say, i typed '1' + Enter you read the character '1', but >> leaves the Enter in the input queue. If you now want to read a line with say getline, than getline will read the empty line before you can type anything. Because the Enter left by >> is still there.

Reminder: You type

 1. Add word
Your option is: 1\n

1 will be read as option but \n is still there.
So the next getline is automaticly back, because he will read: "\n" as line.

Next stop, what if:

 1. Add word
Your option os: 1i write a lot here blahblah.\n

Now whats happening? You will read 1 as character option and you leave the rest behind.
To stop this from happening, the ignore function is there.

 
cin.ignore(amount_of_chars_ignored, ignore_until_i_read_this)


it is used like this:

1
2
3
4
5
6
7
8
9
10
// ignore the next 4 chars or until you read a ' '
cin.ignore(4, ' ');
// ignore the next 10 chars or until you get an EOF
cin.ignore(10, EOF);
// ignore the next 10 chars or until you get an EOF (equivalent to upper)
cin.ignore(10);

// And now the gag. To get numerical limits in computation C++ has some facilities.
// IGNORE AS MUCH AS POSSIBLE until new line ;-)!
cin.ignore(numeric_limits<streamsize>::max(), '\n');


Maikel

I understand it now clearly. Thank you Maikel. finally I wouldn't get this problem again ^_^.
Topic archived. No new replies allowed.