0

I should write a code, which will print the names and faculties of those students, whose years and groups are equal to the numbers which the user inputs, but after compiling, it doesn't leave space for writing the name, why?

#include <iostream>
using namespace std;

struct student
{
    char name[40];
    char faculty[20];
    int year;
    int group;
};

void input(student &x)
{
    cout << "Name: "; cin.getline(x.name, 40);
    cout << "Faculty: "; cin.getline(x.faculty, 20);
    cout << "Year: "; cin >> x.year;
    cout << "Group: "; cin >> x.group;
}

int main()
{
    const int n = 4;
    student arr[n];

    int a, b;

    cout << "Input number 1: ";
    cin >> a;
    cout << "Input number 2: ";
    cin >> b;

    cout << "_______________________________" << endl;

    for (int i = 0; i < n; i++)
         input(arr[i]);

    for (int i = 0; i < n; i++)
        if (a == arr[i].year && b == arr[i].group)
            cout << arr[i].name << " - " << arr[i].faculty << endl;

    return 0;
}
JenJen
  • 3
  • 3
  • Been asked many, many times, here's one duplicate https://stackoverflow.com/questions/5739937/using-getlinecin-s-after-cin The very first comment explains the problem quite well. – john Apr 04 '20 at 12:56
  • Try adding `cin.ignore(1, '\n');` right after `cin >> b;` – tuket Apr 04 '20 at 13:05
  • You can use `cin >> b >> std::ws;` to discard the trailing whitespace. [std::ws](https://en.cppreference.com/w/cpp/io/manip/ws) – Eljay Apr 04 '20 at 13:13
  • @tuket it worked, but I don't really understand what it does, can you make it clear? I'm sorry I'm a beginner, thanks in advance! – JenJen Apr 04 '20 at 13:15
  • @tuket nevermind, I got it, thanks!! – JenJen Apr 04 '20 at 13:32

0 Answers0