Hello to all! I am a begginer to C++ and writing a class . Actually my program runs perfectly but there is logical error. My compiler is devC++. the output of my program should be as follows:

Enter your name: john
Enter your rollno: 45
Enter your semester:4

Enter your name: Michael
Enter you roll no: 78
Enter your semester:3

Name of studend1 is John
Rollno of student1 is 45
Semester of student1 is 4

Name of student2 is Michael
Rollno of student2 is 78
Semester of student2 is 3

Description of error : First six lines appears perfecly on console window but 2nd six lines do not display.
I am attaching a notpad file on which my code is written. Please put my code on your compiler and debug it and also tell me that what mistake i made.

Recommended Answers

All 4 Replies

Sorry i forgot to attach file in first post . i am attaching file in this post

i am pasting my code snipet in this post

#include<iostream.h>
class id
{
     private:
             char *name;                      // declaration of private data members of class 
             int rollno, semester ;

     public:
            void get_name(char *name)           // definition of member functions of class
            {                           
               cout<<"enter your name"<<"\t";           
                cin>>name;          
            }

              void get_rollno(int rollno)
            {
             cout<<"enter your roll_no"<<"\t";
             cin>>rollno;
             }
             void get_semester(int semester)
             {
               cout<<"enter your semester"<<"\t";
               cin>>semester;
               cout<<endl;
               }       
                void display( )                     // definition of member function to display values
                {
                   cout<<"name of student"<<"\t"<<name;
                   cout<<"rollno of student"<<"\t"<<rollno;
                   cout<<"semester of student"<<"\t"<<semester;
                }   


};           
main()                            // main body
{ 

      int  rollno, semester;
      char *name;
      id obj1;

      for( int i=0;i<2;i++)              // using for loop to get data of 2 students
      {
      obj1.get_name(name);                       // calling member functions of objects
      obj1.get_rollno(rollno);
      obj1.get_semester(semester);
        }
      for(int j=0;j<2; j++)      // this loops displays the data of 2 students
       {
         obj1.display();
       }          
      system("pause");
}                          

Your code above will NOT compile error free.

See the '1st round' of corrections suggested ...
(see changes and added comments)

//#include<iostream.h>

#include <iostream>

// need to add
using namespace std;

class id
{
private:
    //char *name;                      // declaration of private data members of class
    char name[80]; // need to resevere memory to hold this
    int rollno, semester ;

public:
    void get_name(/* char *name*/ )           // definition of member functions of class
    {
        cout<<"enter your name: ";
        //cin>>name;    //
        cin.getline( name, sizeof(name) );
    }

    void get_rollno(/* int rollno*/ )
    {
        cout<<"enter your rollno: ";

        cin>>rollno; // but will crash on non int input

        // NEED to add (at least) something like this ...
        cin.sync(); // 'flush' cin stream ...
    }
    void get_semester(/* int semester*/ )
    {
        cout<<"enter your semester ";
        cin>>semester; // but will crash on non int input //

        // NEED to add (at least) something like this ...
        cin.sync(); // 'flush' cin stream ...
        cout<<endl;
    }
    void display( )                     // definition of member function to display values
    {
        cout << "name of student: " << name;
        cout << ", rollno of student: " << rollno;
        cout << ", semester of student: " << semester;
    }


};           

//main()                            // main body
int main()
{ 

    //int  rollno, semester; // NOT USED //
    //char *name;

    id obj1;

    for( int i=0;i<2;i++)              // using for loop to get data of 2 students
    {
        //obj1.get_name(name);                       // calling member functions of objects
        //obj1.get_rollno(rollno);
        //obj1.get_semester(semester);
        obj1.get_name();                       // calling member functions of objects
        obj1.get_rollno();
        obj1.get_semester();

    //}
    //for(int j=0;j<2; j++)      // this loops displays the data of 2 students
    //{
        obj1.display();
        // need to add next line
        cout << endl;
    }
    // system("pause");  // NOT PORTABLE
}                          

But ... better to use C++ string and getline for input of names.

Also ... you will want to validate / handle bad (numeric) input (especially - so your program will not crash.)

Also ... you will want an array (or some other C++ container like a vector) to hold your 'Student' objects ...

This example may get you started ...

// classStudent_test.cpp //

#include <iostream>
#include <iomanip> // re. setw
#include <string>

using namespace std;

const int MAX_NUM_STUDENTS = 3; // keep small while testing //

class Student
{
private:
    int semester, rollno;
    string name;

public:
    void input_semester()
    {
        cout << "Enter student's semester: ";
        while( !( cin >> semester ) || cin.get() != '\n'
               || semester <= 0 || semester > 4 )
        {
            cin.clear();
            cin.sync();
            cout << "\nOnly integers in range 1..4 accepted here ...\n";
            cout << "Enter student's semester: ";
        }
        cin.sync();
    }
    void input_rollno()
    {
        cout << "Enter student's roll_no: ";
        while( !( cin >> rollno ) || cin.get() != '\n'
               || rollno <= 0 )
        {
            cin.clear(); // clear all error flags
            cin.sync(); // 'flush' din stream ...
            cout << "\nOnly valid positive integers accepted here ...\n";
            cout << "Enter student's roll_no: ";
        }
    }
    void input_name()
    {
        cout << "Enter student's name (last, first): ";
        getline( cin, name );
    }

    void display() const // definition of member function to display values
    {
        cout << "Semester: " << semester
             << ", Rollno: " << setfill( '0' ) << setw(4) << rollno
             << ", Name: "  << name << setfill( ' ' );
    }

    int get_rollno() const { return rollno; }
} ;


bool exists_rollno( const Student ary[], unsigned size, int findno )
{
    for( unsigned i = 0; i < size; ++ i )
         if( findno == ary[i].get_rollno() ) return true;
    // else ... if reach here ...
    return false;
}

// utilities ...

char takeInChr( const string& msg )
{
    cout << msg << flush;
    string reply;
    getline( cin, reply );
    if( reply.size() )
        return reply[0];
    // else ...
    return 0;
}
bool more()
{
    if( tolower( takeInChr( "More (y/n) ?" ) ) == 'n' )
        return false;
    // else ...
    return true;
}



int main() // NOTE! in standard C++ and C main returns an int //
{ 
    Student ary[MAX_NUM_STUDENTS]; // get memory to hold students ...

    int size = 0;
    do  // loop to get more students while still space and more
    {
        Student obj;

        obj.input_semester();

        for( ; ; )
        {
            obj.input_rollno();
            if( !exists_rollno( ary, size, obj.get_rollno() ))
                break;
            // else ... if reach here ...
            cout << "\nThat rollno "<< obj.get_rollno()
                 << " already exists ...\n";
        }

        obj.input_name();

        ary[size] = obj;
        cout << endl;

        ++size;

        if( size == MAX_NUM_STUDENTS )
        {
            cout << "\nYou have reached the max number of "
                 << MAX_NUM_STUDENTS << " permitted here.\n";
            break;
        }
    }
    while( more() );

    // loops to display all the students ...
    cout << "\nShowing all the students at present ... \n";
    for( int j = 0; j < size; j++ )
    {
        ary[j].display();
        cout << endl;
    }

    cout << "\nPress 'Enter' to continue/exit ... " << flush;
    cin.get();
}                          
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.