Question

In: Computer Science

Create a program that will loop and prompt to enter the highlighted data items in the...

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.

Solutions

Expert Solution

#include<iostream>
#include<fstream>
using namespace std;

const int NAME_SIZE = 20;
const int STREET_SIZE = 30;
const int CITY_SIZE = 20;
const int STATE_CODE_SIZE = 3;

// Structure
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 = 'N';
    char newLine = '\n';
}; 

int main()
{
        int n;
        // Number of Data Entries (Loops)
        cout<<"Enter the number of entries: ";
        cin>>n;
        struct Customers data[n];
        
        for(int i=0; i<n; i++)
        {
                cout<<"enter name: ";
                cin>>data[i].name;
                cout<<"enter streetAddress_1: ";
                cin>>data[i].streetAddress_1;
                cout<<"enter streetAddress_2: ";
                cin>>data[i].streetAddress_2;
                cout<<"enter city: ";
                cin>>data[i].city;
                cout<<"enter state: ";
                cin>>data[i].state;
        }
        
    // Write into the file
        ofstream myfile;
        myfile.open("Customers.dat", fstream::binary | fstream::out);
        if (!myfile)
        cout << "File Not Found." << endl;
    else
    {
        for(int i=0; i<n; i++)
        {
                myfile << data[i].name << data[i].newLine << data[i].streetAddress_1 << data[i].newLine << data[i].streetAddress_2 << 
                        data[i].newLine << data[i].city << data[i].newLine << data[i].state << data[i].newLine << data[i].newLine;
                }
        }
        myfile.close();
        
        cout<< "Reading The (Customers.dat) file .............\n";
    // Read from the file
        ifstream instream("Customers.dat", fstream::binary | fstream::out);
        string line;
    // Read line by line
        while (instream >> line)
        {
        cout << line;
        if (instream.peek() == '\n') //detect "\n"
        {
                cout <<endl;
        }
        }
        instream.close();
        return 0;
}

Note :

  1. Structure data is of 'char' datatype so do not enter space in between. But if you want to enter any space then change the 'char' data type to 'string'.
  2. Do take care of the data sizes of the variables mentioned in the code as per the question.

Explanation:

  • ofstream is used to write or append data into the file.
  • ifstream is used to read data from the file.
  • instream is used to read data line by line in the ifstream object of the file.

Hope it helps. In case of any query please write it in the comment section.

Good Luck :)


Related Solutions

Create in Java a program that will prompt the user to enter aweight for a...
Create in Java a program that will prompt the user to enter a weight for a patient in kilograms and that calculates both bolus and infusion rates based on weight of patient in an interactive GUI application, label it AMI Calculator. The patients weight will be the only entry from the user. Use 3999 as a standard for calculating BOLUS: To calculate the BOLUS you will multiply 60 times the weight of the patient for a total number. IF the...
Create in java a program that will prompt the user to enter a weight for a...
Create in java a program that will prompt the user to enter a weight for a patient in kilograms and that calculates infusion rates based on weight of patient in an interactive GUI application, label it HEPCALC. The patients’ weight will be the only entry from the user. To calculate the infusion rate you will multiply 12 times the weight divided by 50 for a total number. The end result will need to round up or down the whole number....
5) Create the following in a Java program Create a scanner Prompt the user to enter...
5) Create the following in a Java program Create a scanner Prompt the user to enter the name where the box of mail is shipping from and create the variable and relate to scanner Prompt the user to enter the name of destination where the box of mail will be shipped and create the variable and relate to scanner Prompt the user to enter the weight of the package and create variable to relate to scanner Calculate cost of shipping...
6) Create the following in a Java Program: Create payroll system Prompt the user to enter...
6) Create the following in a Java Program: Create payroll system Prompt the user to enter payroll information Employee name and create variable for scanner Hours worked Hourly pay rate Federal tax rate State tax rate Declare variable doubles for gross pay, federal, state and total deduction Display payroll statement example: Name of employee Hours worked Hourly pay rate Gross pay which is equal hours worked multiply hourly pay rate Federal withholding which is equal of gross pay multiply federal...
Create a program that will prompt me for each of the following items: 1. Date of...
Create a program that will prompt me for each of the following items: 1. Date of Birth 2. Typical wake up time 3. Next Dentist Appointment. ( Date and Time) Create a Date structure and a method to load and return the date structure. Create a Time-of-day structure and a method to load and return the Time-of-day structure. Create a DateTime structure that includes the Date and Time-Of-Day structure. The Date structure should include year, month, day. The Time-of-day structure...
Create a program that creates a sorted list from a data file. The program will prompt...
Create a program that creates a sorted list from a data file. The program will prompt the user for the name of the data file. Create a class object called group that contains a First Name, Last Name, and Age. Your main() function should declare an array of up to 20 group objects, and load each line from the input file into an object in the array. The group class should have the following private data elements: first name ,last...
IN C This assignment is to write a program that will prompt the user to enter...
IN C This assignment is to write a program that will prompt the user to enter a character, e.g., a percent sign (%), and then the number of percent signs (%) they want on a line. Your program should first read a character from the keyboard, excluding whitespaces; and then print a message indicating that the number must be in the range 1 to 79 (including both ends) if the user enters a number outside of that range. Your program...
Program Behavior Each time your program is run, it will prompt the user to enter the...
Program Behavior Each time your program is run, it will prompt the user to enter the name of an input file to analyze. It will then read and analyze the contents of the input file, then print the results. Here is a sample run of the program. User input is shown in red. Let's analyze some text! Enter file name: sample.txt Number of lines: 21 Number of words: 184 Number of long words: 49 Number of sentences: 14 Number of...
Create a program that parses a CSV file of product data and prints the items with...
Create a program that parses a CSV file of product data and prints the items with a price that is less than or equal to that input by the user. • Your program should take two arguments: an input file to process and a price limit. • Print only the names of each item to stdout that have a price less than or equal to the given limit. • If the given file does not exist or cannot be opened,...
ou will create a GUI data entry program to be used to enter Customer Information. This...
ou will create a GUI data entry program to be used to enter Customer Information. This program should be based on the requirements and design you created in previous modules. It will be a GUI, based either on the Java Swing classes or JavaFX. The program will allow for entry of all fields described in your requirements and include appropriate validations for each field. It will be left to you to decide if the validations methods should be part of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT