Hello, I need help with my C++ program assignment, it\'s based on the book, C++
ID: 3828561 • Letter: H
Question
Hello, I need help with my C++ program assignment, it's based on the book, C++ Programing 7th Edition.
Assignment
The existing program keeps track of billing information in an array of variables derived from a struct. Convert the program so that it uses a class instead. Leave no sign that there was a struct in the program behind.
Your program must contain the following attributes:
Choose the default application settings.
-----------------------------------------------------------------------------
-----------------------------------------------------------------------------
Below is the code for my Main program entry
-----------------------------------------------------------------------------
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
struct WaterBillType
{
string AccountNo;
int AccountType;
string BillMonth;
float BillAmount;
};
WaterBillType WaterBill[4];
void SetBillData(int item, string accNo, int accType, string Mo, float amt)
{
WaterBill[item].AccountNo = accNo;
WaterBill[item].AccountType = accType;
WaterBill[item].BillMonth = Mo;
WaterBill[item].BillAmount = amt;
}
string CreateDataLine(int item)
{
string ReturnString;
char buf[9];
sprintf_s(buf, "%.2f", WaterBill[item].BillAmount);
ReturnString =
"Account No. " + WaterBill[item].AccountNo + " " +
"Account Type " + to_string(WaterBill[item].AccountType) + " (1 = Residential, 2 = Commercial, 3 = Industrial) " +
"Billing Month " + WaterBill[item].BillMonth + " " +
"Billing Amount $" + buf + " ";
return ReturnString;
}
int _tmain(int argc, _TCHAR* argv[])
{
SetBillData(0, "ZPK189834", 1, "May, 2017", 214.12);
SetBillData(1, "COM882485", 2, "May, 2017", 798.19);
SetBillData(2, "AIF157893", 3, "May, 2017", 12429.80);
SetBillData(3, "AUV456719", 3, "May, 2017", 9781.48);
cout << "Outstanding Water Bills ";
for (int i = 0; i < 4; i++)
cout << CreateDataLine(i) << endl;
cout << endl;
system("pause");
return 0;
}
-----------------------------------------------------------------------------
-----------------------------------------------------------------------------
Below is my Class (ClassWaterBType.cpp)
-----------------------------------------------------------------------------
#include "stdafx.h"
#include "ClassWaterBType.h"
ClassWaterBType::ClassWaterBType()
{
}
ClassWaterBType::~ClassWaterBType()
{
}
-----------------------------------------------------------------------------
-----------------------------------------------------------------------------
Below is my Class header (ClassWaterBType.h)
-----------------------------------------------------------------------------
#pragma once
class ClassWaterBType
{
public:
ClassWaterBType();
~ClassWaterBType();
};
Convert an existing program that uses a struct for data storage to use a class for data storage. Purpose Learn how to add a class to a program.
Create an array of objects.
Pass data to, and retrieve data from, an object. Instructions Using Microsoft Visual Studio create a C++ program that solves the following problem.
1. Read the problem.
Modify an existing program by changing its use of a struct to a class.
The existing program keeps track of billing information in an array of variables derived from a struct. Convert the program so that it uses a class instead. Leave no sign that there was a struct in the program behind.
Your program must contain the following attributes:
2. Create a new Visual C++ Win32 Console application and name it:
Choose the default application settings.
Write code that solves the problem described in item 1.
Pause the program so that the console does not disappear until a key is pressed.
4. Insure that you properly document your code.
Explanation / Answer
Ans
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
WaterBillType WaterBill[4];
void SetBillData(int item, string accNo, int accType, string Mo, float amt)
{
WaterBill[item].AccountNo = accNo;
WaterBill[item].AccountType = accType;
WaterBill[item].BillMonth = Mo;
WaterBill[item].BillAmount = amt;
}
string CreateDataLine(int item)
{
string ReturnString;
char buf[9];
sprintf_s(buf, "%.2f", WaterBill[item].BillAmount);
ReturnString =
"Account No. " + WaterBill[item].AccountNo + " " +
"Account Type " + to_string(WaterBill[item].AccountType) + " (1 = Residential, 2 = Commercial, 3 = Industrial) " +
"Billing Month " + WaterBill[item].BillMonth + " " +
"Billing Amount $" + buf + " ";
return ReturnString;
}
int _tmain(int argc, _TCHAR* argv[])
{
SetBillData(0, "ZPK189834", 1, "May, 2017", 214.12);
SetBillData(1, "COM882485", 2, "May, 2017", 798.19);
SetBillData(2, "AIF157893", 3, "May, 2017", 12429.80);
SetBillData(3, "AUV456719", 3, "May, 2017", 9781.48);
cout << "Outstanding Water Bills ";
for (int i = 0; i < 4; i++)
cout << CreateDataLine(i) << endl;
cout << endl;
system("pause");
return 0;
}
#include "stdafx.h"
#include "ClassWaterBType.h"
ClassWaterBType::ClassWaterBType(int item, string accNo, int accType, string Mo, float amt)
{
WaterBill.AccountNo = 0;
WaterBill.AccountType = NULL;
WaterBill.BillMonth = 0;
WaterBill.BillAmount = NULL;
}
ClassWaterBType::~ClassWaterBType()
{
}
#pragma once
class ClassWaterBType
{
private :
string AccountNo;
int AccountType;
string BillMonth;
float BillAmount;
public:
ClassWaterBType();
~ClassWaterBType();
String CreateDataLine(int);
void SetBillData(int,string,int,string);
};
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.