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