Losing my mind with Functions!

I have been trying to figure out functions for a while now, and I have been working with a specific program for a week, and have been unable to figure out what I am doing wrong.

Here is the code:

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

void GetTheWeight(double & Weight);
void GetTheKarats(double & Karats);
void CalculateTheValue(double Weight, double Karats);

const double GramsPerOunce = 28.35;
const double GramsPerTroyOunces = 31.1; 
const double CurrentValuePerTroyOunceGold = 1744.50;
const double FullKarat = 24;

int main ()
{

	double Weight;
	double Karats;
	double TroyOunces;
	double Grams;
	double FullKarats;
	double AdjustedValue;

	GetTheWeight(Weight);
	GetTheKarats(Karats);
	CalculateTheValue(Weight, Karats);

	cout << "Your "<< Weight << " ounces of Gold with a Karat Quality of " << Karats << " is $" << AdjustedValue << endl  << endl;

  return(0);
}

void GetTheWeight (double & Weight)
{
	

	cout << "Please enter your weight of Gold in ounces => ";
	cin >> Weight;
	cout << endl << endl;
}

void GetTheKarats(double & Karats)
{
  cout << "Please enter the Quality of gold in Karats => ";
  cin >> Karats;
  cout << endl << endl;
}

void CalculateTheValue(double Weight, double Karats)
{
	double Grams;
	double TroyOunces;
	double FullValue;
	double AdjustedValue;
	
	Grams = Weight * GramsPerOunce;	 

	TroyOunces = Grams / GramsPerTroyOunces;

	FullValue = TroyOunces * CurrentValuePerTroyOunceGold;

	AdjustedValue = FullValue * Karats / FullKarat;
	
}


========================================

This is the Error I receive while trying to build.

========================================
1>------ Build started: Project: functions, Configuration: Debug Win32 ------

(20): warning C4101: 'FullKarats' : unreferenced local variable
(19): warning C4101: 'Grams' : unreferenced local variable
(18): warning C4101: 'TroyOunces' : unreferenced local variable
(27): warning C4700: uninitialized local variable 'AdjustedValue' used
functions\Debug\functions.exe
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
=========================================


I am at my wits end, any help would be greatly appreciated.
Last edited on
edit your post and put the code in codetags. it's the "<>" button on the right
you don't get errors you only have warnings, your code will still get executed.

your problems are:
- lines 18-21: you set up the variables TroyOunces, Grams, FullKarats and AdjustedValue, but they never get a value. That's no problem, you just get warnings.

- line 27: you are printing AdjustedValue, it still has "no" value.

- lines 48-63 this function does nothing.
Last edited on
DarkmasterĀ“s right, your CalculatetheValue function does not return anithing and does not modify any variable as reference (as you did on GetTheWeight, for example, by using &).

It should return AdjustedValue, so it should not be declared as void, but as double.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
double CalculateTheValue(double Weight, double Karats)
{
	double Grams;
	double TroyOunces;
	double FullValue;
	double AdjustedValue;
	
	Grams = Weight * GramsPerOunce;	 

	TroyOunces = Grams / GramsPerTroyOunces;

	FullValue = TroyOunces * CurrentValuePerTroyOunceGold;

	AdjustedValue = FullValue * Karats / FullKarat;

        Return (AdjustedValue);
	
}


in order to use that value in the main function, line 25 could state:

 
AdjustedValue = CalculateTheValue(Weight, Karats);


And now, line 27 makes more sense. Gook luck!
Thank you very much to both of you. You have been Great Help!!!
Topic archived. No new replies allowed.