I have written a simple program in two ways. The program is for getting data from a user, storing it in a txt file, retrieving the data and displaying it. The problem is that one approach works while the other does not. I dont understand why.
The one that works is this:
#include <iostream>
#include <fstream>
using namespace std;
class customer{
    public:
    // Declaring member variables and functions
    string name, address, telNo;
    int age;
    void createCustomer();
    void displayInfo(string inputName);
};
// Member function of customer to enter new customer details
void customer::createCustomer(){
    customer c;
    ofstream file;
    file.open("customers.txt", ios::out);
    cout << "Enter Name: ";
    cin >> c.name;
    cout << "Enter Age: ";
    cin >> c.age;
    cout << "Enter Address: ";
    cin >> c.address;
    cout << "Enter Telephone Number: ";
    cin >> c.telNo;
    file.write((char*)&c, sizeof(c));
    file.close();
    cout << "\n";
}
// Member function of customer to display customer information
void customer::displayInfo(string inputName){
    customer c;
    ifstream file;
    file.open("customers.txt", ios::in);
    if(!file){
        cout << "Could Not Open customers.txt File\n";
        return;
    }
while(!file.eof()){
file.read((char*)&c, sizeof(c));
if (inputName == c.name){
    cout << "\n";
    cout << "Name-------------> " << c.name << endl;
    cout << "Age--------------> " << c.age << endl;
    cout << "Telephone Number-> " << c.telNo << endl;
    cout << "Address----------> " << c.address << endl;
    cout << "\n";
    break;
    }
}
    file.close();
}
int main(){
    customer c;
    c.createCustomer();
    c.displayInfo("name");
    return 0;
}
It gives the following output:
Enter Name: name
Enter Age: 21
Enter Address: add
Enter Telephone Number: telno
Name-------------> name
Age--------------> 21
Telephone Number-> telno
Address----------> add
The one that doesnt work is this:
#include <iostream>
#include <fstream>
using namespace std;
class customer{
    public:
    // Declaring member variables and functions
    string name, address, telNo;
    int age;
    void createCustomer();
    void displayInfo();
};
// Member function of customer to enter new customer details
void customer::createCustomer(){
    customer c;
    ofstream file;
    file.open("customers.txt", ios::out);
    cout << "Enter Name: ";
    cin >> c.name;
    cout << "Enter Age: ";
    cin >> c.age;
    cout << "Enter Address: ";
    cin >> c.address;
    cout << "Enter Telephone Number: ";
    cin >> c.telNo;
    file.write((char*)&c, sizeof(c));
    file.close();
    cout << "\n";
}
// Member function of customer to display customer information
void customer::displayInfo(){
    string inputName = "name";
    customer c;
    ifstream file;
    file.open("customers.txt", ios::in);
    if(!file){
        cout << "Could Not Open customers.txt File\n";
        return;
    }
    while(!file.eof()){
        file.read((char*)&c, sizeof(c));
        if (inputName == c.name){
            cout << "\n";
            cout << "Name-------------> " << c.name << endl;
            cout << "Age--------------> " << c.age << endl;
            cout << "Telephone Number-> " << c.telNo << endl;
            cout << "Address----------> " << c.address << endl;
            cout << "\n";
            break;
        }
    }
    file.close();
}
int main(){
    customer c;
    c.createCustomer();
    c.displayInfo();
    return 0;
}
All I have done is put the string inputName variable inside the function instead of passing it as an argument.
It gives only the following output:
Enter Name: name
Enter Age: 21
Enter Address: add
Enter Telephone Number: telno
If I remove the if condition:
while(!file.eof()){
    file.read((char*)&c, sizeof(c));
    //if (inputName == c.name){
        cout << "\n";
        cout << "Name-------------> " << c.name << endl;
        cout << "Age--------------> " << c.age << endl;
        cout << "Telephone Number-> " << c.telNo << endl;
        cout << "Address----------> " << c.address << endl;
        cout << "\n";
        break;
    //}
}
i get the following output:
Enter Name: name
Enter Age: 21
Enter Address: add
Enter Telephone Number: telno
Name-------------> 0∙⌂┼
Age--------------> 21
Telephone Number-> name
Address----------> §
Why are the name and address fields outputting random symbols and the telephone number field outputting value of the name field?
 
    