Struct with its own input/output operators???

Natural C++ input loop is

1
2
3
4
5
6
7
8
9
char[25] Name;
int StartSalary;
int Age;
ifstream indata;

while( /* input operation, such as indata >> data */ )
{
    // process data
]


which in my case

1
2
3
4
5
while( indata.getline(Name,25,'~')
       && indata >> StartSalary >> Age )
{
   cout >> Name >> StartSalary >> Age >> endl;
}


I received the following suggestion

If the input operation gets too complex, turn your record into a struct with its own input/output operators, so the loop stays simple.

so struct like this

1
2
3
4
5
6
struct Personel_record
{
char[25] Name;
int StartSalary;
int Age;
};


what to do for its own input/output operators - example using the givin info here in code form would be of the most help.
Last edited on
I gave you the suggestion, so I'll show how it might look in code:

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
#include <string>
#include <fstream>
#include <iostream>

struct Personel_record
{
    std::string Name;
    int StartSalary;
    int Age;
};

std::istream& operator>>(std::istream& is, Personel_record& pr)
{
    Personel_record new_pr;
    if(    is >> std::ws
        && std::getline(is, new_pr.Name, '~')
        && is >> new_pr.StartSalary >> new_pr.Age )
    {
        pr = new_pr; // could do more validation here
    }
    return is;
}

std::ostream& operator<<(std::ostream& os, const Personel_record& pr)
{
    return os << pr.Name << " " << pr.StartSalary << " " << pr.Age << '\n';
}

int main()
{
    std::ifstream indata("test.txt");

    Personel_record pr;
    while(indata >> pr)
    {
        std::cout << pr;
    }
}
cant figure how to ask my next question so how about this

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
#include <string>
#include <fstream>
#include <iostream>

struct Personel_record
{
    std::string Name;
    int StartSalary;
    int Age;
};

struct dog
{
   std::string breed;
   int age;
};

std::istream& operator>>(std::istream& is, Personel_record& pr)
{
    Personel_record new_pr;
    if(    is >> std::ws
        && std::getline(is, new_pr.Name, '~')
        && is >> new_pr.StartSalary >> new_pr.Age )
    {
        pr = new_pr; // could do more validation here
    }
    return is;
}

std::istream& operator>>(std::istream& is, dog& doggy)
{
    dog new_doggy;
    if( std::getline(is, new_doggy.breed, '~')
        && is >> new_doggy.age )
    {
        doggy = new_doggy; 
    }
    return is;
}



int main()
{
    std::ifstream indata("test.txt");
    std::ifstream indog("dog.txt");

    Personel_record pr;
    while(indata >> pr)
    {
        std::cout << "complete"; //if i comprehend the IN I should get the OUT
    }

   dog doggy;
   while(indata >> doggy)
    {
        std::cout << "complete"; //if i comprehend the IN I should get the OUT
    }
}

}


The above is just a guess - but if I am on the right track - what connects
while(indata >> pr) and while(indata >> doggy)to thier proper stream functions. HOpe I said this correctly?
since pr and dog are not simple types of variables like int or char or double but instead structs - I am asuming c++ in this case will look for a function to resolve the request . It will use the function that contains the proper variable parameters - In this case istream and the correct stuct - so pr looks for the parameter of personal_records and doggy looks for the parameter of dog????

close????
Last edited on
hey Iamk2...I tried your method in the linux forum but I get:
bash: /etc/modules: Permission denied
any help?
please reply on forum post broadcom802.11 issues...well..you know the place
Last edited on
Send me a link to the proper forum post and I will help you there. Dont want to use cplusplus resources for a non cplusplus issue.
OP said:
...what connects
while(indata >> pr) and while(indata >> doggy)to thier proper stream functions.


The functions defined on Lines 18 and 30 are what "connects" the stream and object type with their proper function call. This is called operator overloading, it tells the program what to do when it sees the operator you specify associated with the data types that you pass as arguments.
Last edited on
Thanks geek -been pounding the pages on overloaded operations and have a descent handle on them now - thanks for the response
Topic archived. No new replies allowed.