cout << "Introduceti numele: "; // 1
			getline(cin, firstname);
			cin.ignore();
			p.setNume(firstname);
			cout << "Introduceti adresa: "; //2
			getline(cin, adr);
			cin.ignore();
			p.setAdresa(adr);
			cout << "Introduceti CNP: "; //3
			cin >> c_n_p;
			cin.ignore();
			p.setCnp(c_n_p);
			cout << "Introduceti seria si numarul buletinului: "; //4
			getline(cin, serie);
			cin.ignore();
			p.setSerienr(serie);
			cout << "Introduceti valabilitatea buletinului: "; //5
			getline(cin, val);
			cin.ignore();
			p.setValabilitate(val);

after entering data for cout number 2 it skips the rest of the cout's,why is that ?

Introduceti numele: a s
Introduceti adresa: sd rf
Introduceti CNP: Introduceti seria si numarul buletinului: Introduceti valabilitatea buletinului:

Recommended Answers

All 10 Replies

I don't understand the problem? I removed all of the code that we can't compile and the text and came up with this:

#include <iostream>
#include <string>
using namespace std;

int main()
{

  string test;
  
  cout << "1: " << endl;
  getline(cin, test);
  cin.ignore();
  cout << test << endl;

  cout << "2: " << endl;
  getline(cin, test);
  cin.ignore();
  cout << test << endl;
  
  cout << "3: ";
  getline(cin, test);
  cin.ignore();
  cout << test << endl;

  
  return 0;
}

Does this not behave as you would expect?

Dave

OK, yours is working,but mine isnt ....and i can`t figure out why

The only difference is that you have a cin >> c_n_p; in the middle. What is the type of c_n_p? Hopefully someone can tell you how this cin statement interacts with the getline and cin.ignore() statements.

c_n_p is long long

try this

cin.ignore(80, '\n');
// instead of
cin.ignore();

Thanks its working now,but i have to press Enter twice everytime,how can i changed the code,to press only once ? :)

you just need the ignore call after you do cin >> c_n_p; . When you change from unformatted input to formatted input you will need to call ignore.

getline(cin, something);
cin >> foo;
cin.ignore(80, '\n');  // need this here for getline to work
getline(cin. somethingelse);

// without a cin >> you don't need anything
getline(cin, something);
getline(cin, somethingelse);
getline(cin, bar);
// no ignore here since they are all getline calls

the reason for this is that cin >> foo leaves the newline in the input buffer where getline(cin, foo) will extract the newline from the input buffer.

if i use it only after cin , i have the same problem i had at the start,to work i need to put cin.ignore(80, '\n'); after all getline`s and cin....

I don't know why you have to call ignore after consecutive getline calls. Here is a little sample code I wrote and it works just fine.

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string a,b,d;
    int c;
    cout << "enter a string: ";
    getline(cin, a);
    cout << "enter another string: ";
    getline(cin, b);
    cout << "enter an integer: ";
    cin >> c;
    cin.ignore(80, '\n');
    cout << "enter one last string: ";
    getline(cin, d);
    cout << endl << a << endl << b << endl << c << endl << d;
    cin.get();
    return 0;
}
cout << "Introduceti numele: ";
			getline(cin, firstname);
			p.setNume(firstname);
			cout << "Introduceti adresa: ";
			getline(cin, adr);
			p.setAdresa(adr);
			cout << "Introduceti CNP: ";
			cin >> c_n_p;
			cin.ignore(80 , '\n');
			p.setCnp(c_n_p);
			cout << "Introduceti seria si numarul buletinului: ";
			getline(cin, serie);
			p.setSerienr(serie);
			cout << "Introduceti valabilitatea buletinului: ";
			getline(cin, val);
			p.setValabilitate(val);

Output:

Introduceti numele: Introduceti adresa: bla bla
Introduceti CNP: 1234566677
Introduceti seria si numarul buletinului: sdfsdfs
Introduceti valabilitatea buletinului: sdfsfsfs
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.