debug error

the pennies variable is being used without being initialized. I cant see the error.
I'm getting an error

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
  #include <iostream>
#include <iomanip>
using namespace std;

int input_coins (int,int,int,int);
void Totamt( double&,double&,double&,double&,double);
void display(double);

int main()


{
	int quaters,dimes,nickles,pennies;
	double quaters1,dimes1,nickles1,pennies1,total;

	input_coins(quaters,dimes,nickles,pennies);

	Totamt(quaters1,dimes1,nickles1,pennies1,total);

	display (total);


	return 0;

}


int input_coins(int quaters,int dimes,int nickles,int pennies)

{
	cout<<"Please enter total quaters"<<endl;
	cin>>quaters;
	cout<<"Please enter total dimes"<<endl;
	cin>>dimes;
	cout<<"Please enter total nickles"<<endl;
	cin>>nickles;
	cout<<"Please enter total pennies"<<endl;
	cin>>pennies;

	return quaters,dimes,nickles,pennies;

}

void Totamt(double & quaters, double & dimes, double & nickles, double & pennies,double total)

{
	total=(quaters*.25)+(dimes*.10)+(nickles*05)+(pennies*.01);
		
}

void display(double total)

{
	cout<<setprecision(10)<<setw(8)<<endl;

	cout<<"Your total amount in your piggybank is: "<<total<<endl;

}
Your original error means exactly that. You declared a bunch of variables but you didn't give them any initial values. In some cases, they're just initialized to some garbage value. You should set them to zero when you declare them.

2nd: your line 40 makes no sense. You can't return four values from a function like that.

Also, you're missing a decimal your value of a nickel when you're calculating in Totamt(). Should be:
total=(quaters*.25)+(dimes*.10)+(nickles*.05)+(pennies*.01);

Finally, you have your syntax completely backwards for pass-by-value and pass-by reference. Clearly, the intent of your input_coins() function is to get amounts of each coin, so you should be passing variables by reference and returning void:
void input_coins(int& quaters, int& dimes, int& nickles, int& pennies)

On the other hand, your Totamt() function is calculating a single total, so you just need to pass in your coin values by value and return a single double value:
double Totamt(int quarters, int dimes, int nickles, int pennies).
Last edited on
Hi Tanya,

The error comes from calling a function with an uninitialised variable. The way out is to try to always initialise your variables with some value that will help point to errors, but not contribute to producing problems. zero i s not always a good choice. I always declare & initialise & comment each variable on it's own line.

Also note that you can only return 1 value from a function (line 40).

If you have compile errors, it is a good idea to post them here.

Line 3 is should be avoided as well, google for the reasons why - there has been plenty written about this.

Try not to abbreviate function & variable names too much. Totamt is fine for this program, but for larger amounts of code I would prefer something like TotalAmount. There are various conventions for naming things - try googling the Google Coding Standard.

http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml


There is a difference between compiling & debugging. A debugger is a program that allows one to step through a program 1 line at a time while watching the values of all the variables to see where things go wrong. This is very different to looking at compiler errors.

Hope all goes well :+)

EDIT:

With Floating Point numbers like floats & doubles, I always fully qualify them with numbers before & after the decimal point, as in 0.05 not .05
Last edited on
input_coins() takes integer values as arguments, which means that takes the actual arguments (such as the "dimes" in main()) then creates duplicates which are used in input_coins() (also labelled "dimes"). The "dimes" in main() is different than the "dimes" in input_coins() (the input_coins() "dimes" ceases to exist after input_coins() is finished). Therefore nothing is input into the main() "dimes" and thus "dimes" was never initialized. You need to pass by pointer or reference, and no return value is needed for what you're doing.

Also return quaters,dimes,nickles,pennies; doesn't mean what you probably think it does. The compiler views what you wrote like this:
return (quaters,(dimes,(nickles,pennies)))
First nickles,pennies evaluates to nickles
so (dimes,(nickles,pennies))
is the same as dimes,nickles
which evaluates to dimes which means
(quaters,(dimes,(nickles,pennies)))
is the same as quaters,dimes
which evaluates to quaters, which is the value returned (the "quaters" variable local to input_coins() disappears after it is returned)
Topic archived. No new replies allowed.