0

In this case, let's just put aside the non-standard code presented by the OP plus the seemingly 'impossible' basic requirements. It's quite obvious that the "void main()" etc. is not the key topic here, at least from the OP's viewpoint.

Again, like I wrote above, a simple #define solves this 'problem' in 100% standard and portable C++ code. As far as I can see, the only good outcome of this exercise for the OP would be to realize the possible dangers of using macros (e.g. using #defines carelessly).


@OP
It would actually be nice to know how come you are facing this dilemma, would you care to share that with us?

Ok ... I'll admit, I'm lost. I used #defines to declare token strings. I'm not seeing how that will do anything?

1

Ok ... I'll admit, I'm lost. I used #defines to declare token strings. I'm not seeing how that will do anything?

You #define d wrong things then :)

How about us letting this thread get some rest and see if the OP returns?

Votes + Comments
+rep for figuring out the #define.
1

did you now ? i find answer

void function(int arg)
{
	char chars[10];
	cout<<0;
	exit(0);
}

void main()
{
	int arg=0;
	function(5050);
	arg++;
	cout<<arg;
}

but my professor ignore it and tell me :there is another way

and in comparison with your indictments about (This very much looks like a 'trick' question) you said,anyway my professor will give me the correct answer after 1 week then i will tell you that answer then you will sorry from me>>

Votes + Comments
This is a doable, albeit odd, assignment. Have a couple points back...
0

>> i find answer
Your answer would require to include an additional header file <cstdlib>, and you cannot do that, given that you only are allowed to modify the function() function.
I think that the general problem setup suggests something else.

>> then i will tell you that answer
By all means, post back with the answer when you have it.

>> then you will sorry from me
I don't quite understand what that means .. though you sort of sound as having been insulted, if so, how come?

0

>> Your answer would require to include an additional header file <cstdlib>
My test works fine without this header.

The only header I have to include is iostream.h for 'cout'

0

I have a solution...

replace it with:

#define function(x); \
    arg--;

EDIT:
Looking back someone else has already sugested #define so credit to mitrmkar for the #define idea first

Edited by Colezy: n/a

0

>> Your answer would require to include an additional header file <cstdlib>
My test works fine without this header.

The only header I have to include is iostream.h for 'cout'

Your compiler is not standards-compliant then, exit() needs the <cstdlib> header. Maybe try out a modern compiler and see if your old code compiles?

0

>> Your compiler is not standards-compliant then, exit() needs the <cstdlib> header

True, but I've seen this before. Even VS (2005 pro) doesn't complain.

0

>> Your compiler is not standards-compliant then, exit() needs the <cstdlib> header

True, but I've seen this before. Even VS (2005 pro) doesn't complain.

It might be a Microsoft "extension". It also works in VS2008 Pro. However, it does not work in Code::Blocks.

0

I compiled using GCC v4.5.1

I'm having hard time in believing that 4.5.1 would come with the pre-standard file <iostream.h>. Are you sure that your compiler/IDE configurations are OK?

Would be nice to know if Visual Studio 2010 also behaves like this, anyone care to test?

0

I also tried with 4.5.1 and without the include I get compilation errors as I would expect. I will be able to test Visual Studio 2010 a bit later.

Edited by Valaraukar: n/a

0

tbh you are probably right about 4.5.1 not including iostream.h as standard. I am running this on my work linux server and it has tonnes of old headers and libraries for backwards compatability.

0

Am I the only one who STILL doesn't understand what the requirements of this assignment are? I've been following this thread. Apparently initially everyone was as confused as I was, but then a few posts back, everyone else "got it". I'm still as confused as I was twenty posts back. I always feel stupid when everyone else sees the light and I don't.

Apparently I'm using gcc 3.4.5. It allows me to use iostream.h, but gives me warnings. It won't allow "void main". OS is MS Vista.

0

Am I the only one who STILL doesn't understand what the requirements of this assignment are? I've been following this thread. Apparently initially everyone was as confused as I was, but then a few posts back, everyone else "got it". I'm still as confused as I was twenty posts back. I always feel stupid when everyone else sees the light and I don't.

Apparently I'm using gcc 3.4.5. It allows me to use iostream.h, but gives me warnings. It won't allow "void main". OS is MS Vista.

I'll PM you an example solution.

0

As far as I understand it, the requirements are:

-Only edit the code marked /* Some Statements */
-Do not touch any other code, do not touch the main func and we are assuming that we cannot include other headers but the OP hasn't explicitly stated this.
-The program should run and simply print a zero (0) on the screen and nothing else.

As is, the program prints a one (1) then exits.

Colezy

0

Thanks for the PM and the explanation guys. I imagine you have correctly divined the requirements and the solution and the point of the assignment. The OP could have made life easier by stating everything in the first post.

0

As far as I understand it, the requirements are:

-Only edit the code marked /* Some Statements */
-Do not touch any other code, do not touch the main func and we are assuming that we cannot include other headers but the OP hasn't explicitly stated this.
-The program should run and simply print a zero (0) on the screen and nothing else.

As is, the program prints a one (1) then exits.

Colezy

yes that is what exactly need to do

@ Colezy
did you test this solution :
#define function(x); /

arg--;

?????????
because i test it and it no work...

Edited by ASTA LAVISTA: n/a

0

yes that is what exactly need to do

@ Colezy
did you test this solution :
#define function(x); /

arg--;

?????????
because i test it and it no work...

It doesn't work because you used a forward slash '/' as a continuation mark. It should be a backslash '\'. Read this.

Edited by Fbody: n/a

0

replace it with:

#define function(x); \
    arg--;

Maybe try copy and paste next time :P

0

I have a solution...

replace it with:

#define function(x); \
    arg--;

EDIT:
Looking back someone else has already sugested #define so credit to mitrmkar for the #define idea first

thanxxxxxxxxxxxxxxxxxxxxx very much man you are proooo

but last question : pleas explain to me what is (#define) operation??

thank you 1000 time

0

#define is a compiler directive, it instructs the compiler to search through the whole program and replace the string 'function(x);' (where x can be anything) with the string 'arg--;'

So at compile time you program actually reads

int arg = 0;
   arg--;
   arg++;
   cout<<arg;

Regards,
Colezy

Edited by Colezy: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.