problrm with multiple types in text file

T

truedos

hello,
I have a text file that looks like this:
text
numbers
text
numbers
text
numbers
..
..
..

I figure out how to get the "numbers" line into the double variable I
have created. I have erased all attempts, because they didn't work. I
hope I have made enough sense of what I am trying to do.

Here is my code:

#include <iostream>
#include <fstream>

using namespace std;
struct type
{
string item;
double price;
};
int main()
{
type list[16];

int i;
ifstream infile;
infile.open( "c:\\stuff.txt" );

while( !infile.eof() )
{
for( int i = 0; i < 16; i++ )
{
getline( infile, list.item );
}
}
return 0;
}
 
T

truedos

I apologize, that was the worst description of a problem I have ever
seen. I thought I read through it enough, I guess not. The problem I
am having is not being able to add the variable "price" to the array.
The list.item works fine, but I can't figure out how to add
list.price along with it.
 
M

Mike Wahler

I apologize, that was the worst description of a problem I have ever
seen. I thought I read through it enough, I guess not. The problem I
am having is not being able to add the variable "price" to the array.
The list.item works fine, but I can't figure out how to add
list.price along with it.




infile >> list.price;

Read about 'stream extraction operators'.

Alternatively, read the digits into a string using
'getline()', and use a conversion function such as
'strtod()' to convert the string to a numeric type.

Also see the C++ FAQ about how to use 'ifstream::eof()'
correctly, which you're not doing.

-Mike
 
T

truedos

Thanks for your response, I will go and do some reading.

The way i wrote it is the way i was taught to do it, but it wouldn't
suprise me if it wasn't the correct way. Does it just depend on which
compiler you use? I am using the Borland c++ builder 5.
 
H

Howard

Thanks for your response, I will go and do some reading.

The way i wrote it is the way i was taught to do it, but it wouldn't
suprise me if it wasn't the correct way. Does it just depend on which
compiler you use? I am using the Borland c++ builder 5.

I assume you're talking about your use of ifstream::eof()?

Many schools teach the general algorithm "while not end of file...", which
is fine. But if they're teaching using the C++ file stream eof() function
that way, they're teaching it wrong.

The eof() function cannot know if the end of file is reached until *after*
it has failed to read. The function simply tells you whether that failure
was due to the end of the file being reached. Otherwise, some error must
have occurred. This is why the read functions return a value indicating
whether the read succeeded or not. So your loop should really end when
getline fails. Once it fails, you can check eof() to see if it's a "normal"
failure (that is, the end of the file), and report an error otherwise.

-Howard
 
T

truedos

Yeah, I was just using !infile.eof() as while not the end of file. I
finally got it working, I set it up as a string and then converted it
to a double on the next line.

Here is what works if any of you care:

#include <iostream>
#include <fstream>
using namespace std;
struct type
{
string item;
string menuP;
double price;
};
int main()
{
type list[8];
int i;
ifstream infile;
infile.open( "c:\\stuff.txt" );
while( !infile.eof() )
{
for( i = 0; i < 8; i++ )
{
getline( infile, list.item );
getline( infile, list.menuP );
list.price = strtod( list.menuP.c_str(), NULL );
}
}
return 0;
}
 
K

Kristo

Yeah, I was just using !infile.eof() as while not the end of file. I
finally got it working, I set it up as a string and then converted it
to a double on the next line.

Here is what works if any of you care:

This might "work" for you but it's definitely not standard. First of
all, you haven't heeded others' advice (which you snipped) about the
correct use of ifstream::eof. I'll make the rest of my comments inline
with your code.
#include <iostream>

You don't need this. The version of getline you're using is in
#include <fstream>

You forgot to #include <string>. <iostream> or <fstream> might have
included it for you, but you must not rely on that. You've also
forgotten said:
using namespace std;
struct type
{
string item;
string menuP;
double price;
};
int main()
{
type list[8];
int i;
ifstream infile;
infile.open( "c:\\stuff.txt" );

You can condense this down to one line:
ifstream infile("c:\\stuff.txt");
while( !infile.eof() )

As others have said, you can't meaningfully test for EOF until you've
actually read something.
{
for( i = 0; i < 8; i++ )
{
getline( infile, list.item );
getline( infile, list.menuP );


Remember that getline can fail. ALWAYS check the return value.
list.price = strtod( list.menuP.c_str(), NULL );
}
}


Don't forget to close the file when you're done with it.
return 0;
}

So, your code "works" as long as the file you're reading has a positive
multiple of 16 lines. Here's an improved version of the same code
(n.b. this is untested):

#include <cstdlib>
#include <fstream>
#include <string>
#include <vector>
using namespace std;

struct type
{
string item;
string menuP;
double price;
};

int main()
{
vector<type> myList;
ifstream infile("c:\\stuff.txt");
type temp;

while (getline(infile, temp.item) && getline(infile, temp.menuP))
{
temp.price = strtod(temp.menuP.c_str(), 0);
myList.push_back(temp);
}

infile.close();
return 0;
}

The && operator will short circuit if the first call to getline fails.
Further, I believe the very first call to getline will also fail if the
file failed to open. Somebody correct me if I'm wrong on that.

Hope this helps.

Kristo
 
T

truedos

Thanks for the advice Kristo. I am just doing it the way it was taught
to me in my entry level c++ class. I realize they didn't teach me the
correct way to do a lot of things, and that they are hold a lot of
stuff back, but I have to work with what I know. The only thing I know
about iostream::eof, is what is written in my previous posts. I never
complained becuase I always came out with the results I was looking
for.

I appreciate the time you took to post and give me some sound advice.
I'll do better next time.
 

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

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,276
Latest member
Sawatmakal

Latest Threads

Top