Trouble with inputing a data file into a class
Mar 9, 2013 at 1:34am UTC
I'm having a hard time doing this. I am supposed to be getting data from a data file and inputting it into a class named Person. I haven't been able to get this to work. I don't know what I'm doing wrong. Please help. Any help is appreciated.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
#include<iostream>
#include<fstream>
#include<string>
#include<iomanip>
using namespace std;
class Person
{
private :
string fname, lname, job;
char gender;
double income;
int age;
public :
void setFname(string F){fname=F;}
void setLname(string L){lname=L;}
void setGender(char G){gender=G;}
void setAge(int A){age=A;}
void setIncome(double I){income=I;}//********************************************************************************************
string getFname(){return fname;}
string getLname(){return lname;}
char getGender(){return gender;}
int getAge(){return age;}
double getIncome(){return income;}
void Disp(){cout<<fname<<" " <<lname<<" " <<age<<" " <<job<<" " <<gender<<" " <<income<<" " <<endl;}
};
int main()
{
ofstream ofile;
ofile.open("match.txt" );
Person person[8];
ifstream infile;
infile.open("dates.txt" );
for (int x=0; x<8; x++)
{
person[x].setFname();
person[x].setLname();
person[x].setGender();
person[x].setAge();
person[x].setIncome();
}
system("pause" );
return 0;
}
Mar 9, 2013 at 1:45am UTC
I see a line where the infile
is opened. But I don't see any place in the code where that file is being accessed.
It would be useful to see what the data file actually contains. Only by understanding the data is it possible to decide how it should be read and interpreted.
Mar 9, 2013 at 1:47am UTC
I don't know what I'm doing wrong.
I would begin by looking at and addressing errors flagged by the compiler.
Mar 9, 2013 at 2:00am UTC
The text file has the following information
Mario Lopez 25 M Actor 200000
Emanuel Goldstein 45 M Hacker 100000
Emily Autumn 26 F Singer 200000
Betty White 87 F Comedian 130000
Nancy White 21 F Model 80000
Irene Godinez 25 F Student 5200
Brad Pitt 60 M Slave 0
Cee Garza 26 F Insurance 40000
Mar 9, 2013 at 2:24am UTC
Here is the backbone but...It has worked for me back then..but now...IDK can you figure a way how to read off of the file... -_-...you have failed me .getline()...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <fstream>
using namespace std;
class INFO
{
public :
char FirstName[256];
char LastName[256];
char Job[256];
char Gender; //M or F
char Hash[256];
double Income;
int Age;
};
int main(int nNumberofArgs,char * pszArgs[])
{
INFO person[256];
char filename[256];
cout << "Enter the name of file you want to open:" ;
cin.getline(filename,256);
for (int s = 0;s != '\n' ;s++)
{
if (filename[s] == NULL)
{
filename[s] = '.' ;
filename[s + 1] = 't' ;
filename[s + 2] = 'x' ;
filename[s + 3] = 't' ;
filename[s + 4] = NULL;
break ;
}
}
cout << "Opening and reading contents of '" << filename << "'" << endl;
ifstream File(filename);
int I = 0;
for (int i = 1;;i++)
{
char object[256];
int object_;
double object__;
File.getline(object,256);
for (int a = 0;a < 256;a++)
{
person[i].FirstName[a] = object[a];
}
File.getline(object,256);
for (int a2 = 0;a2 < 256;a2++)
{
person[i].LastName[a2] = object[a2];
}
File.getline(object,256);
for (int a3 = 0;a3 < 256;a3++)
{
person[i].Job[a3] = object[a3];
}
File >> object_;
person[i].Gender = object_;
File >> object__;
person[i].Income = object__;
File >> object_;
person[i].Age = object_;
File.getline(object,256);
for (int a4 = 0;a4 < 256;a4++)
{
person[i].Hash[a4] = object[a4]; //A line in the .txt file which is "-----------------"
}
if (File.fail())
{
cout << "File read end..." << endl;
break ;
}
I++;
}
for (int i2 = 1;i2 < I;i2++)
{
cout << "First name:\t" << person[i2].FirstName << endl;
cout << "Last name:\t" << person[i2].LastName << endl;
cout << "Job:\t" << person[i2].Job << endl;
cout << "Gender:\t" << person[i2].Gender << endl;
cout << "Income:\t" << person[i2].Income << endl;
cout << "Age:\t" << person[i2].Age << endl;
cout << "----------------------------------" << endl;
}
system("PAUSE" );
return 0;
}
Mar 9, 2013 at 2:48am UTC
what would be the easiest way of doing what i need to do. I have a C++ book in front of me but it kind of sucks because they use very few examples. I don't know how to read the code you gave me as an example, I am feeling hopeless right now. Please help.
Mar 9, 2013 at 2:58am UTC
I've got to stand by my original recommendation:
cire wrote:I would begin by looking at and addressing errors flagged by the compiler.
Your original code won't even compile, so everything else is pretty much irrelevant.
Mar 9, 2013 at 3:05am UTC
I have done that, I am not an expert in C++ but I am not that far back.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
#include<iostream>
#include<fstream>
#include<string>
#include<iomanip>
using namespace std;
class Person
{
private :
string fname, lname, job;
char gender;
double income;
int age;
public :
void setFname(string F){fname=F;}
void setLname(string L){lname=L;}
void setGender(char G){gender=G;}
void setAge(int A){age=A;}
void setIncome(double I){income=I;}//********************************************************************************************
string getFname(){return fname;}
string getLname(){return lname;}
char getGender(){return gender;}
int getAge(){return age;}
double getIncome(){return income;}
void Disp(){cout<<fname<<" " <<lname<<" " <<age<<" " <<job<<" " <<gender<<" " <<income<<" " <<endl;}
};
int main()
{
ofstream ofile;
ofile.open("match.txt" );
Person person[8];
ifstream infile;
infile.open("dates.txt" );
for (int x=0; x<8; x++)
{
infile>>person[x].setFname()>>person[x].setLname()>>person[x].setGender()>>person[x].setAge()>>person[x].setIncome();
}
system("pause" );
return 0;
}
This is where I have a problem
1 2 3 4
for (int x=0; x<8; x++)
{
infile>>person[x].setFname()>>person[x].setLname()>>person[x].setGender()>>person[x].setAge()>>person[x].setIncome();
}
the error it shows when it intends to compile is "no operator matches these operands ">>".
Last edited on Mar 9, 2013 at 3:07am UTC
Mar 9, 2013 at 3:14am UTC
Are you unfamiliar with the term "indentation?"
The error it shows when it intends to compile is "no operator matches these operands ">>".
And that's true.
infile >> void
doesn't make much sense. Note that
setFname is a function which returns type void. You might also note that it's a function which takes an argument (which you aren't supplying.)
If you were extracting input from
cin is this the way you would do it?
Topic archived. No new replies allowed.