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....
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...
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...
In C++, Create an array of 10 characters. Use a loop to prompt for 10 letters...
In C++, Create an array of 10 characters. Use a loop to prompt for 10 letters (a-z|A-Z) and store each in the array. If an invalid character is entered, re-prompt until a valid char is entered. After 10 characters are stored in the array, use a loop to print out all characters in the array from the first to the last element. Then, use another loop starting at the last element to print all characters in the array in reverse...
Create in C++ Prompt the user to enter a 3-letter abbreviation or a day of the...
Create in C++ Prompt the user to enter a 3-letter abbreviation or a day of the week and display the full name of the day of the week. Use an enumerated data type to solve this problem. Enumerate the days of the week in a data type. Start with Monday and end with Friday. Set all of the characters of the user input to lower case. Set an enumerated value based on the user input. Create a function that displays...
Prompt the user to enter an integer Then, prompt the user to enter a positive integer...
Prompt the user to enter an integer Then, prompt the user to enter a positive integer n2. Print out all the numbers that are entered after the last occurrence of n1 and whether each one is even or odd If n1 does not occur or there are no values after the last occurrence of n1, print out the message as indicated in the sample runs below. Sample: Enter n1: -2 Enter n2: 7 Enter 7 values: -2 3 3 -2...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT