Calling Function Issues! Need Help ASAP Please!

Allrigghty, Here's my problem and my code

Have to calculate two returning values: The fat percentage and the Fat calories in a food item
My only inputs are going to be the fat grams and the total calories.

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
#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;

//function prototype
double getFatCals(int, double, int);
double getFatPercent(int, double, int);

int main()
{
	//declare variables
		int totalCals = 0;
		int fatGrams = 0;
		int fatCals = 0;
		double fatPercent = 0.0;

	//get input items
	cout << "Total Calories? ";
	cin >> totalCals;
	cout << "Grams of fat? ";
	cin >> fatGrams;
	

	//call function to Fat calories and fat Percentage
	fatCals = getFatCals(fatGrams * 9);
	fatPercent = getFatPercent(static_cast<double>(fatCals)/ static_cast<double>(totalCals) * 100);

	//display 
cout << fixed << setprecision(2) << endl;
cout << "Fat Calories: " << fatCals << endl;
cout << "Fat Percent: " << fatPercent << endl;

system("pause");
return 0;
} //end of main function

//*****function definitions*****
int getFatCals(int fatGram)
{
	//calculates and returns fat grams
	int getFatCals = 0.0;
	getFatCals = fatGram * 9;
	return getFatCals;
} //end of getFatCals function

double getFatPercent(int fatCal, int totalCal)
{
	//calculates and returns fat percent
	double getFatPercent = 0.0;
	getFatPercent = static_cast<double>(fatCal)/ static_cast<double>(totalCal) * 100;
	return getFatPercent;
} //end of getFatPercent function 


I'm getting a red line underneath getFatPercent on line 28
It says "Error: no instance of overloaded function 'getFatPercent' matches the arguement list"

Also since I haven't been able to run it yet, if you see any errors please let me know!

Thanks to everyone who replies.
Your function prototypes need to match your function definitions.
This: double getFatCals(int, double, int); doesn't match this: int getFatCals(int fatGram)
The function names are the same, but the parameter lists aren't.
Last edited on
Alright fixed that and now I don't have any errors shown but the build is failing.

Here's my updated 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
#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;

//function prototype
int getFatCals(int);
double getFatPercent(int, int);

int main()
{
	//declare variables
		int totalCals = 0;
		int fatGrams = 0;
		int fatCals = 0;
		double fatPercent = 0.0;

	//get input items
	cout << "Total Calories? ";
	cin >> totalCals;
	cout << "Grams of fat? ";
	cin >> fatGrams;
	

	//call function to Fat calories and fat Percentage
	fatCals = getFatCals(fatGrams * 9);
	fatPercent = getFatPercent(static_cast<double>(fatCals)/ static_cast<double>(totalCals) * 100);

	//display 
cout << fixed << setprecision(2) << endl;
cout << "Fat Calories: " << fatCals << endl;
cout << "Fat Percent: " << fatPercent << endl;

system("pause");
return 0;
} //end of main function

//*****function definitions*****
int getFatCals(int fatGram)
{
	//calculates and returns fat grams
	int getFatCals = 0.0;
	getFatCals = fatGram * 9;
	return getFatCals;
} //end of getFatCals function

double getFatPercent(int fatCal, int totalCal)
{
	//calculates and returns fat percent
	double getFatPercent = 0.0;
	getFatPercent = static_cast<double>(fatCal)/ static_cast<double>(totalCal) * 100;
	return getFatPercent;
} //end of getFatPercent function 


and here's whats in the build log.

1>------ Build started: Project: Example , Configuration: Debug Win32 ------
1>Build started 4/7/2013 10:14:58 PM.
1>InitializeBuildStatus:
1> Touching "Debug\example.unsuccessfulbuild".
1>ClCompile:
1> Example.cpp
1>c:location\Example.cpp(32): error C2660: 'getFatPercent' : function does not take 1 arguments
1>c:\location\Example.cpp(47): warning C4244: 'initializing' : conversion from 'double' to 'int', possible loss of data
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:00.77
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Your call to getFatPercent is wrong. The compiler is telling you that it does not take 1 argument. You called it and only passed one argument, but you declared it and defined it to take 2 arguments. I think you mean for line 28 to say:
fatPercent = getFatPercent(fatCals, totalCals);
All that business with the casting and the dividing already takes place inside the function, so there's no need to do it in main where you call the function.
okay Fixed that and it's displaying this

.cpp(31): error C3861: 'getFatCals': identifier not found

Here's updated code as well:

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
#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;

//function prototype
//int getFatCals(int);
double getFatPercent(int, int);

int main()
{
	//declare variables
		int totalCals = 0;
		int fatGrams = 0;
		int fatCals = 0;
		double fatPercent = 0.0;

	//get input items
	cout << "Total Calories? ";
	cin >> totalCals;
	cout << "Grams of fat? ";
	cin >> fatGrams;
	

	//call function to Fat calories and fat Percentage
	fatCals = getFatCals(fatGrams);
	fatPercent = getFatPercent(fatCals, totalCals);

	//display 
cout << fixed << setprecision(2) << endl;
cout << "Fat Calories: " << fatCals << endl;
cout << "Fat Percent: " << fatPercent << endl;

system("pause");
return 0;
} //end of main function

//*****function definitions*****
int getFatCals(int fatGram)
{
	//calculates and returns fat grams
	int getFatCals = 0;
	getFatCals = fatGram * 9;
	return getFatCals;
} //end of getFatCals function

double getFatPercent(int fatCal, int totalCal)
{
	//calculates and returns fat percent
	double getFatPercent = 0.0;
	getFatPercent = static_cast<double>(fatCal)/ static_cast<double>(totalCal) * 100;
	return getFatPercent;
} //end of getFatPercent function 
Last edited on
You commented out the function prototype for getFatCals(int);
Oh wow nevermind. For some reason it had put the prototype for it as a comment. sillyme.jpeg

NOW ITS RUNNING :D
Topic archived. No new replies allowed.