Help on struct ?

T

tvn007

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
 
J

John Harrison

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
 
T

tvn007

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
 

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

Similar Threads

struct ? 5
CLASS ? 2
Help on istream& operator ? 2
Help, read input from file ? 3
read data from file into structure ? 2
Array size ? 6
I need help 1
CIN Input #2 gets skipped, I don't understand why. 1

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
SterlingLa
Top