Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   Runtime error (http://www.go4expert.com/forums/runtime-error-t19583/)

Alex_code 29Sep2009 13:36

Runtime error
 
#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 //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 ; } }

shabbir 29Sep2009 13:39

Re: Runtime error
 
Check the post once again

SaswatPadhi 29Sep2009 18:23

Re: Runtime error
 
Please post you code,
(1) Properly indented
(2) Always within [code]...<your code>...[/code].

I have done it for you this time, but next time, your thread might be closed/removed.
Code: CPP

#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: CPP

//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: CPP

classList = new string [];

You need to mention the size inside [].


All times are GMT +5.5. The time now is 09:28.