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.
#include <iostream>
usingnamespace 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;
}
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.
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
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?
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.