Answer to Question #216457 in C++ for Tushar

Question #216457
#include <iostream>
#include <cstring>
#include <fstream>
#include <ctime>
#include <conio.h>
using namespace std;
time_t t; // t passed as argument in function time()
struct tm * tt; // declaring variable for localtime()

struct customer{
char phone[22];
char name[20];
char address[50];
};

struct reservation{
int yearday, month, date;
int customerid[20];//note that array index is hotel room number.
};

reservation rese[366];

void addcust(){
// Max Marks 5
//You are required to add customer in a
// customer file with attributes as given in struct customer.
}
1
Expert's answer
2021-07-12T17:18:00-0400
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstring>
#include <stdlib.h>
#include <stdio.h>
#include <fstream>
#include <ctime>
#include <conio.h>
using namespace std;


time_t t; // t passed as argument in function time()


struct tm* tt; // declaring variable for localtime()


struct customer {


	char phone[22];


	char name[20];


	char address[50];


};


struct reservation {


	int yearday, month, date;


	int customerid[20];//note that array index is hotel room number.


};


reservation rese[366];


void addcust(const char*fname,const char *_phone,const char *_name,const char* adres) {
//Create file Handler
	//fname-file name(.txt) _phone-phone customer...
	ofstream out(fname,ios_base::app);//Create file
	out << _phone << " " << _name << " " << adres << "\n";//Write date to file
	out.close();//Close file
	cout << "Succefuly add customer!!!\n";
}
//Example demo
//Read Customer 
void ReadCustomerFile(const char* fname, customer* custs,int *size)//Read Date to array customers
{
	//*size=pointer int size of array customer(count customers)
	*size = 0;
	FILE* fl;//File handler
	fl = fopen(fname, "r+");//Open file C mode
	if (!fl)
	{
		cout << "File not found!!!\n";
		return;
	}
	char _ph[22];
	char _nm[20];
	char adrs[50];
	//Read customers
	while (fscanf(fl,"%s%s", _ph, _nm)==2)
	{
		cout << _ph << " " << _nm << endl;
		fscanf(fl, "\n");
		fgets(adrs, 50, fl);//Read Adress
		strcpy(custs[*size].phone, _ph);
		strcpy(custs[*size].name, _nm);
		strcpy(custs[*size].address, adrs);
		*size = *size + 1;
	}
	fclose(fl);
}
int main()
{
	addcust("Customer.txt", "+123456789", "Chris", "USA");
	addcust("Customer.txt", "+123457789", "Jon", "Canada");
	addcust("Customer.txt", "+123456789", "Cumir", "Russia");
	addcust("Customer.txt", "+123454789", "Dayan", "Cuba");
	//Example read
	customer* ct = new customer[366];
	int size = 0;
	ReadCustomerFile("Customer.txt", ct, &size);
	for (int i = 0; i < size; i++)
	{
		cout << ct[i].phone << " " << ct[i].name << " " << ct[i].address << endl;
	}
	return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS