!!! Please fix my original code, which is at the very bottom of the question. !!
ID: 3828805 • 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
//ClassWaterBType.h
#pragma once
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
class ClassWaterBType
{
private:
string AccountNo;
int AccountType;
string BillMonth;
float BillAmount;
public:
ClassWaterBType();
ClassWaterBType(string accNo, int accType, string Mo, float amt);
~ClassWaterBType();
string CreateDataLine();
void SetBillData(string accNo, int accType, string Mo, float amt);
};
//ClassWaterBType.cpp
#include "stdafx.h"
#include <iostream>
#include <string>
#include "ClassWaterBType.h"
using namespace std;
//default constructor
ClassWaterBType::ClassWaterBType()
{
AccountNo = ""; //empty string
AccountType = 0;
BillMonth = "";
BillAmount = 0;
}
//Constructor with parameters
ClassWaterBType::ClassWaterBType(string accNo, int accType, string Mo, float amt)
{
AccountNo = accNo;
AccountType = accType;
BillMonth = Mo;
BillAmount = amt;
}
//destructor
ClassWaterBType::~ClassWaterBType()
{
}
//Sets the bill for a particular user/accno
void ClassWaterBType::SetBillData(string accNo, int accType, string Mo, float amt)
{
AccountNo = accNo;
AccountType = accType;
BillMonth = Mo;
BillAmount = amt;
}
//Creates display format for bill
string ClassWaterBType::CreateDataLine()
{
string ReturnString;
char buf[9];
sprintf_s(buf, "%.2f", BillAmount);
ReturnString =
"Account No. " + AccountNo + " " +
"Account Type " + to_string(AccountType) + " (1 = Residential, 2 = Commercial, 3 = Industrial) " +
"Billing Month " + BillMonth + " " +
"Billing Amount $" + buf + " ";
return ReturnString;
}
//main.cpp
// WaterType.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <string>
#include "ClassWaterBType.h"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
ClassWaterBType WaterBill[4]; //creating a variable to store 4 bills
//adding bill data to each variable
WaterBill[0].SetBillData("ZPK189834", 1, "May, 2017", 214.12);
WaterBill[1].SetBillData("COM882485", 2, "May, 2017", 798.19);
WaterBill[2].SetBillData("AIF157893", 3, "May, 2017", 12429.80);
WaterBill[3].SetBillData("AUV456719", 3, "May, 2017", 9781.48);
cout << "Outstanding Water Bills ";
for (int i = 0; i < 4; i++) //printing the bill for each user
cout << WaterBill[i].CreateDataLine() << endl;
cout << endl;
system("pause");
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.