functions and how they interact with each other

Having trouble with my functions interacting with each other. Not sure how to have them link so it will communicate between themselves.

[code]
<
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <string>

using namespace std;
//fucntion prototype


void get_cust_ID(){


cout << "Enter the 3 didgit Customer ID" << endl;
string customerID;
cin >> customerID;
cout << endl;

}
void get_cust_kwh(){
double uc;


//Input the units consumed
cout << "Enter the KWH consumed by the consumer" << endl;
cin >> uc;


}
void calc_charges(double uc){

double amt;

if (uc >= 0 && uc <= 300)
amt = uc * .12;
else
{
if (uc > 300 && uc <= 600)
amt = ((300 * .12) + (uc - 300))*.09;
else

if (uc > 600 && uc <= 1000)
amt = (300 * .12 + 300 * .09) + (uc - 600) * .06;
else
amt = (300 * .12 + 300 * .09 + 400 * .06) + (uc - 1000)* .04;

}

}
void display_cust_data(double total,double amt,double uc,char response,string customerID){


cout << setw(20) << left << "Customer ID" << setw(10) << left << "KWHours" << setw(4) << left << "Charge($)" << endl;
cout << setw(20) << left << customerID << setw(10) << left << uc << setw(4) << left << "$" << amt << endl;
cout << endl;
cout << "Would you like to add a customer?(Y/N)" << endl;
cin >> response;
}


int main()
{

get_cust_ID();
get_cust_kwh();
calc_charges( );
display_cust_data( );



return 0;
} >
[end code]
Last edited on
You should think about functions in math. For example:
 
    y = sin(x);

That's fairly well understood. y is assigned the value sin of x, where x is an angle.

You don't really care how sin is calculated, you just use it, right?

Let's take a lookt at one of yours.
 
    get_cust_ID();

Where is the customer id that the function gets? See the problem?

Your code should read more like:
1
2
3
4
    customer_id = get_cust_ID();
    power_usage = get_cust_kwh(customer_id);
    cost = calc_charges(power_usage)l
    display(customer_id, power_usage, cost);


Got it?
Hi cereal8282, would you mind putting code tags around your code:

[ code] my code [ /code] ( but without the spaces after the '[' )

But it looks like you need to return values from your functions:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int getInt( void )
{
  int i;
  cout << "please enter an integer: ";
  cin >> i;
  cin.ignore( 80, '\n' );
  return i;
}

void printInt( int value )
{
  cout << "The value is: " << value << '\n';
}

int main( void )
{
  int value = getInt();
  printInt( value );
  return 0;
}
Last edited on
Hey cereal8282!
LowestOne is correct where you need return values for your functions.
The problem with void functions is they have no return values so anything you do inside that function stays in that function.

Also, once you work on the return values you also should look at where you're variables are located. For example in your second function, the get_cust_kwh();, you are initializing the variable "uc" and then wanting to use that in your third function, calc_charges( );.
However you didnt return uc from your second function so its now stuck in there until you tell the computer to drag it out to use later. The same applies for "amt".

I dont like to fix people's actual code, just try and explain what they should work on fixing however LowestOne's example shows both of my points where he is using an int function and returning something from it
So I have spent all last night getting this far. Is there something that I am missing? it wont compile.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
>






#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <string>

using namespace std;
//fucntion prototype
string get_cust_ID();
double get_cust_kwh();
double calc_charges(double uc);
void display_cust_data(double total, double amt, double uc, char response, string customerID);
void display_final_data();
int main()
{
	double  totUC = 0.0, totAMT = 0.0, totCust = 0.0;
	
	int  countUC, countAMT, countCust;
	string custID;
	double uc, amt, total,sum,cust;
	char response;
	
	cout << fixed << setprecision(2);
	countCust = 0;


	cout << "Would you like to add a customer?" << endl;
	cin >> response;

	
	
	while (response == 'Y' || response == 'y');

	{
		custID = get_cust_ID();
		totCust += cust;
		++countCust;


		uc = get_cust_kwh();
		totUC += total;
		++countUC;

		calc_charges(uc);
		totAMT += sum;
		++countAMT;

		display_cust_data(total, amt, uc, response, custID);

	}
	
	system("CLS");
	
	display_final_data(totAMT, totCust,totUC);


	return 0;
}

