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?

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?

commented: +rep for figuring out the #define. +11

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>>

commented: This is a doable, albeit odd, assignment. Have a couple points back... +3

>> 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?

>> 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'

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

>> 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?

>> 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.

I compiled using GCC v4.5.1

>> 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.

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?

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.

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.

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.

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.

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

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.

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...

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.

replace it with:

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

Maybe try copy and paste next time :P

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

#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

@Colezy
thanx very much you are really pro programmer >>>

solved

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.