Well that code is a mess so i'll try and highlight what you want to
do with it:
i've noidea how to compile and run a code inside a code :P
what i think you mean by this is to create a function of your own
that does what you want (by saying compile a code within a code
it appears that you want main to call the function, in the same
manner as calling printf does stuff)
Code:
void tehtava1(); /*you need to have a return type, void, int etc*/
int main (void)
{
/*local variable declarations*/
tehtava1(); /*This is where your function is called - the code it contains is executed*/
return 0;
}
void tehtava1()
{
/*Here you define what the function does, ie your attempt at coding above*/
}
For more info on functions follow this link to the tutorial
now i'll go through your code and show the rest of whats wrong:
Code:
tehtava1() /*Needs a return type as already mentioned*/
{
clrscr(); /*use of a non-standard function, this is defined in conio.h so if you
dont have that, it won't work*/
printf("Tehtävä 1: Ohjelmoi 'Hello World' tervehdys, kun olet valmis paina *\n\n");
FILE *tiedosto;
char hw[255];
tiedosto=fopen("c:/temp/helloworld.txt","w"); /*No error checking to see
whether the file was opened
correctly, fopen returns NULL
if the file couldn't be opened -
look into it*/
fflush(stdin); /*already explained by WaltP*/
{ /*no point in having these here - i think this
may be where you were trying to put the loop*/
gets(hw); /*as above WaltP*/
fprintf(tiedosto,"%s\n",hw);
}
} /*poor indentation - not critical fault with program but its bad
style as good indentation can improve debugging the code*/