Reputation: 27
I am trying to get variables from a txt file which looks like;
1 Prince Heins 25
2 Lady Bridgette 29
3 Tony Ann 223
4 Lucy Phoenix 35
and here is my code but i can not get the variables from txt file to myArray. I created them with the number of line by using getline and linenumber is equal to linecount . Then i created many objects which is equal to linecount number. Next step was getting the variables from txt file line by line and i tried to use while(pbin) which is my read function. I am trying to send variables to class by using set functions which includes my strings and integer but i get an compile error:[Error] no match for 'operator>>' (operand types are 'std::ifstream {aka std::basic_ifstream}' and 'void') I checked the examples but none of them solved my issue. Couldn't get the issue and fix it. I would be glad if someone helps.
#include <iostream>
#include <string>
#include <conio.h>
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
#include <sstream>
#include <istream>
#include <iomanip>
#include <cstring>
using namespace std;
class contact{
private:
int listno;
string name;
string surname;
string phonenumber;
public:
contact(){
this->name="Unknown";
this->surname="Unknown";
this->phonenumber="Unknown";
this->listno=0;
}
contact (string name,string surname,string phonenumber){
this->name=name;
this->surname=surname;
this->phonenumber=phonenumber;
this->listno=0;
}
contact(int listno,string name,string surname,string phonenumber){
this->name=name;
this->surname=surname;
this->listno=listno;
this->phonenumber=phonenumber;
}
void setListno(int listno){
this->listno=listno;
}
int getListno(){
return listno;
}
void setName(string name){
this->name=name;
}
string getName(){
return name;
}
void setSurname(string surname){
this->surname=surname;
}
string getSurname(){
return surname;
}
void setNumber(string phonenumber){
this->phonenumber=phonenumber;
}
string getNumber(){
return phonenumber;
}
void showInfos(){
cout << "Listno: " << this->listno << endl;
cout << "Name: " << this->name << endl;
cout << "Surname: " << this->surname << endl;
cout << "Number: " << this->phonenumber<<endl;
}
};
int main(){
int mListno;
string mName;
string mSurname;
string mNumber;
ifstream pbin("phoneData2.txt");
string line;
long linecount;
contact* myArray = new contact[linecount];
for(linecount=0;getline(pbin,line);linecount++);
if(pbin.is_open()){
int i;
for(i=0;i<linecount;i++){
while(
pbin >> myArray[i].setListno(mListno);
pbin >> myArray[i].setName(mName);
pbin >> myArray[i].setSurname(mSurname);
pbin >> myArray[i].setNumber(mNumber);
)
}
}
pbin.close();
return 0;
}
edited:
#include <iostream>
#include <string>
#include <conio.h>
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
#include <sstream>
#include <istream>
#include <iomanip>
#include <cstring>
using namespace std;
class contact{
private:
int listno;
string name;
string surname;
string phonenumber;
public:
contact(){
this->name="Unknown";
this->surname="Unknown";
this->phonenumber="Unknown";
this->listno=0;
}
contact (string name,string surname,string phonenumber){
this->name=name;
this->surname=surname;
this->phonenumber=phonenumber;
this->listno=0;
}
contact(int listno,string name,string surname,string phonenumber){
this->name=name;
this->surname=surname;
this->listno=listno;
this->phonenumber=phonenumber;
}
void setListno(int listno){
this->listno=listno;
}
int getListno(){
return listno;
}
void setName(string name,int count){
this->name=name[count];
}
string getName(){
return name;
}
void setSurname(string surname){
this->surname=surname;
}
string getSurname(){
return surname;
}
void setNumber(string phonenumber){
this->phonenumber=phonenumber;
}
string getNumber(){
return phonenumber;
}
void showInfos(){
cout << "Listno: " << this->listno << endl;
cout << "Name: " << this->name << endl;
cout << "Surname: " << this->surname << endl;
cout << "Number: " << this->phonenumber<<endl;
}
friend istream & operator>> (istream & in, contact & con){
in >> con.listno >> con.name >> con.surname >> con.phonenumber;
return in;
}
};
int main(){
ifstream pbin("phoneData2.txt");
string line;
long linecount=0;
contact* myArray = new contact[linecount];
for(linecount=0;getline(pbin,line);linecount++);
pbin.seekg(0);
if(pbin.is_open()){
int i;
for(i=0;i<linecount;i++){
if(! (pbin >> myArray[i]))
{
cout << "The contact is not found." ;
}
}
}
pbin.close();
return 0;
}
Upvotes: 0
Views: 75
Reputation: 33932
Add to contact
a >>
overload
friend std::istream & operator>> (std::istream & in, contact & con)
{
in >> con.listno >> con.name >> con.surname >> con.phonenumber;
return in;
}
and then in main
, the for
loop becomes
for(i=0;i<linecount;i++){
if (! (pbin >> myArray[i]))
{ // failed to read a contact
// do something here to clean up the mess
}
}
To round out a couple of the other mistakes,
int main(){
ifstream pbin("phoneData2.txt");
string line;
long linecount = 0; // otherwise you don't know what number linecount starts at
contact* myArray = new contact[linecount];
for(linecount=0;getline(pbin,line);linecount++);
pbin.clear(); // Clear the error flag from hitting the end of the file
// Needed when building to older C++ Standards
pbin.seekg(0); // rewind the file
if(pbin.is_open()){
int i;
for(i=0;i<linecount;i++){
if (! (pbin >> myArray[i]))
{
// do something here to clean up the mess
}
}
}
pbin.close();
return 0;
}
Upvotes: 0