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

problrm with multiple types in text file

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[i].item );
}
}
return 0;
}

Jul 23 '05 #1
7 1628
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[i].item works fine, but I can't figure out how to add
list[i].price along with it.

Jul 23 '05 #2

<tr*****@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
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[i].item works fine, but I can't figure out how to add
list[i].price along with it.


infile >> list[i].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
Jul 23 '05 #3
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.

Jul 23 '05 #4

<tr*****@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
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


Jul 23 '05 #5
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[i].item );
getline( infile, list[i].menuP );
list[i].price = strtod( list[i].menuP.c_str(), NULL );
}
}
return 0;
}

Jul 23 '05 #6
true...@gmail.com wrote:
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
<string>.
#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 <cstdlib> which defines strtod.
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[i].item );
getline( infile, list[i].menuP );
Remember that getline can fail. ALWAYS check the return value.
list[i].price = strtod( list[i].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

Jul 23 '05 #7
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.

Jul 23 '05 #8

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

Similar topics

1
by: Steve George | last post by:
Hi, I have a scenario where I have a master schema that defines a number of complex and simple types. I then have a number of other schemas (with different namespaces) where I would like to reuse...
4
by: Carolyn Marenger | last post by:
Hey everyone, I am looking for your thoughts and opinions. Is it preferable to have one css file containing all the style information or break it up into multiple imported files for different...
11
by: dskillingstad | last post by:
I've been struggling with this problem for some time and have tried multiple solutions with no luck. Let me start with, I'm a novice at Access and I'm not looking for someones help to design my...
32
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if...
9
by: Graham | last post by:
I have been having some fun learning and using the new Controls and methods in .Net 2.0 which will make my life in the future easier and faster. Specifically the new databinding practises and...
3
by: Matt D | last post by:
I've got two web services that use the same data types and that clients will have to consume. I read the msdn article on sharing types...
4
by: | last post by:
Hello, I am attempting to convert multiple .wav files to the .flac format via a batch script. I know that this can be done with numerous software implementations automatically for me; however, i am...
1
by: Alenik1989 | last post by:
I am writing a very simple program which suppose to get a text froma file and then by ignoring all the spaces and punctuation, copy them to another file , 1 word per line. my problem is i cant get...
1
by: fortwilliam | last post by:
Hi, I am very new to "object oriented programming". I have this script which I didn't write but have altered and have been using for a while to allow people to upload files to a website. Now I am...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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...
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...

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.