#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Location
    New York
    Posts
    8
    Rep Power
    0

    String arrays + functions


    I'm trying to pass a string array to a function which would populate it. Doesn't work the way I have it written below. I get a an error messages: 'string' : undeclared identifier.
    PHP Code:
    //Processor section
    #include <iostream>
    #include <string>
    const int SIZE 35;        //Max number of students in class
    int NumStudents;

    //Function prototypes
    void DisplayProgramInfo (void);
    void NumOfStudents ();
    void StudentData(string StudentName[], int StudentID[], double FinalGrade[]);
    void ExamWeights (double Exam1Percentdouble Exam2Percentdouble Exam3Percentdouble Exam4Percent);

    using namespace std;


    void main (void)
    {
        
    //Declare variables
                                        //Number of students in class
        
    int StudentID[SIZE];
        
    string StudentName[SIZE];
        
    double FinalGrade[SIZE];

        
    //Display general info about the program
        
    DisplayProgramInfo();

        
    //Get number of students in class
        
    NumOfStudents();
        
        
    //Get Student data
        
    StudentData(StudentNameStudentIDFinalGrade);

    }

    void DisplayProgramInfo (void)

    {
        
    //Display general info about the program


        
    cout <<"This program will help you calculate the final grade of each student,\n"
             
    <<"based on the average of 4 exam grades. It will also display the class average,\n"
             
    <<"and the max and min grades for the class.\n"
             
    <<"You can enter data for a maximum of 35 students.\n";
    }

    void NumOfStudents ()
    {
        
    //Get number of students in class 

        
    do {
        
    cout <<"\nHow many students are in the class?\t";
        
    cin >> NumStudents;

        if (
    NumStudents SIZE || NumStudents <= 0)
        
    cout <<"\nYou have entred an incorrect number, please try again";

        } while (
    NumStudents SIZE || NumStudents <= 0);
    }


    void StudentData(string StudentName[], int StudentID[], double FinalGrade[])
    {
        
    //Declare variables
        
    int i(0);
    //    string StudentName[SIZE];
        
    double exam1exam2exam3exam4;

        
    double Exam1Percent(0.0), Exam2Percent(0.0);        //Exam percentage weight 
        
    double Exam3Percent(0.0), Exam4Percent(0.0);        //Exam percentage weight

        
    cout << NumStudents;
        
    //Get exam weights
        
    ExamWeights (Exam1PercentExam2PercentExam3PercentExam4Percent);


        
    //Get data

        
    for (i=0NumStudentsi++)
        {
        
    cout <<"\n\nPlease enter the student name: ";
        
    cin >> StudentName[i];
        
    cout <<"Please enter the ID number for " << StudentName[i] <<":\t";
        
    cin >> StudentID[i];

        
    cout <<"\nPlease enter the grade for the 1st exam:\t";
        
    cin >> exam1;
        
        
    cout <<"Please enter the grade for the 2nd exam:\t";
        
    cin >> exam2;
        
        
    cout <<"Please enter the grade for the 3rd exam:\t";
        
    cin >> exam3;
        
        
    cout <<"Please enter the grade for the 4th exam:\t";
        
    cin >> exam4;
        
        
    FinalGrade[i] = ((exam1*Exam1Percent) + (exam2*Exam2Percent) + (exam3*Exam3Percent) + (exam4*Exam4Percent));
        }


  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,472
    Rep Power
    15
    In the future, please put a comment in your code on the line with the errror, so people trying to help you know where the error is.

    In your program, string is not recognized because only std::string exists as an identifier. I do this after the includes:

    using namespace std;

    Some people say that's bad form and you should only identify the functions you are going to use in the using statements like this:

    using std::cout;
    using std::cin;
    using std::string;

    but that's way too much typing for me.

IMN logo majestic logo threadwatch logo seochat tools logo