A small business wants to create a file to store its customer information. Each record in the file is to
contain the following data: customer ID number, first name, last name, city, zip code, and account balance.
Write a program to create such a customer file named “customer.txt”. Write the file with space-separated
fields.
Write another program that displays the contents of the file “customer.txt” in column form with
appropriate column headings.
First Program
The program will create a customer file named “customer.txt” and store the information of customers.
#include<iostream>
#include<fstream>
using namespace std;
int main(){
int ID_number;
string first_name, last_name, city, zip_code;
double account_balance;
fstream infile;
infile.open("customer.txt", ios::out);
cout<<"How many customers do you want to store their information\n";
int number;
cin>>number;
for(int i=0; i<number; i++){
cout<<"Customer: "<<(i+1)<<endl;
cout<<"Enter customer ID number\n";
cin>>ID_number;
cout<<"Enter customer first name \n";
cin>>first_name;
cout<<"Enter customer last name \n";
cin>>last_name;
cout<<"Enter customer City \n";
cin>>city;
cout<<"Enter Zip Code \n";
cin>>zip_code;
cout<<"Enter customer Account Balance \n";
cin>>account_balance;
infile<<ID_number<<"\t\t"<<first_name<<"\t\t"<<last_name<<"\t\t"<<city<<"\t\t"<<zip_code<<"\t\t"<<account_balance<<endl;
}
infile.close();
}
Second Program
The program prints the contents of the file “customer.txt” in column form with
appropriate column headings.
#include<iostream>
#include<fstream>
using namespace std;
int main(){
fstream infile;
infile.open("customer.txt",ios::in);
cout<<"ID Number\tFirst Name\t Last Name\tcity\t Zip Code\tAccount Balance.\n";
string str;
while(getline(infile, str)){
cout<<str<<endl;
}
}
Comments
Leave a comment