Records: Write a C++ program that can open and read data from an external data f
ID: 3914463 • Letter: R
Question
Records:
Write a C++ program that can open and read data from an external data file and then display that data on the screen for viewing in the desired format. The data file contains customer information for a fictional company’s records. The maximum number of records could be up to 100. From the file, in each row there is an ID number, a name and the balance due related to that specific customer. The display the program needs to output should be formatted in this particular order: ID (to be sorted in ascending order), Name, balance (from data input file) and 15% of that balance for each customer (to be calculated by the program) in a single row.
Lastly, the data displayed on-screen should also be written out to another data file for recording purposes (all columns MUST be aligned, and floating-point values should have precision set to 2, with 2 digits after the dot). Both data files are text files that could be read from Notepad, and their names (COULD HAVE MORE THAN ONE WORD WITH THE FILENAME) must be allowed to enter at the run time of the program.
Please see the examples below for formatting details
- In the data file to be read in, the data will look like the following:
77234 Ashley White# 4563.50
12345 Brad Smith# 128923.45
87123 Lisa Johnson# 2345.93
Then, on the screen AND in the file to be saved for recording purposes, the data should look like the following:
12345 Brad Smith 128923.45 19338.52
77234 Ashley White 4563.50 684.52
87123 Lisa Johnson 2345.93 351.89
Use the following sample file as input data file:
Explanation / Answer
Solution:
#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include <cmath>
using namespace std;
struct Data
{
int ID;
string Name;
string Customer_name;
int amount1;
int amount2;
int total;
};
const int SIZE = 9;
const int INFO = 4;
void openInputFile(ifstream &, string);
void getTotal(Data[]);
void sort(Data[]);
int main()
{
Data arr[SIZE];
ifstream inFile;
string inFileName = "C:\Users\Lisa\Desktop\scores.txt";
openInputFile(inFile, inFileName);
for(int count = 0; count < SIZE; count++)
{
inFile >> arr[count].ID >> arr[count].Name >> arr[count].Customer_name >> arr[count].amount1 >> arr[count].amount2 >> arr[count].total;
}
inFile.close();
getTotal(arr);
cout << " ";
return 0;
}
void openInputFile(ifstream &inFile, string inFileName)
{
inFile.open(inFileName);
if (!inFile)
{
cout << "Error to open file." << endl;
cout << endl;
return;
}
}
void getTotal(StudentData arr[])
{
for(int i = 0; i < SIZE; i++)
{
arr[i].total = arr[i].amount1 + arr[i].amount2;
}
}
void sortData arr[])
{
Data temp;
for (int i = 0; i < (SIZE - 1); i++)
{
for (int j = i + 1; j < SIZE; j++)
{
if (arr[i].Name > arr[j].Name)
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.