STL for loop ?

S

sd2004

Hi,
I do not know why I am getting extra output line.
If someone please help/explain.
Thanks in advance for your time,

///////////////// Source code /////////////
#include<iostream>
#include <string>
#include<vector>
#include<sstream>
#include<fstream>
using namespace std;

class astruct
{
public:
string name;
int id;
string type;
};

#define goldenretriever 11
#define Lab 33
#define Boxer 44
#define Terrier 55

void dog_name (const string& name,const string& type);
int main()
{
vector<astruct> v;
astruct astr;
ifstream in ("test5c.txt");
string line;
while (!getline(in,line).eof()){
istringstream anyname(line);
anyname>>astr.name>>astr.id>>astr.type;
v.push_back(astr);
}
vector<astruct>::iterator search;
for (search=v.begin();search!=v.end();++search){
switch (search->id){
case goldenretriever:
dog_name(search->name,search->type);
break;
case Lab:
dog_name(search->name,search->type);
break;
case Boxer:
dog_name(search->name,search->type);
break;
case Terrier:
dog_name(search->name,search->type);
break;
default:
break;
}
}
return 0;

}
void dog_name (const string& name,const string& type){
cout <<name <<" is " <<type<<endl;
}
///////////// input file "test5c.txt" //////////////////////
cricket 11 GoldenRetriever
nitro 11 GoldenRetriever
Maxtor 33 Lab
Arron 44 Boxer
Dora 55 Terrier

///////////// output from program ////////////////

cricket is GoldenRetriever
nitro is GoldenRetriever
Maxtor is Lab
Arron is Boxer
Dora is Terrier
Dora is Terrier
 
R

red floyd

sd2004 said:
Hi,
I do not know why I am getting extra output line.
If someone please help/explain.
Thanks in advance for your time,

///////////////// Source code /////////////
#include<iostream>
#include <string>
#include<vector>
#include<sstream>
#include<fstream>
using namespace std;

class astruct
{
public:
string name;
int id;
string type;
};

#define goldenretriever 11
#define Lab 33
#define Boxer 44
#define Terrier 55

void dog_name (const string& name,const string& type);
int main()
{
vector<astruct> v;
astruct astr;
ifstream in ("test5c.txt");
string line;
while (!getline(in,line).eof()){

You're very close.
while (getline(in,line)) {
 
L

Luke Meyers

I copied and pasted your code and compiled it with g++ 4.0.2, and saw
no extra output. What compiler are you using, and have you tried it on
any others?

Luke
 
?

=?iso-8859-1?Q?Ali_=C7ehreli?=

I do not know why I am getting extra output line.

I could reproduce your problem when there was an extra empty line at the end
of the file.

[...]
int main()
{
vector<astruct> v;
astruct astr;
ifstream in ("test5c.txt");
string line;
while (!getline(in,line).eof()){

As red floyd already said, this is idiomatic:

while (getline(in,line)) {
istringstream anyname(line);

This is not related to your problem, but please realize that you don't need
to use an istringstream here. You can directly use the ifstream to build
astr.
anyname>>astr.name>>astr.id>>astr.type;

There is your problem on the previous line. When the line above fails, you
are using an old state of astr on the next line without checking whether the
line above succeeded.
v.push_back(astr);

Replace the above with

if (anyname >> astr.name >> astr.id >> astr.type)
{
v.push_back(astr);
}

to solve your immediate problem.

Ali
 

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

passing argument in STL loop ? 3
STL ? 4
STL: copy vector of class ? 18
Another STL ? 3
STL ?? 2
vector of struct ? 2
STL algorithms and output iterators 1
Understanding how to design STL-compatible iterators 4

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top