cannot run it in a loop :(

This is a discussion on cannot run it in a loop :( within the C Programming forums, part of the General Programming Boards category; pls help me make this thing run in a loop.. Code: #include <stdio.h> int main (void) { char name[BUFSIZ] = ...

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    20

    cannot run it in a loop :(

    pls help me make this thing run in a loop..

    Code:
    #include <stdio.h>
    
    int main (void)
    {
    	char name[BUFSIZ] = {30};
    	int age;
    	char ans = 'y';
    	FILE *pFile;
    	pFile = fopen ("c:\\test\\myfile_c++.txt","at");
    	
    	if (pFile!=NULL)
    	{
    		do
    		{ 
    			printf ("Enter your name : ");
    			gets (name);
    			printf ("Enter your age : ");
    			scanf ("%i",&age);
    			fprintf (pFile,"%s %d\n",name,age);
    			fclose (pFile);
    			printf ("Do you want to continue : ");
    			scanf ("%c", ans);
    		}
    		while (ans != 'n');
    	}
    	return 0;
    }

  2. #2
    Im back! shaik786's Avatar
    Join Date
    Jun 2002
    Location
    Bangalore, India
    Posts
    345
    1.) Don't fclose() the file here, instead, just before the statement: 'return 0;'. That's because, if the loop continued for the second time or more, the file pointer pFile would be pointing to some invalid location for it's transaction, since you have already closed the file.
    2.) >scanf ("%c", ans);
    You need to pass &ans as the parameter instead.

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    20
    thanx salem.. that was great help..

    can we make it running in a window, my goal is to make user enter his entries through a window.. using window() ..

    that was the reason y i was using
    char name[BUFSIZ] = {30};
    and cgets in my earlier programs..

  4. #4
    Registered User
    Join Date
    Nov 2002
    Posts
    20

    Smile done at last..

    aaye.. i suppose i m cheating .. but atlast i got this thing working.. and the user will now make entries in window and confirmation will be made to continue or exit.. all those entries will be stored in a file..

    thanx everybody helping me make this code work.. thanx a lot..

    any suggestions or available changes will be highly appreciated

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <conio.h>
    
    	int main(void) 
    	{
        char buff[BUFSIZ];  /* general input buffer */
        char name[BUFSIZ] = {30};
        int age;
        char ans;
    
        FILE *pFile;
        char *p, *q;
        pFile = fopen( "c:\\test\\myfile.txt","at" );
        
        clrscr();
        if(pFile!=NULL)
        
         {
            do{
                window(10,8,60,9);
                /* read the name */
                /* the last statement removes the newline (if present) which */
                /* fgets() will add ( gets() doesn't add a newline ) */
                
                cprintf( "Enter your name : " );
                p = cgets(name);
    
                cprintf( "\r\nEnter your age : " );
                fgets( buff, sizeof(buff), stdin );
                sscanf( buff, "%i", &age );
    
                fprintf( pFile,"%s %d\n",p,age );
                cprintf("\r\n \r\n");
                cprintf( " Do you want to make another entry\r\n" );
                cprintf( "   'y' to continue, 'n' to exit     : ");
                fgets( buff, sizeof(buff), stdin );
                ans = buff[0];
                clrscr();
            }
            while ( ans != 'n');
            fclose( pFile );
        }
        return 0;
    }
    Last edited by vivek_kumbhar; 11-03-2002 at 07:09 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can a "switch" be inside a loop?
    By gmk0351 in forum C Programming
    Replies: 5
    Last Post: 03-28-2008, 05:47 PM
  2. Program will not run thorugh the loop
    By Babs21 in forum C Programming
    Replies: 6
    Last Post: 11-10-2007, 11:59 AM
  3. loop needed also how to make input use letters
    By LoRdHSV1991 in forum C Programming
    Replies: 3
    Last Post: 01-13-2006, 04:39 AM
  4. I need help as soon as possible.
    By hyrule in forum C++ Programming
    Replies: 7
    Last Post: 11-09-2005, 04:49 PM
  5. How to change recursive loop to non recursive loop
    By ooosawaddee3 in forum C Programming
    Replies: 1
    Last Post: 06-24-2002, 08:15 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21