Function not working

This is a discussion on Function not working within the C Programming forums, part of the General Programming Boards category; I am currently working on a final program for school, the first part of my program calls a function that ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    31

    Function not working

    I am currently working on a final program for school, the first part of my program calls a function that calls a file (password.txt). which looks like:

    leia 12345
    darth 23456
    r2d2 34567
    solo 45678
    jabba 56789
    yoda 67890

    with the first column being the user name and the second being the password. My function ask the user to enter his/her username then password... all is well accept it does not work. I am using apples xcode to build this. here is my program so far: PLEASE note I have nothing in main yet, after I get the function working I have to make the program close if the user inputs the password wrong three times.

    ADDED!!!! Ok I just realized I will make this function send back 1 or 0, if it sends back 0 I will have main close the program, if it sends back 1 it will let the main part of the program continue (in this case print something but a lot more goes there, this is just the first part).

    Code:
    /* Location of all files is at: */
    /* /users/williammcfadden/Documents/school/EGR 115/Final Project/    */
    
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    /* function Declarations */
    int password(FILE *);
    
    main()
    {
    
    
    	FILE *Fpassword;
    	FILE *Fplanes;
    	FILE *Fdistout;
    
    	Fpassword=fopen("/users/williammcfadden/Documents/school/EGR 115/Final Project/password.txt", "r");
    	Fplanes=fopen("/users/williammcfadden/Documents/school/EGR 115/Final Project/planes.txt", "w");
    	Fdistout=fopen("/users/williammcfadden/Documents/school/EGR 115/Final Project/distlog.out", "w");
    	
    	
    	/*password function */
    	password(Fpassword);
    
    
    
    
    
    printf("hello ya'll \n");
    
    
    	fclose(Fpassword);
    	fclose(Fplanes);
    	fclose(Fdistout);
    	
    }
    
    
    /************************************************************************************************/
    /************************************************************************************************/
    /************************************************************************************************/
    	
    	
    	
    	int password(FILE *Fpassword)
    		{
    		char  NAME[50];
    		int PASSWORD;
    		int l;
    		char* name[6];
    			for(l=0; l<6; l++)
    					{
    					name[l]= malloc        (50);
    					}
    		int  password[6];
    		int i;
    		int h;
    		int R;
    		int z;
    		
    		
    		for(i=0; i<6; i++)
    			{
    			fscanf(Fpassword, "%s", name[i]);
    			fscanf(Fpassword, "%d", password[i]);
    			}
    		
    		printf("Please enter your\n");
    		scanf("%s", NAME);
    		printf("Please enter your password \n");
    		scanf("%i", &PASSWORD);
    		
    		for(h=0; h<6; h++)
    			{
    			if (0==strcmp(NAME, name[h]))
    				{
    				if (PASSWORD == password[h])
    					{
    					return 1;
    					}
    					else {
    						 printf("please enter a correct name \n");
    						 scanf("%s", NAME);
    						 printf("please enter a correct password \n");
    						 scanf("%i", PASSWORD);
    						 for(R=0; R<6; R++)
    							{
    								if (0==strcmp(NAME, name[R]))
    								{
    								if (PASSWORD== password[R])
    									{
    									return 1;
    									}
    								}
    							}
    							else printf("ONE LAST TIME BUDDY \n");
    							{
    								 printf("please enter your name \n");
    								 scanf("%s", NAME);
    								 printf("please enter your password");
    								 scanf("%i", PASSWORD);
    								  for(z=0; z<6; z++)
    									{
    										if( 0==strcmp(NAME, name[z]))
    										{
    										if (PASSWORD == password[z])
    										{
    										return 0;
    									}
    			}

    Currently this program will not compile or run, earlier (before I broke it) it would compile and give me a sigbus10 or 11 error.

    IF you want to know what the instructor wants the password function to do. Here is what he said:


    This program must be secure. The user must input their name and pin number, if both (name and pin) matches the list you have in a text file on the disk (password.txt), than the user gets access to this program. The program should allow 3 tries for name and password entry, if unsuccessful after 3 tries the program should print a message to the user and terminate.

    More requirements, In addition to our standards:
    The password.txt file will only contain the six names and pin numbers mentioned above.
    Your program should be able to process the maximum of 50 aircrafts.
    You must use a two dimensional array for the aircraft position data.
    The main function must do very little other than calling other functions and passing parameters to those functions.
    Your program should be modularly designed with functions designed to do one task and one task well.
    Functions that calculate should not print. And functions that print should do minimal calculations if any.
    Put functions below the main and place prototypes above the main.
    Do not use global variables. Pass data back and forth via parameters or as return values.
    Make your functions as general as possible so that they can be called more than once if needed.
    Document your main function as well as every function you write.
    Use defined constants for ALL constants in your program. """"

    THANKS
    Last edited by sloopy; 11-12-2005 at 02:36 PM.

  2. #2
    Registered User
    Join Date
    Nov 2005
    Posts
    31
    ok if I change the malloc to "new" then add an array it will compile but I get sigus code 11. Also I am using an apple so I am using xcode.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    26
    what I can see is, you first have to declare all the variables, and then you can use things like loops and so on.
    In your function password() you first declare some variables and char* name[6], then you use a for-loop and then you declare again some variables.

    and what's the thing with the malloc(50)? or the new(50)?

  4. #4
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    What errors does the compiler give? If it compiles, but crashes, where?

    1) You pass %d to fscanf when reading the passwords, but %i when getting the passwords. I don't think this will affect your output, but why?
    2) You've called malloc() without calling free() - again, not the immediate cause of your problem.
    3) The area where you prompt for passwords contains a lots of redundant code. Take your instructors advice, and see if perhaps you could lay out the section where you prompt three times in another manner, with repeating so much code. Your instructors advice about functions doing on thing and doing to well was good. A better layout in that (currently confusing and hard to read) area might bring out the error.
    4) Again, many of the if() conditionals in that area are redundant. Instead of:
    Code:
    if (0==strcmp(NAME, name[h]))
    {
    if (PASSWORD == password[h])
    { return 0; }
    else
    {
    ...
    }
    Wrap that into one nice statement: (Unless you plan to take a different action if only 1 credential matches)
    Code:
    if(0==strcmp(NAME, name[h]) && PASSWORD == password[h])
    Ok. Found your error. Check the code near "ONE LAST TIME BUDDY" -- that else has no if statement above it. The curly brace immediately above that if closes the for() loop, not the if(). The printf() statement should also go inside the else's curly brace.

    Found why it segfaults. This line:
    Code:
    fscanf(Fpassword, "%d", password[i]);
    fscanf expects a pointer to an integer. (int *). You're passing an integer. Try:
    Code:
    fscanf(Fpassword, "%d", &(password[i]);
    Check your other scanf() calls as well. You repeat that error at least twice more when prompting for pin numbers.

    Finally, recheck your logic on your if() statements where you check user & pin. They will not work like you expect. A wrong user/pin combonation can, with your layout, return a successful login.
    Last edited by Cactus_Hugger; 11-12-2005 at 03:30 PM.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  5. #5
    Super Moderator Harbinger's Avatar
    Join Date
    Nov 2004
    Posts
    74
    Well you've certainly got the "write f-ugly code and dump it on a message board for fixing" sorted out

  6. #6
    Registered User
    Join Date
    Nov 2005
    Posts
    31
    lol I still cant get it fixed, I have ruined it!!!

    Code:
    /* Location of all files is at: */
    /* /users/williammcfadden/Documents/school/EGR 115/Final Project/    */
    
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    /* function Declarations */
    int password(FILE *);
    
    main()
    {
    
    
    	FILE *Fpassword;
    	FILE *Fplanes;
    	FILE *Fdistout;
    
    	Fpassword=fopen("/users/williammcfadden/Documents/school/EGR 115/Final Project/password.txt", "r");
    	Fplanes=fopen("/users/williammcfadden/Documents/school/EGR 115/Final Project/planes.txt", "w");
    	Fdistout=fopen("/users/williammcfadden/Documents/school/EGR 115/Final Project/distlog.out", "w");
    	
    	
    	/*password function */
    	password(Fpassword);
    
    
    
    
    
    printf("hello ya'll \n");
    
    
    	fclose(Fpassword);
    	fclose(Fplanes);
    	fclose(Fdistout);
    	
    }
    
    
    /************************************************************************************************/
    /************************************************************************************************/
    /************************************************************************************************/
    	
    	
    	
    	int password(FILE *Fpassword)
    		{
    		char  NAME[50];
    		int PASSWORD;
    		int l;
    		char* name[6];
    			for(l=0; l<6; l++)
    					{
    					name[l]= malloc        (50);
    					}
    		int  password[6];
    		int i;
    		int h;
    		int R;
    		int z;
    		
    		
    		for(i=0; i<6; i++)
    			{
    			fscanf(Fpassword, "%s", name[i]);
    			fscanf(Fpassword, "%d", &(password[i]));
    			}
    		
    		printf("Please enter your\n");
    		scanf("%s", NAME);
    		printf("Please enter your password \n");
    		scanf("%i", &PASSWORD);
    		
    		for(h=0; h<6; h++)
    			{
    			if(0==strcmp(NAME, name[h]) && PASSWORD == password[h])
    					{
    					return 1;
    					}
    					else {
    						 printf("please enter a correct name \n");
    						 scanf("%s", NAME);
    						 printf("please enter a correct password \n");
    						 scanf("%i", PASSWORD);
    						 for(R=0; R<6; R++)
    							{
    								if (0==strcmp(NAME, name[R]))
    								{
    								if (PASSWORD== password[R])
    									{
    									return 1;
    									}
    								}
    							}
    							else {
    							printf("ONE LAST TIME BUDDY \n");
    							
    								 printf("please enter your name \n");
    								 scanf("%s", NAME);
    								 printf("please enter your password");
    								 scanf("%i", PASSWORD);
    								  for(z=0; z<6; z++)
    									{
    										if( 0==strcmp(NAME, name[z]))
    										{
    										if (PASSWORD == password[z])
    										{
    										return 0;
    									}
    			}}}}}}}
    go ahead and fix it if you want but I just want to know how to... I'm to lazy to fix the bracket ugliness, but if one of you wants to jump in and do it go right ahead...
    IT WAS beautiful in my eyes..

  7. #7
    Registered User
    Join Date
    Nov 2005
    Posts
    31
    besides the }}}}}} what is so ugly about it?

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Code:
    scanf("%s", NAME);
    1) Why are your variables in uppercase? Uppercase is usually reserved for constants.
    2) That won't properly read in a name like "John Smith". scanf() (by default) doesn't consider whitespace part of strings.
    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.

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Code:
    besides the }}}}}} what is so ugly about it?
    The indentation.
    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.

  10. #10
    Registered User
    Join Date
    Nov 2005
    Posts
    31
    1. out of habit
    2. so you are saying there should be no space after the comma?"

    indentation looks right to me. What's wrong?

  11. #11
    Registered User
    Join Date
    Nov 2005
    Posts
    31
    BTW I am now getting an error at that else statement.

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    2. so you are saying there should be no space after the comma?"
    No, no, no. scanf() considers whitespace to separate strings. Use fgets() if you want spaces included in your string. (You can do it with scanf, too. I don't remember how.)

    indentation looks right to me. What's wrong?
    Most people like to indent 4 spaces (or at least a consistent value). And they usually line up closing braces with opening ones.
    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.

  13. #13
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    What? "Parse error at end of input"? "Unexpected end of file found"?
    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.

  14. #14
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Wait. You have
    Code:
    for() {}
    else {}
    [edit]
    If you indented properly, it might make it easier to see that kind of thing.
    Bad:
    Code:
    #include <stdio.h>
    
    int main(void)
      {
      printf("Hello, World!\n");
      if(1)
                        {
                        printf("Hello again.\n");
                        }
               return 0;
               }
    Good:
    Code:
    #include <stdio.h>
    
    int main(void) {
        printf("Hello, World!\n");
        if(1) {
            printf("Hello again.\n");
        }
        return 0;
    }
    or, if you really want to align the braces that way,
    Code:
    #include <stdio.h>
    
    int main(void)
        {
        printf("Hello, World!\n");
        if(1)
            {
            printf("Hello again.\n");
            }
        return 0;
    }
    Personally, I find that style of braces very hard to read.
    [/edit]
    Last edited by dwks; 11-12-2005 at 05:48 PM.
    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.

  15. #15
    Registered User
    Join Date
    Nov 2005
    Posts
    31
    parse error before else

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 07:54 PM
  2. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-30-2005, 12:21 AM
  3. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 06:45 AM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM
  5. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 11:00 AM

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