Array size ?

S

sd2004

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++;
}

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
 
B

Bob Hairgrove

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 said:
///////////////////////////////////////////////////////////////////////////
#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
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.
 
M

Marcus Kwok

sd2004 said:
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.
 
D

deane_gavin

sd2004 said:
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++;
}

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
 
B

Bob Hairgrove

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.
 
M

Marcus Kwok

Bob Hairgrove said:
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 :)
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)
 
D

Dietmar Kuehl

Bob said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top