My program displays the details of 2 students. name id address module code and module marks. my program works well but the problem is when i enter the details of the 2nd student i am not able to enter the name as if it skip this first part and go directly to id here are my codes:

#ifndef STUDENT_H
#define STUDENT_H


class student
{
    public:
        void enterDetails();
        void enterModule();
        void displayDetails();
        float computeCPA();

    private:
        char name[100];
        char studID[100];
        char address[100];
        char module[100];
        float marks[100];
        float CPA[100];
        int M[20][2];
};



#include <cstdlib>
#include <iostream>
using namespace std;
#include "student.h"

void student::enterDetails()
{
    cout<<"enter name: ";
    cin.getline(name,100,'\n');
    cout<<"enter id: ";
    cin.getline(studID,100,'\n');
    cout<<"enter address of student: ";
    cin.getline(address,100,'\n');
    cin.ignore();
}

void student::enterModule()
{


    cin.ignore();
}

void student::displayDetails()
{
    cout<<"Name: "<<name<<endl;
    cout<<"ID: "<<studID<<endl;
    cout<<"Address: "<<address<<endl;
    cin.ignore();
}




#include <cstdlib>
#include <iostream>
#include "student.h"

using namespace std;

int main(int argc,char *argv[])
{
int x;
string M[20][2];
int row,col;
const int MAXSTUDENT = 2;
student s1[MAXSTUDENT];
cout<<"Processing details of Student"<<endl;
for (int i=0; i<MAXSTUDENT; i++){
s1[i].enterDetails();

cout<<"Enter number of modules:" ;
cin>> x;
for(row=0;row<x;row++)
{
cout << " Enter module code and corresponding module marks  :" ,(row+1);
for(col=0;col<2;col++)
cin >> M[row][col];
}

}
cout<<"Displaying details of Student"<<endl;
for (int i=0; i<MAXSTUDENT; i++){
s1[i].displayDetails();
for (row=0; row<x; row++)
{
for(col=0;col<2;col++)
cout << "\t" <<M[row][col];
cout<< endl;
}
}
return EXIT_SUCCESS;
}

line 84: flush the input stream here. You don't need cin.ignore() in the other places where you have it.

Also, study this thread how to flush the input stream. Just using cin.ignore() may not do it.

#include <limits>
...
cin.ignore(std::numeric_limits<std::streamsize>::max(), cin.widen('\n'));

It is just that you have left a trailling '\n' in the input stream.

You can tidy that up with code like this:

for (int i=0; i<MAXSTUDENT; i++)
    {
      s1[i].enterDetails();
      std::cout<<"Enter number of modules:" ;
      std::cin>> x;
      for(row=0;row<x;row++)
        {
          std::cout << 
            " Enter module code and marks  :" <<(row+1)<<" ";
          for(col=0;col<2;col++)
            std::cin >> M[row][col];
    }
 std::cin.ignore
     (std::numeric_limits<std::streamsize>::max(),'\n'); 

Note the last line is std::cin.ignore(Number,character).
What is happening is that the std::numeric_limits<std::streamsize>::max is effectively a value.
It is telling ignore to read at that many characters and ignore all of them until the first instance of '\n' AND if it doesn't find one, then read to the end of the string.

If you were to write say :std::cin.ignore(10,'X'); then that would skip the input stream until (a) 10 characters have been read and discarded, (b) it is at an X (including skipping the X). (c) until the end of the stream IF there were less than 10 characters.

NOTE: If you want std::numeric_limits you need to add the header
#include <limits> to your C++ code.

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.