In: Computer Science
C++
comments would be nice, especially with "Customers.dat" part.
Please don't use an already answered solution from chegg. I've unfortunately had that happen at many occasions.
I'll most certainly upvote if program is right. I always do
......
......
Statement: Create a program that will loop and prompt to enter the highlighted data items in the structure below. This is every item except customerNumber , isDeleted and newLine;
const int NAME_SIZE = 20;
const int STREET_SIZE = 30;
const int CITY_SIZE = 20;
const int STATE_CODE_SIZE = 3;
struct Customers { long customerNumber; char name[NAME_SIZE]; char streetAddress_1[STREET_SIZE]; char streetAddress_2[STREET_SIZE]; char city[CITY_SIZE]; char state[STATE_CODE_SIZE]; int zipCode;
char isDeleted;
char newLine;
};
Always set the item isDeleted to 'N' and
newline to '\n'. The item newLine
is a convenient item that is there to assist in viewing the
contents of the file using "type filename" the cmd window.
Notepad will show the binary chars and will not line up the data as expected. You may see some odd characters after the expected data for the character arrays. That is normal for C/C++.
The item customerNumber should start at 0 and increase by 1 for every record written.
Once the data in the structure is loaded, write it to the file
"Customers.dat" and prompt to continue. If the reply is to not
continue, close the file and exit.
The file "Customers.dat" must be opened in Binary mode.
Deliverable is a working CPP program.
C++ program:
#include<iostream>
#include<fstream>
#include<string>
#include<ios>
#include<limits>
using namespace std;
const int NAME_SIZE = 20;
const int STREET_SIZE = 30;
const int CITY_SIZE = 20;
const int STATE_CODE_SIZE = 3;
struct Customers {
long customerNumber;
char name[NAME_SIZE];
char streetAddress_1[STREET_SIZE];
char streetAddress_2[STREET_SIZE];
char city[CITY_SIZE];
char state[STATE_CODE_SIZE];
int zipCode;
char isDeleted;
char newLine;
};
int main(){
//make a new object of Customers struct
Customers c;
c.customerNumber = 0;
//opening the file in binary mode
ofstream fp("Customers.dat", ios::out | ios::binary);
if(!fp) {
cout << "Cannot open file!" << endl;
return 0;
}
//take user inputs continuously until user asks for exit
while(1){
//cin.getline is used to input a line with spaces .
cout<<"\n\nName: ";
cin.getline(c.name, NAME_SIZE);
cout<<"\nStreet Address 1: ";
cin.getline(c.streetAddress_1, STREET_SIZE);
cout<<"\nStreet Address 2: ";
cin.getline(c.streetAddress_2, STREET_SIZE);
cout<<"\nCity: ";
cin.getline(c.city, CITY_SIZE);
cout<<"\nState: ";
cin>>c.state;
cout<<"\nZip Code: ";
cin>>c.zipCode;
//flush input stream
cin.ignore(numeric_limits<streamsize>::max(),'\n');
//setting values of isDeleted amd newLine
c.isDeleted = 'N';
c.newLine = '\n';
//incrementing customer count
c.customerNumber++;
//write data to Customers.dat
fp.write((char*)&c, sizeof(Customers));
char op2;
cout<<"\nDo you want to continue? (Y/N): ";
cin>>op2;
//flush input stream
cin.ignore(numeric_limits<streamsize>::max(),'\n');
//checking if user wants to exit the program
if(op2 == 'N') {
cout<<"Exiting the program....."<<endl;
//closing the file
fp.close();
break;
}
}
return 0;
}
Output: