Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Array size ?

Reply
Thread Tools

Array size ?

 
 
sd2004
Guest
Posts: n/a
 
      12-05-2005
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

 
Reply With Quote
 
 
 
 
Bob Hairgrove
Guest
Posts: n/a
 
      12-05-2005
On 5 Dec 2005 12:04:36 -0800, "sd2004" <(E-Mail Removed)> 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
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
 
Marcus Kwok
Guest
Posts: n/a
 
      12-05-2005
sd2004 <(E-Mail Removed)> 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
 
Reply With Quote
 
deane_gavin@hotmail.com
Guest
Posts: n/a
 
      12-05-2005

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

 
Reply With Quote
 
Bob Hairgrove
Guest
Posts: n/a
 
      12-05-2005
On Mon, 5 Dec 2005 20:25:21 +0000 (UTC), (E-Mail Removed) (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
(E-Mail Removed)
 
Reply With Quote
 
Marcus Kwok
Guest
Posts: n/a
 
      12-05-2005
Bob Hairgrove <(E-Mail Removed)> wrote:
> On Mon, 5 Dec 2005 20:25:21 +0000 (UTC), (E-Mail Removed) (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
 
Reply With Quote
 
Dietmar Kuehl
Guest
Posts: n/a
 
      12-05-2005
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.
--
<(E-Mail Removed)> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Preferred Size, Minimum Size, Size Jason Cavett Java 5 05-25-2008 08:32 AM
mega pixels, file size, image size, and print size - Adobe Evangelists Frank ess Digital Photography 0 11-14-2006 05:08 PM
Could a struct with size 44 bytes point always points to a char array with size 2024 bytes? eagle_jyjh@citiz.net C++ 8 04-10-2006 03:05 PM
Could a struct with size 44 bytes point always points to a char array with size 2048 bytes? eagle_jyjh@citiz.net C Programming 5 04-09-2006 02:49 PM
On template method to obtain the size of an array. Was: [OT] Re: 2 stupid array questions!! Zygmunt Krynicki C++ 1 10-11-2003 01:18 AM



Advertisments