3 Replies - 1508 Views - Last Post: 07 August 2012 - 02:31 PM Rate Topic: -----

#1 JohnnyCab  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 07-August 12

How to return a total in a function

Posted 07 August 2012 - 02:03 PM

I am having trouble getting my final variable (tot_charge) to keep the total charges stored for a final calculation. I have tried the things I can think of, but need help from someone with more experience. Thank you in advance.

#include <iostream>		//Include needed system files
#include <string>		//Include strings
#include <iomanip>		//Allow setprecision
using namespace std;	//Replaces using std:cout, etc.



string get_cust_ID(string);
int get_cust_kwh(int);
double calc_charge(int);
void display_cust_data(string, int, double);
void display_final_report(int, int, double);


int main()
{
	int x =0;
	int kwh = 0;
	int numcusts = 0;
	int totalkwh = 0;
	int z = 0;
	string custnum = "000";
	string y = "123";
	double charges = 0.0;
	double tot_charges = 0;
	
	cout << "Welcome to the Electrical Pricing Calculator v2.0! \n";
	
	do
	{
	 cout << "Choose one of the following: \n";
	 cout << "1. Enter new customer data \n";
	 cout << "2. Show summary of all customer data \n";
	 cout << "3. Quit program \n";
	 cout << "Please make your selection: ";
	 cin >> x;

	 if (x==1)
	 {
		 numcusts ++;
		 custnum = get_cust_ID(y);
		 kwh = get_cust_kwh(z);
		 charges = calc_charge(kwh);
		 tot_charges = charges + charges;
		 display_cust_data(custnum, kwh, charges);
		 totalkwh = totalkwh+kwh;
	 }
	 if (x==2)
	 {
		 display_final_report(numcusts, totalkwh, tot_charges);
	 }

	}while (x != 3);
	 system ("pause");
	 return 0;
}

string get_cust_ID(string num1)
{
	
	cout << "Please enter the customer number: ";
	cin >> num1;
	return num1;
}

int get_cust_kwh(int num1)
{
	

	cout << "Please enter the total kilowatt hours used for the month: ";
	cin >> num1;

	return num1;
}

double calc_charge(int num1)
{
	double total = 0;
	double w = 0;
	double x = 0;
	double y = 0;
	double z = 0;

	if (num1 <= 300)
	{
		w=num1*.12;
	}
	else if (num1 <= 600 && num1 > 300)
	{
		x=w+(num1-300)*.09;
	}
	else if (num1 <= 1000 && num1 > 600)
	{
		y=w+x+(num1-600)*.06;
	}
	else if (num1 > 1000)
	{
		z=w+x+y+(num1-1000)*.04;
	}
	total=(w+x+y+z);

	return total;
}

void display_cust_data(string num1, int num2, double num3)
{
	cout << endl;
	cout << endl;
	cout << endl;
	cout << fixed;
	cout << right << "CUSTOMER ID" << "	KILOWATT HOURS" << "		CHARGE ($)" << endl;
	cout << right << "        " << num1 << "	          " << setprecision(0) << num2 << "		     " << setprecision(2) << num3 << endl;
	cout << endl;
	cout << endl;
	cout << endl;
}

void display_final_report(int num1, int num2, double num3)
{
	if(num1 == 0) return;
		
	int avgkwh = 0;
	double tot_charge = 0.0;
	avgkwh = num2/num1;
	tot_charge = num3 + tot_charge;

	system ("CLS");
	cout << endl;
	cout << endl;
	cout << endl;
	cout << fixed;
	cout << right << "Total # of Customers:" << "	Total KWH:" << "	Avg KWH:" << "	Total Charges ($):" << endl;
	cout << "--------------------------------------------------------------------------" << endl;
	cout << right << "                    " << num1 << "	      " << num2 << "	    " << avgkwh << "		     " << setprecision(2) << tot_charge << endl;
	cout << endl;
	cout << endl;
	cout << endl;
}


Is This A Good Question/Topic? 0
  • +

Replies To: How to return a total in a function

#2 duffman18  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 54
  • Joined: 20-October 10

Re: How to return a total in a function

Posted 07 August 2012 - 02:19 PM

I think you want to do something like the following:
tot_charges = tot_charges + charges;

in stead of:
tot_charges = charges + charges;

What you have right now will just keep replacing your previous value of tot_charges with 2*charges.
Was This Post Helpful? 2
  • +
  • -

#3 jimblumberg  Icon User is online

  • member icon

Reputation: 3058
  • View blog
  • Posts: 9,304
  • Joined: 25-December 09

Re: How to return a total in a function

Posted 07 August 2012 - 02:20 PM

It looks like you are just storing double the last charge, not totaling anything. You seem to know how to compute the totalkwh, wouldn't the total charges be similar?

Jim
Was This Post Helpful? 1
  • +
  • -

#4 JohnnyCab  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 07-August 12

Re: How to return a total in a function

Posted 07 August 2012 - 02:31 PM

Thanks! I looked right over that one!

-John
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1