473,698 Members | 2,635 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1638
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.goo glegroups.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.goo glegroups.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.c om 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:\\stu ff.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:\\stu ff.txt");
type temp;

while (getline(infile , temp.item) && getline(infile, temp.menuP))
{
temp.price = strtod(temp.men uP.c_str(), 0);
myList.push_bac k(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
2233
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 some of these master complex and simple types. This I believe will assist me in transforming between the master schema and the other smaller schemas that contain a subset of the elements in the master schema. I understand that I can import an...
4
5140
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 types of formatting. For example, one file for page layout related items and another for text formattion? I have seen different combinations on different pages, and am wondering which people find is easier to maintain. Being from a coding...
11
4525
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 database,just help in getting me pointed in the right direction. I have a database with 8 tables, which from what I have read, cannot be linked on a single form, and be updatable. I have created a query which includes all 8 tables, and then...
32
14852
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 ((someString.IndexOf("something1",0) >= 0) || ((someString.IndexOf("something2",0) >= 0) ||
9
2769
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 wizards. But, I have found that trying to do something "outside the norm" adds a rather large level of complexity and/or data replication. Background I have been commissioned to create a web-based application for a client. It has a formsaunthentication...
3
6503
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 (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnservice/html/service07162002.asp) but I don't want clients to have to add two web references and then manually have to edit the proxy classes. After doing some searching I found that putting references to multiple web services in a .disco...
4
2653
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 interested to understand how to create a script to perform my purpose and since this function is useful, what the hey! I am rather new to batch files but would appreciate any help. TIA My process thus far has been the following: I edited the...
1
1326
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 my program to ignore the punctuations. my code is:: #include<stdio.h> int main(void) { int charac; int count=0;
1
2116
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 trying to adapt the same script to upload files to multiple websites specified in an array. This is for a content management system for our websites. I thought I could just stick a foreach loop round most of the script and that would work. However, no...
0
8683
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8610
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9170
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8873
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7740
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6528
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4372
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3052
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2339
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.