Help

This is a discussion on Help within the C Programming forums, part of the General Programming Boards category; Hi im new and i was wondering if anyone could help me with this code Code: /* ******************************* * author:kevin ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Location
    Germany
    Posts
    10

    Help

    Hi im new and i was wondering if anyone could help me with this code
    Code:
    /* *******************************
    * author:kevin peters 
    * revison #:0.1
    **********************************/
    #include <stdio.h>
    #include <stdlib.h>
    
    #define QUIT 5
        char filename[80];
    	int get_menu( void );
    	FILE *fp;
    
    main()
    {
    	
    	int choice = 0;
    	/* while() makes sure choice does not equal QUIT */
    	while (choice != QUIT)
    	{
    		
    		choice = get_menu();
    		if (choice == 1)
    		{
    			// creates file
    			puts("Enter a name for the file:");
    			gets(filename);
    			if ((fp = fopen(filename, "w")) == NULL)
    			{
    				fprintf(stderr, "Error creating file %s", filename);
    				exit(1);
    			}
    			
    			
    			fclose(fp);
    		}
    		if (choice == 2)
    		{
    			//edits previous file
    			puts("Enter a file to edit:");
    			gets(filename);
    			if ((fp = fopen(filename, "w")) == NULL)
    			{
    				fprintf(stderr, "Error opening file.");
    				exit(1);
    			}
    			fclose(fp);
    		}
    		if (choice == 3)
    		{
    			
    			//reads a file
    			puts("Enter a file to read:");
    			gets(filename);
    			if ((fp = fopen(filename, "r")) == NULL)
    			{
    				fprintf(stderr, "No exsiting file.");
    				exit(1);
    			}
    			fclose(fp);
    		}
    		if (choice == 4)
    		{
    			// deletes previous file
    			puts("Enter a filename to remove: ");
    			gets(filename);
    			if ( remove(filename) == 0)
    				printf("Succesful deletion of %s", filename);
    			else
    				fprintf(stderr, "Error deleting file %s", filename);
    			exit(1);
    			
    		}
    		
    	}
    	return (0);
    }
    
    int get_menu( void )
    {
    	int selection = 0;
    	/* display menu() to screen */
    	do
    	{
    		
    		printf("\n");
    		printf("1 - Create entry\n");
    		printf("2 - Edit previous entry\n");
    		printf("3 - Read previous entry\n");
    		printf("4 - Delete previous entry\n");
    		printf("5 - Quit\n");
    		printf("\n");
    		printf("\nEnter what you would like to do: ");
    	
    		scanf("%d", &selection);
    		
    	}
    	while (selection < 1 || selection > 5);
    	return selection;
    }
    i keep getting warnings and if this helps too i edit it in visual c++ and compile with borlands c/c++ compiler because visual c++ compiler doesnt work for me
    Last edited by Rakansen; 10-21-2006 at 12:45 PM.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    i keep getting warnings
    what are they? what line number?
    thatll be alot easier than reading each line to find it.

    edit: is it because you dont have 'int main'?

  3. #3
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    main should be defined as either int main() or as int main (int argc, char **argv).
    Also, as nadroj said, before we can help you we need the warnings you are getting.

  4. #4
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    That may be one issue though as far as I know the implied type is int. But clearly state int main... I don't feel like trying to compile it myself or reading through so please post up your warnings.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Location
    Germany
    Posts
    10
    i always thought for c that it is only supposed to be main() not int main()
    ohh and the warnings are not the choice parts but the if statements in each if statement

  6. #6
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094

  7. #7
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    can you copy and paste the warning with the line numbers.

  8. #8
    Registered User
    Join Date
    Oct 2006
    Location
    Germany
    Posts
    10
    heres the warnings:

    Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland

    Warning W8069 35: Nonportable pointer conversion in function main
    Warning W8060 35: Possibly incorrect assignment in function main
    Warning W8069 49: Nonportable pointer conversion in function main
    Warning W8060 49: Possibly incorrect assignment in function main
    Warning W8069 62: Nonportable pointer conversion in function main
    Warning W8060 62: Possibly incorrect assignment in function main
    Warning W8075 67: Suspicious pointer conversion in function main
    Turbo Incremental Link 5.00 Copyright (c) 1997, 2000 Borland

  9. #9
    Registered User
    Join Date
    Apr 2006
    Posts
    2,012
    main() is valid and standard, because if you don't give a function a return value it is assumed to be int. It is however confusing and bad practice to do this.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  10. #10
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    where you do things like this:
    Code:
    if (fp = fopen(filename, "r") == NULL)
    you should be doing:
    Code:
    if ((fp = fopen(filename, "r")) == NULL)
    and also compare all of your fclose calls.. one of them is wrong.

  11. #11
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    King Mir, that is only in C89 that main can be defined as such. In C99 main should be defined as int main() or int main(int argc,char **argv).

    Perhaps you should read the FAQ I posted also.

  12. #12
    Registered User
    Join Date
    Oct 2006
    Location
    Germany
    Posts
    10
    Ok. thanks for that but now im getting the problem of not allowing me to open any files or even edit them. it keeps showing the stderr from fprintf. can anyone help me on this? btw there is no more error in the compiling

  13. #13
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    // suggestion deleted...

    edit:

    it looks like this was the problem (and, again, wherever similar code exists):
    Code:
    puts("Enter a name for the file:");
    gets(filename);
    when i used scanf instead of gets it worked.

    notice that you never get a chance to actually type in a filename (or at least it didnt for me) after you print to ask for one.. im sure its something to do with the newline character or some buffer from the initial scanf from the menu.

    ive never written a C program before so i dont know the details about which function is better or why this is happening (exactly).

    it looks like you should use fgets instead of gets as the latter function is prone to buffer overflows. to keep your program as it is (ie gets or fgets) ive read that you can just use getchar() after your first scanf to clear the buffer.. give it a shot.
    Last edited by nadroj; 10-21-2006 at 09:12 PM.

  14. #14
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Using gets() is a bad idea: http://faq.cprogramming.com/cgi-bin/...&id=1043284351

    I suspect that when you "used scanf instead of gets" you have also have a buffer overflow, such as this:
    Code:
    char *str;
    scanf("%s", str);
    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

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