By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,433 Members | 1,702 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,433 IT Pros & Developers. It's quick & easy.

Help on struct ?

P: n/a
Please help, I thought the program save all data into struc
*ptrstudent.
however, when cout it only print the last piece of data.

#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
using namespace std;

struct Record_info {
string name;
double midterm;
double quiz;
double final;
}student[50],*ptrstudent;
int main (void){

double grade;

ifstream in ("test2.txt");
string line,word;
while (getline(in,line)){
ptrstudent = student;
if (line.find("#")!=string::npos)continue ;
istringstream anyname(line);

anyname>>ptrstudent->name>>student->midterm>>student->quiz>>student->final;

ptrstudent++;
}

for (ptrstudent=student; ptrstudent->name[0];ptrstudent++){
cout << ptrstudent->name<<endl;
cout << ptrstudent->midterm<<endl;
cout << ptrstudent->quiz<<endl;
cout << ptrstudent->final<<endl;
}

return 0;
}
/////////// input file "test2.txt" ///////////////////////
##########3
##########3
Tony 90.0 -15.2 98.2
##########3
Michael 95.0 17.2 92.2
/////////////////////////////////////////////

//////////////// output from program /////////////
Michael
95.0
17.2
92.2

////////////////////// Want the output as follow /////////////
Tonny
90.0
-15.2
98.2
Michael
95.0
17.2
92.2

Nov 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
tv****@hotmail.com wrote:
Please help, I thought the program save all data into struc
*ptrstudent.
however, when cout it only print the last piece of data.

#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
using namespace std;

struct Record_info {
string name;
double midterm;
double quiz;
double final;
}student[50],*ptrstudent;
int main (void){

double grade;

ifstream in ("test2.txt");
string line,word;
while (getline(in,line)){
ptrstudent = student;


Here is the error, you assign strudent to ptrstudent each time round the
loop. It doesn't matter than you are incrementing it later on because
you go and set it back to the beginning each time round the loop.

You probably meant only to assign it once before the loop, like this

ptrstudent = student;
while (getline(in,line)){

john
Nov 22 '05 #2

P: n/a
after the fix, the output still does not come out correct.

#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
using namespace std;

struct Record_info {
string name;
double midterm;
double quiz;
double final;
}student[50],*ptrstudent;
int main (void){

double grade;

ifstream in ("test2.txt");
string line,word;
ptrstudent = student;
while (getline(in,line)){
//ptrstudent = student;
if (line.find("#")!=string::npos)continue ;
istringstream anyname(line);

anyname>>ptrstudent->name>>student->midterm>>student->quiz>>student->final;

ptrstudent++;
}

for (ptrstudent=student; ptrstudent->name[0];ptrstudent++){
cout << ptrstudent->name<<endl;
cout << ptrstudent->midterm <<endl;
cout <<ptrstudent->quiz<< endl;
cout << ptrstudent->final<<endl;
}

return 0;
}
/////////// input file "test2.txt" ///////////////////////
##########3
##########3
Tony 90.0 -15.2 98.2
##########3
Michael 95.0 17.2 92.2
/////////////////////////////////////////////

//////////////// output from program /////////////
Tony
95
17.2
92.2
Michael
0
0
0
////////////////////// Want the output as follow /////////////
Tonny
90.0
-15.2
98.2
Michael
95.0
17.2
92.2

Nov 22 '05 #3

P: n/a
never mind. I was a typo on my part.
Thanks

Nov 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.