473,388 Members | 1,391 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

Help on struct ?

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
3 1393
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
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
never mind. I was a typo on my part.
Thanks

Nov 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: cpptutor2000 | last post by:
I am trying to create a simple linked list. The source code is provided below. I receive the following error message: (Please see after source code). I have marked the lines where the error occurs...
2
by: Foodbank | last post by:
Hi everyone, I'm having trouble implementing a supposedly simple Least Significant Digit first Radix Sort. The book I'm using gave me somewhat of a template that I touched up (below), but I'm...
10
by: Angel | last post by:
I'm using several C functions (in a dll) that receive a struct as parameter. Since I'm doing it in C#, I assume I need to recreate the struct in C# in order to call the function with the required...
6
by: TC | last post by:
Hi. I write a program in c language that read a text file and extrapolate the word. for all word the program calculate the number of the times that word is present in the text. The problem is the...
2
by: duncanblacksmithmath | last post by:
I know a lot of you have seen this before but I have worked on the program and have gotten it to work thus far but I need help getting these two functions to work and implementing them. Here is...
9
by: Danny Mavromatis | last post by:
I have a chunk of VC.NET code (below) that I need to convert to VB.NET syntax. Could someone help me get started? I'm new to structures and unions and I don't understand how to nest then in...
4
by: Steve Chow | last post by:
I was wondering if someone could help with my assignment. I've pretty done finished the work but cannot figure out why it only prints the last one added. Here is my code. The way things are done...
66
by: genestarwing | last post by:
QUESTION: Write a program that opens and read a text file and records how many times each word occurs in the file. Use a binary search tree modified to store both a word and the number of times it...
83
by: deppy_3 | last post by:
Hi.I am started learning Programm language C before some time.I am trying to make a programm about a very simple "sell shop".This programm hasn't got any compile problem but when i run it i face...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.