I get

error C2143: syntax error : missing ';' before '*'[/code] at the typedef line (and every other line with the word 'string').

...

//File:        prob4.h

#pragma once

//header files + efficiency
#include <string>
#include <iostream>
using std::cin ;
using std::cout ;
using std::endl ;

typedef string * strPtr ;

//Student Class
//    stores the name of the student, number of classes,
//    and the names of classes taken.  Functions provided
//    for accessing and mutating the data.
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 ;
} ;

Recommended Answers

All 12 Replies

String is part of the standard library so either:

using std::string ;

or std:: all strings.

sweet thanks.

Alright, got all the syntax errors out, but I'm getting a runtime error.

Here's the full code.

//File:        prob4.h

#pragma once

//header files + efficiency
#include <string>
#include <iostream>
using std::cin ;
using std::cout ;
using std::flush ;
using std::endl ;
using std::string ;

typedef string * strPtr ;

//Student Class
//    stores the name of the student, number of classes,
//    and the names of classes taken.  Functions provided
//    for accessing and mutating the data.
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[] = { "Cisco" , "C++" , "Economics" , "NV.NET" , "C#" } ;

    Student s1 ;
    Student s2 ( "Caleb" , 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 ;
    }
}

Error Dialog:

Unhandled Exception: System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt...

Student::Student ( string sName , int numC , string cList[ ] ) : name ( sName ) , numClass ( numC )
{
    classList = new string [numClass ] ;
    for ( int i = 0 ; i < numClass ; i++ )
        classList[ i ] = cList[ i ] ;
}

You must specify the size of the array you're allocating. Can'e new[]!

ahhh! excellent!

Also you can put the initialisation in the initialisation list:

Student::Student ( string sName , int numC , string cList[ ] ) : name ( sName ) , numClass ( numC ), classList( new string [numClass] )
{
    for ( int i = 0 ; i < numClass ; i++ )
        classList[ i ] = cList[ i ] ;
}

Might as well use it when you can.

void Student::input ( )
{
    cout << "This program reads a student name and the number & 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" ;
    classList = new string [ numClass ] ;
    for ( int i = 0 ; i < numClass ; i++ )
        getline( cin , classList [ i ] ) ;
}

This skips the first iteration of the for loop.
It starts asking for classes, and if I put 4 as numClass it stops letting me input classes at 3. Here's a sample output:

This program reads a student name and the number & name of the classes.
Enter the student name: Matt
Enter the number of classes: 4
Enter 4 classes, each followed by pressing 'Enter':
History
Math
Science
Outputting student data for: Matt
1.
2. History
3. Math
4. Science
Outputting student data for: Caleb
1. Cisco
2. C++
3. Economics
4. NV.NET
5. C#
Press any key to continue . . .

Also you can put the initialisation in the initialisation list:

Student::Student ( string sName , int numC , string cList[ ] ) : name ( sName ) , numClass ( numC ), classList( new string [numClass] )
{
    for ( int i = 0 ; i < numClass ; i++ )
        classList[ i ] = cList[ i ] ;
}

Might as well use it when you can.

oh ok, didn't know you could do that. Thanks, I'll add that in now.

Your other problem use cin.ignore();

Mixing up getline() and cin>> and getline() again will store '\n''s in the the buffer which getline() will recognise and hence pass out.

so i do cin.ignore ( '\n' ) where?

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 ;

    delete []classList;
    classList = new string[numClass];
    cin.ignore();
 
    cout << "Enter " << numClass << " classes, each followed by pressing 'Enter':\n" ;
    for ( int i = 0 ; i < numClass ; i++ )
        getline( cin , classList [ i ] ) ;
}

I highlighted the changes in red. You have to delete what's there and re-allocate it so that you don't mess it up! And the ignore goes after the >> but before the getline()

This program reads a student name and the number and name of the classes
Enter the student name: 5
Enter the number of classes: 5
Enter 5 classes, each followed by pressing 'Enter':
d
d
d
d
d
Outputting student data for: 5
1. d
2. d
3. d
4. d
5. d
Outputting student data for: Caleb
1. Cisco
2. C++
3. Economics
4. NV.NET
5. C#

commented: Excellent help. +4

Awesome, it's working great! I need to put some test code in main for my other functions, but I don't see them being a problem! Thanks a bunch!

Marking as solved.

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.