Question

In: Computer Science

Write a C++ program in a file called pop.cpp that opens a file called pop.dat which...

Write a C++ program in a file called pop.cpp that opens a file called pop.dat which has the following data (you’ll have to create pop.dat)

AX013 1.0

BX123456 1234.56

ABNB9876152345 99999.99

The data are account numbers and balances. Account numbers are, at most 14 characters. Balances are, at most, 8 characters (no more than 99999.99). Using a loop until the end of file, read an account number and balance then write these to standard output in the following format shown below. Use the I/O manipulators setw, left, and right. Set the precision/fixed/showpoint so that all numeric data is written are written with exactly two digits to the right of the decimal.

Account Number    Balance

-------------------------

AX013          $     1.00

BX123456       $ 1234.56

ABNB9876152345 $ 99999.99

Solutions

Expert Solution

CODE:

#include<iostream>
#include<iomanip>
#include<bits/stdc++.h>
#include<sstream>

using namespace std;

void readFile(string filename){
//reading and opening the file
fstream file;
file.open(filename.c_str());
string word;
//printing the header
cout<<setw(20)<<left<<"Account Number"<<setw(15)<<left<<"Balance"<<endl;
cout<<setw(20)<<left<<"--------------"<<setw(15)<<left<<"-------"<<endl;
int counter = 0;
//reading the file
while(file>>word){
//if counter == 0
if(counter == 0){
//it is a bank account number
cout<<setw(20)<<left<<word<<"$ ";
counter = 1;
}else if(counter == 1){
//if counter is 1 it is the balance
stringstream ss(word);
//converting string to double
cout.precision(2);
double x;
ss>>x;
//printing the balance
cout<<setw(15)<<left<<fixed<<x<<endl;
counter = 0;
}
}
//closing the file
file.close();
}
//main method
int main(){
//calling the file
readFile("pop.dat");
return 0;
}
__________________________________________

CODE IMAGES:

______________________________________________

OUTPUT:

pop.dat

__________________________________________

Feel free to ask any questions in the comments section

Thank You!


Related Solutions

Write a C program that opens a file called "numbers.txt" in writing mode. The program should...
Write a C program that opens a file called "numbers.txt" in writing mode. The program should then read floating point numbers from the keyboard, and write these lines to the opened file one per line, stopping when the number 0 is entered. Your program should check to make sure that the file was opened successfully, and terminate if it was not.
write a program in c++ that opens a file, that will be given to you and...
write a program in c++ that opens a file, that will be given to you and you will read each record. Each record is for an employee and contains First name, Last Name hours worked and hourly wage. Example; John Smith 40.3 13.78 the 40.3 is the hours worked. the 13.78 is the hourly rate. Details: the name of the file is EmployeeNameTime.txt Calculate the gross pay. If over 40 hours in the week then give them time and a...
Write a C ++ program which opens a file and reads several numbers, utilizing the fscanf()...
Write a C ++ program which opens a file and reads several numbers, utilizing the fscanf() function. Can you add few comments with explanations what is going on?
For C++ Write a program that opens a specified text file then displays a list of...
For C++ Write a program that opens a specified text file then displays a list of all the unique words found in the file. Hint: Store each word as an element of a set.
1. Write a public Java class called WriteFile which opens an empty file called letters.dat. This...
1. Write a public Java class called WriteFile which opens an empty file called letters.dat. This program will read a String array called letters and write each word onto a new line in the file. The method should include an appropriate throws clause and should be defined within a class called TFEditor. The string should contain the following words: {“how”, “now”, “brown”, “cow”}
Write a program that prompts the user to enter a file name, then opens the file...
Write a program that prompts the user to enter a file name, then opens the file in text mode and reads names. The file contains one name on each line. The program then compares each name with the name that is at the end of the file in a symmetrical position. For example if the file contains 10 names, the name #1 is compared with name #10, name #2 is compared with name #9, and so on. If you find...
Write a C++ program that creates a file called Readings.txt. Inside the file, your program must...
Write a C++ program that creates a file called Readings.txt. Inside the file, your program must create a list. The list is composed of integer double pairs. There is one pair per line. The integers are in sequence (0, 1, 2, 3, ...) beginning with zero and ending with some random value between 512 and 1024. The doubles should be random values between 50.000 and 90.000. The doubles only have 3 decimal places. The file should look like this (of...
Write a program that opens the file: "Lab6A_Data", reads all the values from the file, and...
Write a program that opens the file: "Lab6A_Data", reads all the values from the file, and calculates the following: A) The number of values in the file B) The sum of all the values in the file (a running total) C) The average of all the values in the file. D) The minimum value. E) The maximum value. F) The range of the data set of values. G) The number of times the value: '357' occurrs in the file. Display...
Write a program that reads a file called document.txt which is a text file containing an...
Write a program that reads a file called document.txt which is a text file containing an excerpt from a novel. Your program should print out every word in the file that contains a capital letter on a new line to the stdout. For example: assuming document.txt contains the text C++
Write a python program: There is a file called file 2. File2 is a txt file...
Write a python program: There is a file called file 2. File2 is a txt file and I have written the contents of file 2 below in the exact format it was in notepad. # This comment does not make sense # It is just to make it harder # The job description starts after this comment, notice that it has 4 lines. # This job description has 700150 hay system points\\ the incumbent will administer the spending of kindergarden...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT