473,408 Members | 1,861 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,408 software developers and data experts.

Help on istream& operator ?

Please help why this code not compile
I would like it to be something like this:
istringstream stringstreams(line);
stringstreams>>ptrstudent->bypassed>>ptrstudent->name>>ptrstudent->midterm;
stringstreams>>ptrstudent->quiz>>ptrstudent->final>>ptrstudent->testname;

///////////////////////////////////////////////////////////////////////////////////////////////////////
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
using namespace std;
const int MAX=50;
struct Record_info {
string bypassed;
string name;
int midterm;
double quiz;
double final;
string testname;
}student[MAX],*ptrstudent;

int main (void){
ptrstudent = student;
ifstream in ("test2.txt");
string line;
string line2;
char buffer[40];
while (getline(in,line)){
if (line.find("#")!=string::npos)continue ;
istream& operator >>(istream & is,prtstudent & r){
is>>r.bypassed>>r.name>>r.midterm,r.quiz,r.final,r .testname;}
ptrstudent++;
}
return 0;
}

Dec 1 '05 #1
2 2452
tvn007 wrote:
Please help why this code not compile
Well, the compiler told you, in the error message. Please post it,
there are good reasons why the FAQ says you must do that.
///////////////////////////////////////////////////////////////////////////////////////////////////////
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
using namespace std;
const int MAX=50;
struct Record_info {
string bypassed;
string name;
int midterm;
double quiz;
double final;
string testname;
}student[MAX],*ptrstudent;

int main (void){
ptrstudent = student;
ifstream in ("test2.txt");
string line;
string line2;
char buffer[40];
while (getline(in,line)){
if (line.find("#")!=string::npos)continue ;
istream& operator >>(istream & is,prtstudent & r){
is>>r.bypassed>>r.name>>r.midterm,r.quiz,r.final,r .testname;}
ptrstudent++;
}
return 0;
}


Functions should be defined and used separately. It looks like you
don't
understand the difference, and you tried to put something that's
neither
a definition nor a call of operator>> inside main.

Try a simpler program first, defining and calling a function void
print_hello_world( )

HTH,
Michiel Salters

Dec 1 '05 #2

"tvn007" <tv****@hotmail.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
| Please help why this code not compile
| I would like it to be something like this:
| istringstream stringstreams(line);
|
stringstreams>>ptrstudent->bypassed>>ptrstudent->name>>ptrstudent->midte
rm;
|
stringstreams>>ptrstudent->quiz>>ptrstudent->final>>ptrstudent->testname
;
|
|
////////////////////////////////////////////////////////////////////////
///////////////////////////////
| #include <iostream>
| #include <string>
| #include <fstream>
| #include <sstream>
| using namespace std;
| const int MAX=50;
| struct Record_info {
| string bypassed;
| string name;
| int midterm;
| double quiz;
| double final;
| string testname;
| }student[MAX],*ptrstudent;
|
| int main (void){
| ptrstudent = student;
| ifstream in ("test2.txt");
| string line;
| string line2;
| char buffer[40];
| while (getline(in,line)){
| if (line.find("#")!=string::npos)continue ;
| istream& operator >>(istream & is,prtstudent & r){
| is>>r.bypassed>>r.name>>r.midterm,r.quiz,r.final,r .testname;}
| ptrstudent++;
| }
| return 0;
| }
|

Start by getting a simplified skeleton to work first. Appropriate
formatting goes a long way to help you identify your individual
declarations.

// test2.txt
name_1
name_2
name_3

So your test.cpp file should look like this:

#include <iostream>
#include <ostream>
#include <string>
#include <fstream>

// Record type
struct Record_info
{
void set(std::string& s) { name = s; }
private:
std::string name;
/* Friend op>> */
friend std::ostream&
operator<<(std::ostream&, const Record_info&);
};

// global op<<
std::ostream& operator<<(std::ostream& os, const Record_info& r)
{
os << "name: " << r.name << "\n";
return os;
}

int main()
{
Record_info students[10];

// open file
std::ifstream in("test2.txt");
if( !in )
{
std::cout << "error while opening file.\n";
return 0;
}

// parse file
int count = 0;
std::string s;
while ( std::getline(in, s) )
{
students[count++].set(s);
}
if( !in.eof() ) // paranoia check, was the error *not* an eof
marker?
{
std::cout << "error while parsing file.\n";
return 0;
}

// display contents
std::cout << "number of students: " << count << "\n";
for (int i = 0; i < count; ++i)
{
std::cout << students[i];
}

return 0;
} // main()

/*
number of students: 3
name: name_1
name: name_2
name: name_3

*/

Dec 2 '05 #3

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

Similar topics

2
by: Timothy Stark | last post by:
Hello folks, I am trying get operator + working. I developed a class for a pair (left and right values). class Word10 { public: uint18 left, right; :
2
by: Henry | last post by:
Hello I was checking and testing this site and the menu on the left look distorted. This was on a older machine using IE6 win 2000 or older. This is how it looked...
6
by: titan0111 | last post by:
----inside of class called snowfall, i wrote---- int operator == (char); ----and right before main() i wrote---- int snowfall::operator == (char str) // comparing two member data...
1
by: PMB | last post by:
Thank you in advance for any and all assistance. I'm trying to use a make table query to pull the last transactionID, so I can use an append query to reset the transactionID to the next...
0
by: John | last post by:
Hello, How can i go to my helpfile with a button (not F1) in the menu. Please help me. John
3
by: Woody Splawn | last post by:
For reasons I have not yet identified my Help, Search and Help, Index has quit working. That is, in the VB IDE, from the Help menu, I can select Dynamic help or Contents and things work as...
1
by: =?Utf-8?B?U2FzaGE=?= | last post by:
If Help & support centre will not open, how can it be found and used?
14
by: W2K3R2admin | last post by:
I cannot open help and support...how do I install it on an existing system? When I click on Help & Support, the error says "Windows cannot open Help and Support because a system service is not...
7
by: randysimes | last post by:
I have to overload this operator to cin a character array to eventually put the strings in lexicographical order. The is unknown and therefore will change accordingly. I do not know how to do...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.