Array size ?

Discussion in 'C++' started by sd2004, Dec 5, 2005.

  1. sd2004

    sd2004 Guest

    Hi,
    I am new to C++.
    I am reading data from file into array.
    The problem is , I do not know ahead how many lines the file will be.
    I am arbitrary assign array size with "int MAX" which is not good way
    of doing it, I think.
    could someone please sugguest/help me with better way.
    Thanks
    ///////////////////////////////////////////////////////////////////////////
    #include <iostream>
    #include <string>
    #include <fstream>
    #include <sstream>
    using namespace std;

    int MAX=10000;
    typedef double Number;
    class Student_Rec {
    public:
    string name;
    int midterm;
    Number quiz;
    Number final;
    string testname;
    void print_data();
    };
    void Student_Rec:: print_data(){
    cout <<"Student : "<<name<< " "<<midterm<<" "<<testname<<endl;
    }

    int main (void){
    Student_Rec student[MAX],*student_ptr;
    student_ptr = student;
    ifstream in ("test4.txt");
    string line;
    while (getline(in,line)){
    istringstream streams(line);
    streams>>student_ptr->name>>student_ptr->midterm

    >>student_ptr->quiz>>student_ptr->final>>student_ptr->testname;

    student_ptr++;
    }

    for (student_ptr=student; student_ptr->midterm;student_ptr++){
    student_ptr->print_data();
    }

    return 0;
    }

    //////// input file "test4.txt" //////////////////////
    Tony 90 -15.2 98.2 math
    Michael 95 17.2 92.2 physics
    Amy 82 13.9 78.2 accounting
    sd2004, Dec 5, 2005
    #1
    1. Advertising

  2. On 5 Dec 2005 12:04:36 -0800, "sd2004" <> wrote:

    >Hi,
    >I am new to C++.


    OK. For a beginner, you asked a VERY good question!

    >I am reading data from file into array.
    >The problem is , I do not know ahead how many lines the file will be.
    >I am arbitrary assign array size with "int MAX" which is not good way
    >of doing it, I think.
    >could someone please sugguest/help me with better way.
    >Thanks


    Use std::vector<> instead of an array.

    >///////////////////////////////////////////////////////////////////////////
    >#include <iostream>
    >#include <string>
    >#include <fstream>
    >#include <sstream>
    >using namespace std;
    >
    >int MAX=10000;


    Actually, this should be:

    const int MAX = 10000;
    ^^^^^

    Otherwise, the array declaration inside of main() won't compile
    (unless your compiler is broken...)

    >typedef double Number;
    >class Student_Rec {
    > public:
    > string name;
    > int midterm;
    > Number quiz;
    > Number final;
    > string testname;


    Why are all of your data members public? That is one of C++ strong
    points: you can "hide" data!

    > void print_data();
    > };
    > void Student_Rec:: print_data(){
    > cout <<"Student : "<<name<< " "<<midterm<<" "<<testname<<endl;
    > }
    >
    >int main (void){


    int main (){

    > Student_Rec student[MAX],*student_ptr;

    ^^^
    won't copmile unless MAX is const.

    > student_ptr = student;


    You don't need student_ptr ... just use student wherever student_ptr
    is needed. C++ automatically converts the array name into a pointer.

    > ifstream in ("test4.txt");
    > string line;
    > while (getline(in,line)){
    > istringstream streams(line);
    > streams>>student_ptr->name>>student_ptr->midterm
    > >>student_ptr->quiz>>student_ptr->final>>student_ptr->testname;

    A little whitespace would serve you well here!

    > student_ptr++;


    Aha: you need the pointer just for this?

    > }
    >
    >for (student_ptr=student; student_ptr->midterm; student_ptr++){
    > student_ptr->print_data();


    What is this supposed to do? If "student_ptr->midterm" is ever == 0,
    the loop will terminate prematurely.

    > }
    >
    >return 0;
    >}
    >
    >//////// input file "test4.txt" //////////////////////
    > Tony 90 -15.2 98.2 math
    > Michael 95 17.2 92.2 physics
    > Amy 82 13.9 78.2 accounting


    --
    Bob Hairgrove
    Bob Hairgrove, Dec 5, 2005
    #2
    1. Advertising

  3. sd2004

    Marcus Kwok Guest

    sd2004 <> wrote:
    > Hi,
    > I am new to C++.
    > I am reading data from file into array.
    > The problem is , I do not know ahead how many lines the file will be.
    > I am arbitrary assign array size with "int MAX" which is not good way
    > of doing it, I think.
    > could someone please sugguest/help me with better way.
    > Thanks


    Instead of arrays, prefer the use of std::vector (you need to #include
    <vector>), and to add elements to the vector, use the push_back()
    function. std::vector<> is designed to be a replacement for arrays, and
    they handle their own memory, and can grow as needed.

    --
    Marcus Kwok
    Marcus Kwok, Dec 5, 2005
    #3
  4. sd2004

    Guest

    sd2004 wrote:
    > Hi,
    > I am new to C++.
    > I am reading data from file into array.
    > The problem is , I do not know ahead how many lines the file will be.
    > I am arbitrary assign array size with "int MAX" which is not good way
    > of doing it, I think.
    > could someone please sugguest/help me with better way.
    > Thanks
    > ///////////////////////////////////////////////////////////////////////////
    > #include <iostream>
    > #include <string>
    > #include <fstream>
    > #include <sstream>
    > using namespace std;
    >
    > int MAX=10000;
    > typedef double Number;
    > class Student_Rec {
    > public:
    > string name;
    > int midterm;
    > Number quiz;
    > Number final;
    > string testname;
    > void print_data();
    > };
    > void Student_Rec:: print_data(){
    > cout <<"Student : "<<name<< " "<<midterm<<" "<<testname<<endl;
    > }
    >
    > int main (void){
    > Student_Rec student[MAX],*student_ptr;
    > student_ptr = student;
    > ifstream in ("test4.txt");
    > string line;
    > while (getline(in,line)){
    > istringstream streams(line);
    > streams>>student_ptr->name>>student_ptr->midterm
    >
    > >>student_ptr->quiz>>student_ptr->final>>student_ptr->testname;

    > student_ptr++;
    > }
    >
    > for (student_ptr=student; student_ptr->midterm;student_ptr++){
    > student_ptr->print_data();
    > }
    >
    > return 0;
    > }
    >
    > //////// input file "test4.txt" //////////////////////
    > Tony 90 -15.2 98.2 math
    > Michael 95 17.2 92.2 physics
    > Amy 82 13.9 78.2 accounting


    You're using strings and streams from the standard library, which is
    good for someone new to C++. Unfortunately, whoever is teaching you
    seems to have shown you arrays before they showed you std::vector. Look
    it up. It will end your problems here.

    Gavin Deane
    , Dec 5, 2005
    #4
  5. On Mon, 5 Dec 2005 20:25:21 +0000 (UTC), (Marcus
    Kwok) wrote:

    >std::vector<> is designed to be a replacement for arrays,


    True, but not necessarily ... only in cases where the bounds can only
    be determined at runtime. If the bounds can be determined at
    compile-time, and do not change at runtime, there is no reason not to
    use an ordinary array;

    >and they handle their own memory,


    False -- an array must have its dimension(s) known at compile-time, so
    there is no memory issue here. It is only an issue when an array needs
    to be resized dynamically.

    >and can grow as needed.


    True.

    --
    Bob Hairgrove
    Bob Hairgrove, Dec 5, 2005
    #5
  6. sd2004

    Marcus Kwok Guest

    Bob Hairgrove <> wrote:
    > On Mon, 5 Dec 2005 20:25:21 +0000 (UTC), (Marcus
    > Kwok) wrote:
    >
    >>std::vector<> is designed to be a replacement for arrays,

    >
    > True, but not necessarily ... only in cases where the bounds can only
    > be determined at runtime. If the bounds can be determined at
    > compile-time, and do not change at runtime, there is no reason not to
    > use an ordinary array;


    Yes, I said it was "a" replacement for arrays, not necessarily "the"
    replacement for arrays :)

    >>and they handle their own memory,

    >
    > False -- an array must have its dimension(s) known at compile-time, so
    > there is no memory issue here. It is only an issue when an array needs
    > to be resized dynamically.


    OK, but the OP was talking about needing a structure where the number of
    elements is not known until runtime, so my comment was meant to be taken
    in that context. (I am disregarding the OP's original solution of
    declaring a large array of size MAX)

    >>and can grow as needed.

    >
    > True.


    --
    Marcus kwok
    Marcus Kwok, Dec 5, 2005
    #6
  7. Bob Hairgrove wrote:
    > Actually, this should be:
    >
    > const int MAX = 10000;
    > ^^^^^


    Being pedantic about my issue with the const placement, this
    should actually be:

    int const MAX = 10000;
    ^^^^^

    This provides for a consistent placement of 'const' always to the
    right of the entity made constant.
    --
    <mailto:> <http://www.dietmar-kuehl.de/>
    <http://www.eai-systems.com> - Efficient Artificial Intelligence
    Dietmar Kuehl, Dec 5, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Zygmunt Krynicki
    Replies:
    1
    Views:
    611
    Ivan Vecerina
    Oct 11, 2003
  2. Denis Palas
    Replies:
    2
    Views:
    662
    Daniel Pitts
    Dec 19, 2006
  3. Replies:
    5
    Views:
    546
    Flash Gordon
    Apr 9, 2006
  4. Replies:
    8
    Views:
    502
    Bob Hairgrove
    Apr 10, 2006
  5. Jason Cavett

    Preferred Size, Minimum Size, Size

    Jason Cavett, May 23, 2008, in forum: Java
    Replies:
    5
    Views:
    12,564
    Michael Jung
    May 25, 2008
Loading...

Share This Page