0

Im working c++ for school and simply cannot understand why after my inputs, it ignores first word. I've tryed already to find solution here, cause i saw topics about it, but non of them helped me. Simply, only first input is ok. All others are missing 1 word at beginning.

Here is the code:

void vozilo::Unos() {

// Marka vozila
cout << "Unesite marku vozila (do 50 karaktera): ";
markaVozila = new char[50];
cin.ignore();
cin.getline(markaVozila, 50);

// Broj sasije
cout << "Unesite broj sasije (do 20 karaktera): ";
brojSasije = new char[20];
cin.ignore();
cin.getline(brojSasije, 20);

cout << endl << endl; 
}

https://pastebin.com/hkLW77HX - full code.

And here is what is the problem problem

acraig5075
  • 9,913
  • 3
  • 29
  • 45
Danis
  • 1
  • Do you know what `cin.ignore()` does? Why did you include that line in your code? – Yksisarvinen Jan 25 '21 at 10:50
  • Without it, it ignores my first input. If i remove it, it put both cout's at same time. – Danis Jan 25 '21 at 10:56
  • So, you probably have `std::cin >> someVariable" before, right? If yes, then this question will be helpful: [Why does std::getline() skip input after a formatted extraction?](https://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction). The issue is that you wanted to skip a newline that remained in the stream, but ended up skipping the first character in every read. – Yksisarvinen Jan 25 '21 at 11:01
  • I have provided full pastebin code before, i dont have std::getline, only cin.getline function, full code provided is in pastebin link under function. – Danis Jan 25 '21 at 11:04

1 Answers1

0

It's necessary use cin.ignore before read with cin.get() to clean the buffer.

void vozilo::Unos() {
    // Marka vozila
    cout << "Unesite marku vozila (do 50 karaktera): ";
    markaVozila = new char[50];
    cin.ignore();
    cin.get(markaVozila, 50);

    // Broj sasije
    cout << "Unesite broj sasije (do 20 karaktera): ";
    brojSasije = new char[20];
    cin.ignore();
    cin.get(brojSasije, 50);

    cout << endl << endl;
}
frux09
  • 81
  • 6
  • Maybe it's better, but i need to use char, its for school, just look whole code. – Danis Jan 25 '21 at 12:45
  • You can use `>>`operator with char: `int main() { char *varA = new char [50]; char *varB = new char [30]; std::cout << "Enter var A "; std::cin >> varA; std::cout << "Enter var B"; std::cin >> varB; std::cout << varA << " - " << varB << std::endl; } ` – frux09 Jan 25 '21 at 13:22
  • Cause it just takes 1 word, for example: If I type Mercedes Benz, it will ignore second input (brojSasije) and it will appear empty. Test code without cin.ignore & cin.getline with just cin, and u will see (test my code). – Danis Jan 26 '21 at 13:07
  • Yes, I didn't try... Maybe you need use both options: `cin.ignore` and `cin.get()` like this; `markaVozila = new char[50]; cin.ignore(); cin.get(markaVozila, 50);` and add another `cin.ignore` after read `kolone` because you need to clean the input buffer – frux09 Jan 26 '21 at 13:39