Thread: loop and compiling inside a code

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    1

    loop and compiling inside a code

    I've been studying C some weeks for now, and now I'd like to write a little program with it. (I've had quite a lot of experience with QB, Visual Basic and Delphi years ago, but now I picked up C as a course in my univ.)
    Anyway I decided to code a little 'game'.

    The game is of coding.

    Code:
    tehtava1()
    {
    	clrscr();
    	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");
    	fflush(stdin);
    	{
    	gets(hw);
    	fprintf(tiedosto,"%s\n",hw);
    }
    	}
    the idea is to open the helloworld.txt and allow user to write there. Then it should compile and run the code and compare the result to desired result.

    First of all... I just can't make the loop work here... (it should allow user to write lines until user inputs *) also, i've noidea how to compile and run a code inside a code :P

    So any ideas? Should I scrap my code and try something else?


    (And if someone need information what os/compiler I use, Win XP sp2, Borland Developer Studio 2006)
    Last edited by MtJ; 03-03-2006 at 11:29 AM. Reason: A bit more information...

  2. #2
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    First of all, fflush(stdin) is not appropriate as it's undefined. You cannot rely in fflush() on input devices.

    Second, what loop? I see no loop here. No for, no while, no do/while.

    Third, gets() is a very dangerous function to use. use fgets() instead. It limits the input so you won't overrun your input buffer and wipe out memory that you should not overwrite.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  3. #3
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    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*/
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Expand code n times?
    By TriKri in forum C++ Programming
    Replies: 5
    Last Post: 07-11-2008, 09:38 AM
  2. For loop help...
    By Karpaty in forum C Programming
    Replies: 7
    Last Post: 10-19-2007, 12:22 AM
  3. while loop.. do while..?
    By Lau in forum C Programming
    Replies: 2
    Last Post: 11-11-2002, 03:31 PM
  4. problem compiling code and EOF
    By maxthecat in forum C Programming
    Replies: 4
    Last Post: 03-12-2002, 05:22 AM
  5. do..while loop problems
    By catalyst in forum C Programming
    Replies: 3
    Last Post: 11-20-2001, 07:52 PM