Code help?

Not sure why but im getting a error saying "typ "int" unexpected" i tried setting gmsc to int dosent change anythintg. aswell as all my variables being "undeclared" even if I mention them before int main. I'm horrible at coding and i really feel like just dropping out help please

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

double Calculate(int pts, int fg, int fga, int fta, int ft, int orb, int drb, int stl, int ast, int blk, int pf, int tov, float gmsc) {
	double total;
		total = pts + 0.4 * fg - 0.7 * fga - 0.4 * fta - ft + 0.7 * orb + 0.3 * drb + stl + 0.7 * ast + 0.7 * blk - 0.4 * pf - tov;

		return total;
}
int main()
{
	double calculate;
	double gmsc, int pts, int fg, int fga, int fta, int ft, int orb, int drb, int stl, int ast, int blk, int pf, int tov;
	cout << "Enter points";
	cin >> pts ;
	cin >> fg  ;
	cin >> fga ;
	cin >> fta ;
	cin >> ft  ;
	cin >> orb ;
	cin >> drb ;
	cin >> stl ;
	cin >> ast ;
	cin >> blk ;
	cin >> pf  ;
	cin >> tov ;
	gmsc = calculate (pts, fg, fga, fta, ft, orb, drb, stl, ast, blk, pf, tov);
	cout << gmsc;
return 0;
}
 
You can't do
double gmsc, int pts, ...
You have to split up the lines (by type).
1
2
double gmsc;
int pts, fg; // etc  


Second, C++ is case-sensitive. "Calculate" is not "calculate".

Third, you are vacillating between the use of floats and doubles. Prefer double by default.

Fourth, passing, uh...

counts

13 parameters is a bit excessive. Consider grouping common attributes in a struct with a name that lets the reader know how all these variables relate. Also, having too many abbreviations makes code hard to read.

Fifth, as a consequence of the fourth point, you have made an error and you're passing "only" 12 parameters into your function. Which is actually correct. You have an extra, unnecessary "float gmsc" parameter in your function you should remove.

Sixth, remove line 12.
Last edited on
Is there a way to still use that many parameters the thing im trying to calculate takes that many different stats into consideration?
Yes. Grouping things into structs was just a suggestion. You can have dozens of parameters.
Last edited on
Alright so I change it but im still getting the same errors
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
 #include <iostream>
using namespace std;

double calculate
(int pts, int fg, int fga, int fta, int ft, int orb, int drb, int stl, int ast, int blk, int pf, int tov) {
	double total;
		total = pts + 0.4 * fg - 0.7 * fga - 0.4 * fta - ft + 0.7 * orb + 0.3 * drb + stl + 0.7 * ast + 0.7 * blk - 0.4 * pf - tov;

		return total;
}
int main()
{
	
	double calculate;
	double gmsc;
	int pts, int fg, int fga, int fta, int ft, int orb, int drb, int stl, int ast, int blk, int pf, int tov;
	cout << "Enter points";
	cin >> pts ;
	cin >> fg  ;
	cin >> fga ;
	cin >> fta ;
	cin >> ft  ;
	cin >> orb ;
	cin >> drb ;
	cin >> stl ;
	cin >> ast ;
	cin >> blk ;
	cin >> pf  ;
	cin >> tov ;
	gmsc = calculate (pts, fg, fga, fta, ft, orb, drb, stl, ast, blk, pf, tov);
	cout << gmsc;
return 0;
}
Last edited on
You wrote
int pts, int fg, int fga, int fta, int ft, int orb, int drb, int stl, int ast, int blk, int pf, int tov;
but should have written
int pts, fg, fga, fta, ft, orb, drb, stl, ast, blk, pf, tov;
Thank youuuu now I'm getting a error on line 30 saying "expression preceding parentheses of apparent call must have (pointer-to-) function type"? what does that mean
Last edited on
You never removed line 12 in your original code. (Line 14 in your latest code.)

Your local variable called "calculate" is shadowing your function called "calculate".
Thank you I have a very smooth brain
lol, i think most of us have made silly mistakes like that more than we care to admit. Don't be discouraged.
Last edited on
Indeed. The "shadowing" is also called "masking". You were "lucky", compiler did not know what to do. On less severe mask compilation proceeds and compiler at most warns about it in "Are you sure?" kind of way. It is good to pay attention to those remarks. Worst case is compilation completed without warning, but obviously with executable that does not do what we think it does. Quiet compiler is naughty.


1
2
3
double total;
total = pts + 0.4 * fg;
return total;

has three statements. It could have just two or one:

1
2
double total = pts + 0.4 * fg;
return total;


return pts + 0.4 * fg;
Similarly:
1
2
3
double gmsc;
// many lines of code
gmsc = calculate ( ...

could be:
1
2
// many lines of code
double gmsc = calculate ( ...

Are these are more or less clear? That is up to you, but in these the variable is given a known value already when it is created (declared) and not "later". We tend to forget do task that we leave for later ...


total = pts + 0.4 * fg - 0.7 * fga - 0.4 * fta - ft + 0.7 * orb + 0.3 * drb + stl + 0.7 * ast + 0.7 * blk - 0.4 * pf - tov;
Isn't that the same as:
total = pts + 0.4 * (fg - fta - pf) - ft + 0.7 * (orb + ast + blk - fga) + 0.3 * drb + stl - tov;

Mathematically yes, but technically the CPU instructions might round interim results bit differently, so both equations may not end up with same result. Floating point math is devious.
Last edited on
Topic archived. No new replies allowed.