can't find the mistake

Can someone tell me where is the mistake in this code? It's supposed to have one according to my uni-test, but i've compiled it and it shows that everything is okey. I'm really confused.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  #include <iostream>
using namespace std;
int n = 20;
int g(int n) {
 n = n+2;
 cout << n << ::n;
 return n;
}
int f(int n) {
 n = n+1;
 return n;;
}
int main()
{
int n = 17;
cout << g(n-1) + g(n+1);
cout << n << ::n;
return 0;
}
What does your "uni-test" actually say?

Perhaps the problem is the extra semi colon on line 11? Or perhaps the lack of any whitespace in your output? Or perhaps the lack of a consistent indentation style?

sorry, it means university test. I've done it last semester and i'm doing it again. The compiler doesn't take the extra semi colon as a mistake. According to the answer i got from my professor it is something within the "int g (int n)" function, but i still can't find out the problem.
Main types of errors:
* Syntax. Fails to compile.
* Logical errors. Program does not do what it should.

What is in lines 9-12? Nothing that would execute. Is that a logical error?
The code you posted is syntactically correct. Without knowing the parameters of the "test" I can only guess as to the problem.

Is this a program that you wrote for the test?

The type of the test is that we have different codes and we must type the answers that come out. The answer of the one i gave you is - "???" and that means there is some kind of a mistake within the code.
__________________________________________________________________
#include <iostream>
using namespace std;
int n = 20;
int g(int n) {
n = n+2;
cout << n << ::n;
return n;
}
int f(int n) {
n = n+1;
return n;;
}
int main()
{
int n = 17;
cout << f(n-1) + f(n+1);
cout << n << ::n;
return 0;
}

For an example this one is correct and it gives- 361720
Actually the output of the program you gave in the first snippet is: 18202020381720, which looks correct for the given values.
How do we decide what should be the correct output? There needs to be some indication of what the program is supposed to be doing. Usually I'd consider it a problem to use cout << with a function which contains its own cout statement. That's not necessarily an error, but I'd suggest it would be a poor design - unless the specification called for it.
The idea is to give a correct output. I also get the same values from the first code. Is it possible that there is a mistake not in the code, but in the test answer given by the university?
Good question for your instructor I would say.

I'm leaning toward Chervil's suggestion. Perhaps the cout line in the g function (line 6 in OP) should be removed. It would then at least have some symmetry with the f function that was invoked in your "working" example.
Thanks a lot for the help!!
Topic archived. No new replies allowed.