!!! Please fix my original code, which is at the very bottom of the question. !!
ID: 3828816 • Letter: #
Question
!!! Please fix my original code, which is at the very bottom of the question. !!!
!!! Please fix my original code, which is at the very bottom of the question. !!!
!!! Please fix my original code, which is at the very bottom of the 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;
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 <iostream>
#include <string>
#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()
{
}
-----------------------------------------------------------------------------
-----------------------------------------------------------------------------
Below is my Class header (ClassWaterBType.h)
-----------------------------------------------------------------------------
#pragma once
#include "stdafx.h"
#include <iostream>
#include <string>
#include "ClassWaterBType.h"
class ClassWaterBType
{
private:
string AccountNo;
int AccountType;
string BillMonth;
float BillAmount;
public:
ClassWaterBType();
ClassWaterBType(int item, string accNo, int accType, string Mo, float amt);
~ClassWaterBType();
String CreateDataLine(int);
void SetBillData(int, string, int, string);
};
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:
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
Here are the fixed files for your code:
Class header (ClassWaterBType.h)
-------------------------------------------------------
#include <string>
using namespace std;
class ClassWaterBType
{
private:
string AccountNo;
int AccountType;
string BillMonth;
float BillAmount;
public:
ClassWaterBType();
~ClassWaterBType();
void SetBillData(string, int, string, float);
string CreateDataLine();
};
Class (ClassWaterBType.cpp)
---------------------------------------------
#include "stdlib.h"
#include <iostream>
#include <string>
#include <stdio.h>
#include "ClassWaterBType.h"
using namespace std;
ClassWaterBType::ClassWaterBType()
{
}
ClassWaterBType::~ClassWaterBType()
{
}
void ClassWaterBType::SetBillData(string accNo, int accType, string month, float amt)
{
AccountNo = accNo;
AccountType = accType;
BillMonth = month;
BillAmount = amt;
}
string ClassWaterBType::CreateDataLine()
{
string ReturnString;
char buf[9];
sprintf(buf, "%.2f",BillAmount);
ReturnString = "Account No= " + AccountNo + " ";
switch(AccountType)
{
case 1:
ReturnString += "Account Type Residential ";
break;
case 2:
ReturnString += "Account Type Commercial ";
break;
case 3:
ReturnString += "Account Type Industrial ";
break;
default:
ReturnString += "Account Type Residential ";
}
ReturnString += "Billing Month " + BillMonth + " "+
"Billing Amount $" + buf + " ";
return ReturnString;
}
Main program entry
--------------------------------
#include "stdlib.h"
#include <iostream>
#include <string>
#include "ClassWaterBType.h"
using namespace std;
int main()
{
ClassWaterBType bills[4];
bills[0].SetBillData("ZPK189834", 1, "May, 2017", 214.123);
bills[1].SetBillData("COM882485", 2, "May, 2017", 798.19);
bills[2].SetBillData("AIF157893", 3, "May, 2017", 12429.806);
bills[3].SetBillData("AUV456719", 3, "May, 2017", 9781.478);
cout << "Outstanding Water Bills ";
for (int i = 0; i < 4; i++)
{
cout << bills[i].CreateDataLine() << endl;
}
cout << "Press any key to exit the program""";
cin.get();
return 0;
}
Couple of points to be noted here:
----------------------------------------------------
Any function for initializing or retrieving object details must be decalred and defined inside the class, so moved the SetBillData, CreateDataLine functions inside the class.
Used case statement for converting int values of accountType to meaningful strings.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.