Strings, File I/O 1. Check. Create a project titled Lab6_Check. Write a program
ID: 3604749 • Letter: S
Question
Strings, File I/O 1. Check. Create a project titled Lab6_Check. Write a program that asks the user the check information and prints the check. The dialog should be as follows: date: 2/13/2014 name: William Schmidt amount, dollars: 23 cents: 3 payee: Office Max your check: William Schmidt pay to: Office Max twenty three and 3/100 2/13/2014 $23.03 dollars You may assume that a person always has the first name and last name (no middle names or initials). The payee name is also always two words. The dollar and cent amount are integers and the amount is always less than 100 dollars. Note that the dollar amount could be zero, in which case, when you spell the dollar amount, it should print "zero". The date is always a single (non-white space separated string). Your date, dollar amount in numbers and the word "dollars" have to vertically align.Explanation / Answer
#include<iostream>
#include<fstream>
#include<string>
#include<iomanip>
#include<sstream>
using namespace std;
char dollars[29][10] = { "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "ninteen", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety", "hundred" };
int main()
{
//inpout stream handler for reading file
ifstream in;
//output file handler for writing to file
ofstream out;
in.open("Checkdatabase.txt");
//chk if input file is open
if (!in)
{
cout << "Not able to open input file" << endl;
return -1;
}
//open output file
out.open("check.txt");
//chk output file is open for writing
if (!out)
{
cout << "Not able to open output file" << endl;
return -1;
}
string str, fullName,payee,date,doll,cen;
std::string::size_type dollar, cents;
while (!in.eof())
{
in >> str>>date;
in >> str;
std::getline(in, fullName);
in >> str;
in >> str>>doll;
in >> str;
in >> cen;
in >> str;
std::getline(in, payee);
stringstream intDoller(doll),intCent(cen);
intDoller >> dollar;
intCent >> cents;
//write to file
/*out <<setw(30)<<fullName << " " << left<<date << endl;
out <<setw(30) << "pay to: " << payee << " "<<left << "$" << dollar << "." << cents << endl;
cout << setw(20) << fullName << " " << left << date << endl;
cout << setw(30) << "pay to: " << payee << " " << left << "$" << dollar << "." << cents << endl;*/
//remove leading space in string
out <<left<<setw(30)<<fullName<< " " << right<<setw(20) << date << endl;
out << " pay to: " << payee << " " << right << setw(20) << "$" << dollar << "." << cents << endl;
int rem;
do
{
rem = dollar % 10;
dollar = dollar / 10;
out << dollars[dollar * 10-1]<<" " ;
} while (dollar > 10);
out << dollars[rem - 1] << " and " << cents << "/" << "100" << setw(20) << " " << "dollars" << endl;
}
}