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

STL for loop ?

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

Jan 21 '06 #1
4 2683
sd2004 wrote:
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)) {

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

Jan 21 '06 #2
Already tried it.
Same result (still print extra output )

Jan 21 '06 #3
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

Jan 21 '06 #4
"sd2004" <tv****@hotmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
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

Jan 21 '06 #5

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

Similar topics

0
by: Charles Alexander | last post by:
Hello I am new to php & MySQL - I am trying to retrieve some records from a MySQL table and redisplay them. The data in list form looks like this: Sample_ID Marker_ID Variation ...
3
by: Anand Pillai | last post by:
This is for folks who are familiar with asynchronous event handling in Python using the asyncore module. If you have ever used the asyncore module, you will realize that it's event loop does not...
43
by: Gremlin | last post by:
If you are not familiar with the halting problem, I will not go into it in detail but it states that it is impossible to write a program that can tell if a loop is infinite or not. This is a...
5
by: Martin Schou | last post by:
Please ignore the extreme simplicity of the task :-) I'm new to C, which explains why I'm doing an exercise like this. In the following tripple nested loop: int digit1 = 1; int digit2 = 0;...
32
by: Toby Newman | last post by:
At the page: http://www.strath.ac.uk/IT/Docs/Ccourse/subsection3_8_3.html#SECTION0008300000000000000 or http://tinyurl.com/4ptzs the author warns: "The for loop is frequently used, usually...
2
by: Alex | last post by:
Compiler - Borland C++ 5.6.4 for Win32 Copyright (c) 1993, 2002 Borland Linker - Turbo Incremental Link 5.65 Copyright (c) 1997-2002 Borland Platform - Win32 (XP) Quite by accident I stumbled...
3
by: Ben R. | last post by:
In an article I was reading (http://www.ftponline.com/vsm/2005_06/magazine/columns/desktopdeveloper/), I read the following: "The ending condition of a VB.NET for loop is evaluated only once,...
32
by: cj | last post by:
When I'm inside a do while loop sometimes it's necessary to jump out of the loop using exit do. I'm also used to being able to jump back and begin the loop again. Not sure which language my...
16
by: Claudio Grondi | last post by:
Sometimes it is known in advance, that the time spent in a loop will be in order of minutes or even hours, so it makes sense to optimize each element in the loop to make it run faster. One of...
2
ADezii
by: ADezii | last post by:
If you are executing a code segment for a fixed number of iterations, always use a For...Next Loop instead of a Do...Loop, since it is significantly faster. Each pass through a Do...Loop that...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.