0
#include <iostream>
#include <string.h>
using namespace std;
int nos;
string stddata[1000][2];
void pop(int);
void output(int);
int main()
{

cout<<"Enter number of students: ";
cin>>nos;
pop(nos);
output(nos);
}

void pop(int nos)
{
for(int i=0;i<nos;i++)

    for(int il=0; il<2;il++)
    
        getline(cin,stddata[i][il]);    // when i=0 && il = 1 compiler skips it
}
void output(int nos)
{
cout<<"\t\t RESULT \n";

for(int i=0;i<nos;i++)
    for(int il=0; il<2; il++)
        cout<<stddata[i][il]<<"\t"
}

inner loop of function pop then loop has to execute 2 times but it executes only one time skipped the 2nd one why it is happening? only when we using getline

  • When you provided your input for the `nos` (cin >> nos), you not only submitted the following integers, but also an implicit newline was appended to the stream. To avoid this use `cin.ignore()` after `cin>>nos` it will discard the next available integer so that new line is not on the way. – Linear Data Structure Jan 27 '21 at 22:37
  • i already use cin.ignore and cin.clear but these are not working – Muneeb Aslam Feb 02 '21 at 19:31

0 Answers0