string get_cust_ID(){
	string custID;

		cout << "Enter 3 digit ID: ";
	    cin >> custID;
	
		return custID;
}
double get_cust_kwh(){
	double uc;


	//Input the units consumed
	cout << "Enter the KWH consumed by the consumer" << endl;
	cin >> uc;
	
	return uc;
}
double calc_charges(double uc){

	double amt;

	if (uc >= 0 && uc <= 300)
		amt = uc * .12;
	else
	{
		if (uc > 300 && uc <= 600)
			amt = ((300 * .12) + (uc - 300))*.09;
		else

		if (uc > 600 && uc <= 1000)
			amt = (300 * .12 + 300 * .09) + (uc - 600) * .06;
		else
			amt = (300 * .12 + 300 * .09 + 400 * .06) + (uc - 1000)* .04;

	}
	return amt;
}
void display_cust_data(double total,double amt,double uc,char response,string customerID){
		

	cout << setw(20) << left << "Customer ID" << setw(10) << left << "KWHours" << setw(4) << left << "Charge($)" << endl;
	cout << setw(20) << left << customerID << setw(10) << left << uc << setw(4) << left << "$" << amt << endl;
	cout << endl;
	cout << "Would you like to add a customer?(Y/N)" << endl;
	cin >> response;
}

void display_final_data(double totCust, double totUC, double totAMT){

	double average = 0.00;
	double totCust,totUC,totAMT;
	average = totAMT / totCust;

	//print the bill
	cout << setw(6) << right << "CUSTOMER READ OUT" << endl;
	cout << "---------------------------" << endl;
	cout << setw(6) << left << " Customer Count:" << endl;
	cout << setw(6) << left << totCust << endl;
	cout << endl;
	cout << setw(6) << left << "Total KWHours" << endl;
	cout << setw(6) << left << totUC << endl;
	cout << endl;
	cout << setw(6) << left << "Total Charges" << endl;
	cout << setw(5) << left << "$" << setw(6) << left << totAMT << endl;
	cout << "---------------------------" << endl;
	cout << setw(6) << left << "Average" << endl;
	cout << setw(6) << left << average << endl;

}


<
it wont compile.

Thank you for giving us no details whatsoever about what errors you're getting. This is so much more fun when we have to play guessing games.
Line 12: You're declaring that display_final_data takes no arguments.
Line 53: You're calling display_final_data with three arguments.
Your function prototype and call must agree in number and type of arguments.

Line 110: You're redeclaring totCust, totUC, and totAMT. You've already declared these as arguments. It's an error to declare them again.

You also have a number of warnings that indicate other problems in your program.

Line 35: cust is unititialized. You're going to be adding garbage.
Line 40: ditto total.
Line 41: ditto countUC.
Line 44: ditto sum.
Line 45: ditto countAMT.

Learn to understand your compiler's error message. THe compiler is trying to tell you what is wrong. If you don't understand a compiler error message, post the EXACT text of the error.


Line 110: You're redeclaring totCust, totUC, and totAMT. You've already declared these as arguments. It's an error to declare them again.

Just to expand on this: it's legal C++ to do this, but it's almost certainly a mistake. What it means is that you create local variables, that hide the arguments. Whenever you read, or set, the values of these variables, you are not reading/setting the values of the arguments to the function. You're reading/setting the local variables instead.


Edit: What I wrote above appears to be incorrect. My apologies.
Last edited on
MikeyBoy wrote:
it's legal C++ to do this

Unless this changed in C++14, Visual Studio flags this as an error, not a warning. You can redeclare them in a nested scope with the results you describe, but you can't redeclare them at function scope.
AbstractionAnon wrote:
Unless this changed in C++14, Visual Studio flags this as an error, not a warning. You can redeclare them in a nested scope with the results you describe, but you can't redeclare them at function scope.


Hmm... I just tested this in VS 2010 and you're right. Sorry for the misinformation!
Topic archived. No new replies allowed.