Using Getline Problem.

Alright so I recently learned getline, from a tutorial and I looked it up for examples on this website and
This is the example provided from the website:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// istream getline
#include <iostream>
using namespace std;

int main () {
  char name[256], title[256];

  cout << "Enter your name: ";
  cin.getline (name,256);

  cout << "Enter your favourite movie: ";
  cin.getline (title,256);

  cout << name << "'s favourite movie is " << title;

  return 0;
}

my code:
1
2
3
4
5
6
7
8
9
10
char AiDee[8], AitemName[20], AitemProducer[20];
int number;
     cout<<"Enter ID: ";
     cin >> AiDee;
     cout<<"Enter Item Name: ";
     cin.getline (AitemName,20);
     cout<<"Enter Producer: ";
     cin.getline (AitemProducer,20);
     cout<<"Enter Quantity: ";
     cin >> number;


My output is like this :

Enter ID: 49583
Enter Item Name: Enter Producer:


Why does it skip line? What did I do differently than the example?
(IF I took out both other
cin>>
then It would work o-o. )
Last edited on
Using operator>>() to read from std::cin sucks. Its behavior is completely unintuitive: it always leaves a newline in the input buffer, so if you mix reading methods, something is bound to go wrong. Use exclusively getline(). In fact, I'd recommend std::getline() plus std::string.
How do you use getline()?

I tried from a tutorial
and he did
string x;
getline(cin, x);

I tried this and I got an error before saying "no matching function for call to `getline(std::istream&, char[20])'"

My code, I didn't know if this was right:

 
getline(cin, AitemName);
Last edited on
char AiDee[8], AitemName[20], AitemProducer[20];
int number;
cout<<"Enter ID: ";
cin >> AiDee;

cin.ignore(1);



cout<<"Enter Item Name: ";
cin.getline (AitemName,20);
cout<<"Enter Producer: ";
cin.getline (AitemProducer,20);
cout<<"Enter Quantity: ";
cin >> number;
Omg. Thank you so much! You're a life saver! :D .

I don't know what it does but it works ^_^!!!!
Topic archived. No new replies allowed.