string array in c++


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 12 of 12

Thread: string array in c++

  1. #1
    Join Date
    Jul 2007
    Posts
    7

    string array in c++

    can somebody help me:(

    i don't know what's wrong with my program:
    this is my header file:
    // StudentInformation.h file

    #include <string> // uses string class
    using std::string;

    // StudentInformation class definition
    class StudentInformation
    {
    public:
    const static int students = 30; // constant number of student in the class
    studentInformation ( string, string, double ); // constructor that initialize the firstname, lastname and Finalscore.
    void setFirstName ( string ); // function that set the first name
    string getFirstName (); // function that get the first name
    void setLastName ( string ); // function that set the last name
    string getLastName (); // function that get the last name
    void setFinalScore ( double ); // function that set the final score
    double getFinalScore (); // function that get the final score
    void print (); // function that print the first name, last name and the final score
    void printGrades (); // function that print all the students first name, last name and the final grade
    double lowestScore (); // function that determine the lowest score
    double highestScore (); // function that determine the highest score
    double avgScore (); // function that determine the average score
    void add ( string, string, double );
    private:
    string firstName; // firstname for this student information class
    string lastName; // lastname for this student information class
    double finalScore; // final score for this student information class
    string fName[ students ]; // array of students' first name for the student information class
    string lName[ students ]; // array of students' last name for the student information class
    double fScore[ students ]; // array of student final score for the student information class
    }; // end class student information



    then this is my .cpp file:
    // StudentInformation II.cpp
    // Define class studentInformation that contains firstName, lastName, finalScore data
    // and member functions to set and get its value.
    #include <iostream> // allows program to perform input and output data
    // to the screen
    using namespace std;

    #include <string> // program uses C++ standard string class
    using namespace std;

    #include <iomanip>
    using namespace std;

    #include "StudentInformation II.h"

    // StudentInformation constructor initializes header, firstname and
    // lastname with string supplied as argument and the array finalScore.
    StudentInformation::StudentInformation( string f, string l, double s )
    {
    setFirstName( f ); // initialize firstName
    setLastName( l ); // initialize lastName
    setFinalScore(s);// initialize finalScore
    } // end of constructor

    void add( string f, string l, double s )
    {
    static double max = 0; // initialize first time add is called

    // if max is less than students, copy firstName, lastName and finalScore into their respective arrays.
    if ( max < students )
    {
    string fName[ max ] = firstName; // store firstName in array firstName
    string lName[ max ] = lastName; // store lastName in array lastName
    string fScore[ max ] = finalScore; // store finalScore in array finalScore
    max++; // increment max by 1
    }

    } // end of constructor

    // function that gets the first name
    string StudentInformation::getFirstName()
    {
    return firstName; // return object's firstName
    } // end function getFirstName

    // function that sets the first name
    void StudentInformation::setFirstName( string f )
    {
    firstName = f; // store the first name in the object
    } // end function setFirstName

    // function that gets the last name
    string StudentInformation::getLastName()
    {
    return lastName; // return object's lastName
    } // end function getLastName

    // function that sets the last name
    void StudentInformation::setLastName( string l )
    {
    lastName = l; // store the last name in the object
    } // end function setLastName

    // function that gets the finalScore
    double StudentInformation::getFinalScore()
    {
    return finalScore; // return object's finalScore
    } // end function getFinalScore

    //function that sets the finalScore
    void StudentInformation::setFinalScore( double s )
    {
    finalScore = s; // store the final score in the object
    } // end function setFinalScore

    // find minimum score
    double StudentInformation::lowestScore()
    {
    double lowScore = 100; // assume lowest grade is 100

    // loop through finalScore array
    for ( int student = 0; student < max; student++ )
    {
    // if current score lower than lowScore, assign it to lowScore
    if ( fScore[ student ] < lowScore )
    lowScore = fScore[ student ]; // new lowest score
    } // end the for loop

    return lowScore; // return lowest score
    } // end function lowestScore

    // find maximum score
    double StudentInformation::highestScore()
    {
    double highScore = 0; // assume highest score is 0


    // loop through finalScore array
    for ( int student = 0; student < max; student++ )
    {
    // if current score higher than highScore, assign it to highGrade
    if ( fScore[ student ] > highScore )
    highScore = fScore[ student ];
    } // end for

    return highScore; // return highest grade
    } // end function highestScore

    // find average score
    double StudentInformation::avgScore()
    {
    double total = 0; // initialize total

    // sum scores in array
    for ( int student = 0; student < max; student++ )
    total += fScore[ student ];

    // return average of scores
    return total / max;
    } // end function average score

    // function that print first name, last name and the final grade of all the students
    void StudentInformation::printGrades()
    {
    // output each student grade
    for ( int student = 0; student < max; student++ )
    {
    if ( fScore[ student ] >= 90 ) // if final score is more than 90, print Final Grade is A
    cout<< "\n" << fName[ student ] << setw(10) << lName[ student ] << setw(10) << "Final Grade is A";
    else if ( fScore[ student ] >= 80 ) // if final score is more than 80, print Final Grade is B
    cout<< "\n" << fName[ student ] << setw(10) << lName[ student ] << setw(10) << "Final Grade is B";
    else if ( fScore[ student ] >= 70 ) // if final score is more than 70, print Final Grade is C
    cout<< "\n" << fName[ student ] << setw(10) << lName[ student ] << setw(10) << "Final Grade is C";
    else if ( fScore[ student ] < 70 ) // if final score is less than 70, print Final Grade is F
    cout<< "\n" << fName[ student ] << setw(10) << lName[ student ] << setw(10) << "Final Grade is F";
    } // end for loop
    } // end function that print the grades of all the students

    // function that print first name, last name and finalScore
    void StudentInformation::print()
    {
    cout<< "\n\nFirst Name" << setw(10) << "Last Name" << setw(10) << "Final Score\n\n";

    // output each student's score
    for ( int student = 0; student < max; student++ )
    cout<< fName[ student ] << setw(10) << lName[ student ] << setw(10) << fScore[ student ] <<"\n";
    } // end function print


    now, this is my main() file:

    // function main of StudentInformation II.cpp
    #include <iostream>
    using namespace std;

    #include "StudentInformation II.h" // Include definition of class StudentInformation II

    // function main begins program execution
    int main()
    {
    // create one studentInformation object
    StudentInformation myStudentInformation1( "Emeric", "Bechi", 95);
    myStudentInformation1.print();
    StudentInformation myStudentInformation2( "Isaac", "Newton", 100 );
    myStudentInformation2.print();
    system ("pause");
    } // end main


    now, this is the errors that i get:
    5 C:\Dev-Cpp\StudentInformationIV.cpp In file included from C:\Dev-Cpp\StudentInformationIV.cpp

    11 C:\Dev-Cpp\StudentInformation II.h ISO C++ forbids declaration of `studentInformation' with no type

    C:\Dev-Cpp\StudentInformationIV.cpp In function `int main()':

    11 C:\Dev-Cpp\StudentInformationIV.cpp no matching function for call to `StudentInformation::StudentInformation(const char[7], const char[6], int)'

  2. #2
    Join Date
    Aug 2005
    Location
    Melbourne...Australia
    Posts
    279
    I just tried to run it in VS7 and you have many many errors.

    For starters your default constructor is missing a capital S in studentInformation!

    You are trying to use 'max' in numerous functions that don't know it exists..

  3. #3
    Join Date
    Jul 2007
    Posts
    7

    string array in c++

    i just realize that. and i fixed it by putting the max value in the header file as a public data. however it still gives me that same error which is:
    11 F:\Dev-Cpp\StudentInformation.h ISO C++ forbids declaration of `studentInformation' with no type .

    and i have no idea why it doesn't recognize my constructor.

  4. #4
    Join Date
    Aug 2005
    Location
    Melbourne...Australia
    Posts
    279
    Quote Originally Posted by emeric
    i just realize that. and i fixed it by putting the max value in the header file as a public data. however it still gives me that same error which is:
    11 F:\Dev-Cpp\StudentInformation.h ISO C++ forbids declaration of `studentInformation' with no type .

    and i have no idea why it doesn't recognize my constructor.
    studentInformation is not the same as StudentInformation

  5. #5
    Join Date
    Jul 2007
    Posts
    7
    i did that too. i meant, i fixed both of my mistakes, but i still get the same error message.

  6. #6
    Join Date
    Apr 2007
    Location
    Texas
    Posts
    151
    By which you mean: you get the same error at the same line of code, line 11 in this case?
    Last edited by Greggle; 07-15-2007 at 11:41 AM.

  7. #7
    Join Date
    Jul 2007
    Posts
    7
    now, this is the error that i get:
    26 F:\Dev-Cpp\StudentInformation.h ISO C++ forbids in-class initialization of non-const static member `max'

    i don't know why i can't initialize 'max' etheir in public or in private.

  8. #8
    Join Date
    Nov 2003
    Posts
    4,118
    You can't initialize data members inside the class. They must be initialized in the constructor. The only except is const static integral data.
    Danny Kalev

  9. #9
    Join Date
    Jul 2007
    Posts
    7

    Question declaration of data

    thank you for your help Everybody. however, my concern is that, i'm trying to use the 'max' value throughout the whole program. in that case, where should i declare it ? i declared it in the constructor but it gives me bunch of error message in the functions where i used the 'max' value.:confused:

  10. #10
    Join Date
    Dec 2003
    Posts
    3,366
    in the cpp file do this:

    static const double classname::value = 3.1415;


    in the class (header file), make a member:

    double value;

    also, beware 'max' as it is used as a macro in many systems.


    alternately, just put
    double value; in your class header file and in the ctor put
    value = 3.1415; to assign it (just remember that its a const and should never be on lhs).

  11. #11
    Join Date
    Jul 2007
    Posts
    7

    Question declaration of value

    thank you for your response. my problem is that i want to be able to increment the 'max' value. so in that case, i can't declare it as a const, can I?
    if not, how can i declare it and where in my cpp file and my header file?
    Last edited by emeric; 07-15-2007 at 10:26 PM.

  12. #12
    Join Date
    Dec 2003
    Posts
    3,366
    put in the class as a member, your call on public or private.

    -If it is shared across all your class instances, make it static as I described but without the const.

    -Initialize it in the constructor, or, if static, in the cpp file.

Similar Threads

  1. Submitting Infopath to a .net Web Service
    By athomas42 in forum .NET
    Replies: 1
    Last Post: 06-25-2007, 04:54 PM
  2. Replies: 0
    Last Post: 01-17-2006, 07:23 PM
  3. Input string was not in a correct format
    By mdengler in forum ASP.NET
    Replies: 0
    Last Post: 11-26-2002, 02:32 PM
  4. VB/C Array parameters
    By Gastao Woelfert in forum VB Classic
    Replies: 2
    Last Post: 09-01-2000, 11:36 AM
  5. How do I detect an FTP timeout?
    By Julian Milano in forum VB Classic
    Replies: 0
    Last Post: 08-10-2000, 09:16 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center