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

Reading a string from an infile that's part of a struct..

Does anyone know the proper syntax to properly, read in data from a txt file..and have it's contents be put into string variables, that are part of a struct array? I keep getting errors on the read ins, regarding >> operatios. here is my code:

#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cmath>
#include <iomanip>
using namespace std;
int last;

class List
{

struct phoneT
{
string lname[15];
string fname[10];
string mi;
string phone [15];
string room [3];
string mm [2];
string yy [2];
string year [4];
};
public:
static phoneT dir[100];
void readin();
void showlist();
void fnamesearch(string);
void lnamesearch(string);
};


void List::readin()
{

ifstream infile;
infile.open("dir.txt");
int x=0;
while (!infile.eof())
{

infile>>dir[x].lname>>dir[x].fname>>dir[x].mi>>dir[x].phone>>dir[x].room>>dir[x].mm>>dir[x].yy>>dir[x].year;
x++;
last++;
}
}
void List::showlist()
{
for (int i=0; i<last; i++)
{
cout<<dir[i].lname<<" "<<dir[i].fname<<" "<<dir[i].mi<<" "<<dir[i].phone<<" "<<" "<<dir[i].room<<" "<<dir[i].mm<<" "<<dir[i].yy<<" "<<dir[i].year<<" ";
}
}
Nov 13 '07 #1
2 1292
gpraghuram
1,275 Expert 1GB
Does anyone know the proper syntax to properly, read in data from a txt file..and have it's contents be put into string variables, that are part of a struct array? I keep getting errors on the read ins, regarding >> operatios. here is my code:

#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cmath>
#include <iomanip>
using namespace std;
int last;

class List
{

struct phoneT
{
string lname[15];
string fname[10];
string mi;
string phone [15];
string room [3];
string mm [2];
string yy [2];
string year [4];
};
public:
static phoneT dir[100];
void readin();
void showlist();
void fnamesearch(string);
void lnamesearch(string);
};


void List::readin()
{

ifstream infile;
infile.open("dir.txt");
int x=0;
while (!infile.eof())
{

infile>>dir[x].lname>>dir[x].fname>>dir[x].mi>>dir[x].phone>>dir[x].room>>dir[x].mm>>dir[x].yy>>dir[x].year;
x++;
last++;
}
}
void List::showlist()
{
for (int i=0; i<last; i++)
{
cout<<dir[i].lname<<" "<<dir[i].fname<<" "<<dir[i].mi<<" "<<dir[i].phone<<" "<<" "<<dir[i].room<<" "<<dir[i].mm<<" "<<dir[i].yy<<" "<<dir[i].year<<" ";
}
}
If u are referrring to the static variable inside the class then u shuld use classname::variable name
like List::dir

Raghuram
Nov 13 '07 #2
weaknessforcats
9,208 Expert Mod 8TB
What ir dir[x]?? Some kind of array??

Thses are member functions so you can:
Expand|Select|Wrap|Line Numbers
  1. infile>>lname>>fname>>mi>>phone>>room>>mm...etc
  2.  
That would read one object from the input file stream.

I this object is part of an array, then do that in main(). The memebr function is only concerned with the this object.
Nov 13 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: muser | last post by:
With the following code I'm trying to read a text file (infile) and output inaccuracies to the error file (printerfile). The text file is written and stored on disk, while the printerfile has to be...
1
by: muser | last post by:
The following program reads two lines of a file in my A drive and reads no more. Can some sarmartian tell me why that is. Please copy and paste the program into your own compiler please. ...
2
by: Brian Ward | last post by:
First: sorry as a relative newbie for previously not including code. My question: Reading C++ books I almost always find programs such as the one below give the following type of code for reading...
11
by: mkarja | last post by:
Hi, I'm trying to figure out how to read some range of rows from a file. Is it possible to search the file with some criteria and then when the search string is found read 3 rows before and...
12
by: Felix85 | last post by:
here is my method for reading in a file: static room room::file2Room(int rnum){ ostringstream filename; filename << "../gamefiles/rooms/" << rnum << ".room"; ifstream...
4
by: Fazana | last post by:
I need help for reading a text file into in my java program. The file contains this information: ID# Student’s Answers --------------------------- 236499 TFTFTFTFFFFTFTFFFTF 643828...
2
by: spidey12345 | last post by:
what i need this program to do is to read paragraphs like "st blah blh test ere se sit blha eere w" and then it will reformat to "st blah...
0
by: jrjan1 | last post by:
I am confused about looping through an infile for my homework. in my header file I have: class Item { friend istream& operator>>(istream&, Item&); public: void print() const; Item(string...
2
by: GeoUK | last post by:
Hi All, New member here with a bit of problem. I have read the FAQ's and searched text books but still cannot solve the problem that I have. As part of a course I am doing at University I had...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
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...
0
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...

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.