Runtime error

Discussion in 'C++' started by Alex_code, Sep 29, 2009.

  1. Alex_code

    Alex_code New Member

    Joined:
    Sep 10, 2009
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    #include <string> #include <iostream> using std::cin ; using std::cout ; using std::flush ; using std::endl ; using std::string ; typedef string * strPtr ; [FONT=&quot]class[/FONT] [FONT=&quot]Student[/FONT] { public: [FONT=&quot]Student[/FONT] ( ) ; //default ctor [FONT=&quot]Student[/FONT] ( string sName , int numC , string cList[ ] ) ; //parameterized ctor [FONT=&quot]Student[/FONT] ( [FONT=&quot]Student[/FONT] & cpy ) ; //copy ctor ~Student ( ) ; //dtor [FONT=&quot]Student[/FONT] operator = ( const [FONT=&quot]Student[/FONT] & rtSide ) ; void input ( ) ; //input data from user void output ( ) ; //output data void resData ( ) ; //reset numClass and [FONT=&quot]classList[/FONT][] private: int numClass ; string [FONT=&quot]name[/FONT] ; strPtr [FONT=&quot]classList[/FONT] ; } ; #include "prob4.h" int main ( ) { string s2Classes[] = { "Psychology" , "C++" , "Government" , "NV.NET" , "C#" } ; [FONT=&quot]Student[/FONT] s1 ; [FONT=&quot]Student[/FONT] s2 ( "Lilly" , 5 , s2Classes ) ; s1.input ( ) ; s1.output ( ) ; s2.output ( ) ; return 0 ; } //File: prob4.func //header file #include "prob4.h" [FONT=&quot]Student[/FONT]::[FONT=&quot]Student[/FONT] ( ) : numClass ( 4 ) , [FONT=&quot]name[/FONT] ( "John Doe" ) , [FONT=&quot]classList[/FONT] ( NULL ) { /*Body intentionally empty*/ } [FONT=&quot]Student[/FONT]::[FONT=&quot]Student[/FONT] ( string sName , int numC , string cList[ ] ) : [FONT=&quot]name[/FONT] ( sName ) , numClass ( numC ) { [FONT=&quot]classList[/FONT] = new string [ ] ; for ( int i = 0 ; i < numClass ; i++ ) [FONT=&quot]classList[/FONT][ i ] = cList[ i ] ; } [FONT=&quot]Student[/FONT]::[FONT=&quot]Student[/FONT] ( [FONT=&quot]Student[/FONT] & cpy ) : [FONT=&quot]name[/FONT] ( cpy.[FONT=&quot]name[/FONT] ) , numClass ( cpy.numClass ) { [FONT=&quot]classList[/FONT] = new string[ numClass ] ; for ( int i = 0 ; i < numClass ; i++ ) [FONT=&quot]classList[/FONT][ i ] = cpy.[FONT=&quot]classList[/FONT][ i ] ; } [FONT=&quot]Student[/FONT]::~Student() { delete [ ] [FONT=&quot]classList[/FONT] ; } [FONT=&quot]Student[/FONT] [FONT=&quot]Student[/FONT]::eek:perator =(const [FONT=&quot]Student[/FONT] &rtSide) { if ( [FONT=&quot]name[/FONT] == rtSide.[FONT=&quot]name[/FONT] ) return *this ; numClass = rtSide.numClass ; delete [] [FONT=&quot]classList[/FONT] ; [FONT=&quot]classList[/FONT] = new string [] ; for ( int i = 0 ; i < numClass ; i++ ) [FONT=&quot]classList[/FONT] = rtSide.[FONT=&quot]classList[/FONT] ; return *this ; } void [FONT=&quot]Student[/FONT]::input ( ) { cout << "This program reads a [FONT=&quot]student[/FONT] [FONT=&quot]name[/FONT] and the number and [FONT=&quot]name[/FONT] of the classes.\n" ; cout << "Enter the [FONT=&quot]student[/FONT] [FONT=&quot]name[/FONT]: " << flush ; getline ( cin , [FONT=&quot]name[/FONT] , '\n' ) ; cout << "Enter the number of classes: " << flush ; cin >> numClass ; cout << "Enter " << numClass << " classes, each followed by pressing 'Enter':\n" ; for ( int i = 0 ; i < numClass ; i++ ) getline( cin , [FONT=&quot]classList[/FONT] [ i ] ) ; } void [FONT=&quot]Student[/FONT]::resData( ) { numClass = 0 ; [FONT=&quot]classList[/FONT] = NULL ; } void [FONT=&quot]Student[/FONT]::eek:utput() { cout << "Outputting [FONT=&quot]student[/FONT] data for: " << [FONT=&quot]name[/FONT] << endl ; for ( int i = 0 ; i < numClass ; i++ ) { cout << "\t" << i+1 << ".\t" << [FONT=&quot]classList[/FONT][ i ] << endl ; } }
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Check the post once again
     
  3. SaswatPadhi

    SaswatPadhi ~ Б0ЯИ Τ0 С0δЭ ~

    Joined:
    May 5, 2009
    Messages:
    1,342
    Likes Received:
    55
    Trophy Points:
    0
    Occupation:
    STUDENT !
    Location:
    Orissa, INDIA
    Home Page:
    http://www.crackingforfun.blogspot.com
    Please post you code,
    (1) Properly indented
    (2) Always within [noparse]
    Code:
    ...<your code>...
    [/noparse].

    I have done it for you this time, but next time, your thread might be closed/removed.
    Code:
    #include <string>
    #include <iostream>
    using std::cin;
    using std::cout ;
    using std::flush ;
    using std::endl ;
    using std::string ;
    typedef string * strPtr ;
    class Student
    {
    public:
        Student ( ) ; //default ctor
        Student ( string sName , int numC , string cList[ ] ) ; //parameterized ctor
        Student ( Student & cpy ) ; //copy ctor
        ~Student ( ) ; //dtor
        Student operator = ( const Student & rtSide ) ;
        void input ( ) ; //input data from user
        void output ( ) ; //output data
        void resData ( ) ; //reset numClass and classList[]
    private:
        int numClass ;
        string name ;
        strPtr classList ;
    };
    #include "prob4.h"
    int main ( )
    {
        string s2Classes[] = { "Psychology" , "C++" , "Government" , "NV.NET" , "C#" } ;
        Student s1 ;
        Student s2 ( "Lilly" , 5 , s2Classes ) ;
        s1.input ( ) ;
        s1.output ( ) ;
        s2.output ( ) ;
        return 0 ;
    }
    File: prob4.func
    Code:
    //header file
    #include "prob4.h"
    Student::Student ( ) : numClass ( 4 ) , name ( "John Doe" ) , classList ( NULL )
    { /*Body intentionally empty*/ }
    Student::Student ( string sName , int numC , string cList[ ] ) : name ( sName ) , numClass ( numC )
    {
        classList = new string [];
        for ( int i = 0 ; i < numClass ; i++ ) classList[ i ] = cList[ i ] ;
    }
    Student::Student ( Student & cpy ) : name ( cpy.name ) , numClass ( cpy.numClass )
    {
        classList = new string[ numClass ] ;
        for ( int i = 0 ; i < numClass ; i++ ) classList[ i ] = cpy.classList[ i ] ;
    }
    Student::~Student()
    {
        delete [ ] classList ;
    }
    Student Student::operator =(const Student &rtSide)
    {
        if ( name == rtSide.name ) return *this ;
        numClass = rtSide.numClass ;
        delete [] classList ;
        classList = new string [] ;
        for ( int i = 0 ; i < numClass ; i++ ) classList = rtSide.classList ;
        return *this ;
    }
    void Student::input ( )
    {
        cout << "This program reads a student name and the number and name of the classes.\n" ;
        cout << "Enter the student name: " << flush ;
        getline ( cin , name , '\n' ) ;
        cout << "Enter the number of classes: " << flush ;
        cin >> numClass ;
        cout << "Enter " << numClass << " classes, each followed by pressing 'Enter':\n" ;
        for ( int i = 0 ; i < numClass ; i++ ) getline( cin , classList [ i ] ) ;
    }
    void Student::resData( )
    {
        numClass = 0 ;
        classList = NULL ;
    }
    void Student::output()
    {
        cout << "Outputting student data for: " << name << endl ;
        for ( int i = 0 ; i < numClass ; i++ )
        {
            cout << "\t" << i+1 << ".\t" << classList[ i ] << endl ;
        }
    }
    You should also mention something abt what exactly you want us to do with your code, instead of just dumping it here.

    BTW, your code doesn't compile since you have declarations like :
    Code:
        classList = new string [];
    You need to mention the size inside [].
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice