Thread: username code not taking me to next funtion

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    66

    username code not taking me to next funtion

    The code below should take the user to the function 'f_main_menu ()' when they enter 'Nick' but instead the program just closes any ideas why?

    Code:
    void f_enter_username ()
    
    {
      char Owner[] = "Nick";
      char buf[BUFSIZ];
      
      printf ("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nEnter username: ");
      scanf("%s", Owner);
      fflush(stdout);
      if (fgets(buf, sizeof(buf), stdin))
      {
        char *p = strchr(buf, '\n');
        if (p) *p = '\0';
        if (strcmp(Owner, buf) == 0)
          {
          f_main_menu ();
           }
        else
    
      return 0;
    }

  2. #2
    Registered User
    Join Date
    Jul 2003
    Posts
    85
    I'm guessing you don't want this line:

    scanf("%s", Owner);

    Seeing as how the next few lines are for accepting user input. Try getting rid of it and see how the program works.

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Are you sure that you are giving strcmp() the things you expect?

    Why do you have scanf() for Owner?

    Here's a suggestion:

    put the following just before the line where you invoke strcmp()

    Code:
        printf("Owner = <%s>\n", Owner);
        printf("buf   = <%s>\n", buf);

    printf() is your friend.

    Dave

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    66
    ive tried both but now when its compiling it says 'choice_i' undeclared', refering to the next function in the program.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > it says 'choice_i' undeclared', refering to the next function in the program.
    Means nothing to us without you actually show your code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Mar 2004
    Posts
    66
    when the user enters Nick they should be taken to f_main_menu_choice () but the compiler returns ''choice_i' undeclared'??

    Code:
    void f_enter_username ()
    
    {
      char Owner[] = "Nick";
      char buf[BUFSIZ];
      
      printf ("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nEnter username: ");
      fflush(stdout);
      if (fgets(buf, sizeof(buf), stdin))
      {
        char *p = strchr(buf, '\n');
        if (p) *p = '\0';
        if (strcmp(Owner, buf) == 0)
          {
          f_main_menu_choice ();
           }
        else
    
      return 0;
    }
    
    /* ********************************************************* */
    void f_main_menu_choice ()
    {
    
        int choice_i;
    
    	do
    	{
    		choice_i = f_main_menu ();
    
    		switch ( choice_i )
    		{
    			case 1 : f_designer_menu_choice ();
    				break;
    
    			case 2 : f_customer_menu_choice ();
    				break;
    
    			case 3 : printf ("************************************\n\n");
    				 printf ("Thankyou for using this program\n\n");
    			         printf ("Bye\n\n");
    				 printf ("************************************\n\n");
    				 break;
    		}
    	}
    	while ( choice_i != 3 );
    	
    
    }

  7. #7
    Registered User
    Join Date
    Mar 2003
    Posts
    143
    Count your braces ('{' and '}'). In the function you've posted you
    haven't finished the function. This will cause all sorts of problems
    in the next function.

    The "else return 0" is pretty redundant too...

    Try this:
    Code:
    void f_enter_username ()
    {
      char Owner[] = "Nick";
      char buf[BUFSIZ];
      
      printf ("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nEnter username: ");
      fflush(stdout);
      if (fgets(buf, sizeof(buf), stdin))
      {
        char *p = strchr(buf, '\n');
        if (p) *p = '\0';
        if (strcmp(Owner, buf) == 0)
        {
          f_main_menu ();
        }
        // you only really need an else here if it's actually 
        // doing something
      } // this was missing previously
    }
    DavT
    -----------------------------------------------

  8. #8
    Registered User
    Join Date
    Mar 2004
    Posts
    66
    Great thanks thats sorted that out but now im back to my previous problem. It doesnt actually let me enter anything. the program just exits.

    Code:
    {
      char Owner[] = "Nick";
      char buf[BUFSIZ];
    
      printf ("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nEnter username: ");
      fflush(stdout);
     if (fgets(buf, sizeof(buf), stdin))
      {
        char *p = strchr(buf, '\n');
        if (p) *p = '\0';
        if (strcmp(Owner, buf) == 0)
          {
          f_main_menu_choice ();
           }
    
    }
    }

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Did you call scanf() at all before trying to call fgets() in this function?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Registered User
    Join Date
    Mar 2004
    Posts
    66
    Yes like in the first post but that didnt work.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Mixing scanf() and fgets() is a bad idea. scanf() just loves leaving ununsed characters on the input stream, mostly just newlines.
    This is a problem, because fgets() uses newlines to mark the end of a line of input.

    Which leads to
    Code:
    scanf( "%s", name );
    fgets( buff, sizeof buff, stdin );  // this is skipped
    Kind of comments

    Always (and exclusively) use fgets() for input, then deal with the buffer however you want.

    Code:
    fgets( buff, sizeof buff, stdin );
    sscanf( buff, "%s", name );
    fgets( buff, sizeof buff, stdin );
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  12. #12
    Registered User
    Join Date
    Mar 2004
    Posts
    66
    Great thanks for that its working fine now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Proposal: Code colouring
    By Perspective in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 05-14-2007, 07:23 AM
  2. Updated sound engine code
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-18-2004, 12:38 PM
  3. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  4. Please help me
    By teedee46 in forum C++ Programming
    Replies: 9
    Last Post: 05-06-2002, 11:28 PM
  5. Replies: 4
    Last Post: 01-16-2002, 12:04 AM