Help with scanf

This is a discussion on Help with scanf within the C Programming forums, part of the General Programming Boards category; Im not sure what i have done wrong here but my scanf doesnt seem to be work. Also is it ...

  1. #1
    newprog
    Guest

    Help with scanf

    Im not sure what i have done wrong here but my scanf doesnt seem to be work. Also is it okay to call the main function again like that. I just wanted to make main run again if the user inputs yes (y).

    Code:
    int correct()
    {
    	char character;
    
    	printf( "Well Done. You guess the correct number\n" );
    	printf( "Do you want to try again? (y or n): " );
    	scanf( "%c", &character );
    
    	if ( character == 'y' )
    		main();
    	if ( character == 'n' ) {
    		printf( "Exiting Program!" );
    		return 0;
    	}
    }
    Thanks

  2. #2
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,109
    scanf is used for digits but in ur case u need

    fgets
    Code:
    char str[BUFSIZ];
    fgets(str, BUFSIZ, stdin);
    BUFSIZ is defined in stdio.h
    fgets defined in string.h
    and stdin is standard inputwich is the keyboard

    now about the main thing u can say in main something like this
    well this is how i would do it
    i would call ur function in main and then after the call ask if u want to go on
    so the if(....=='y') thing would be in main

  3. #3
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Never call main in another function. This is not how it works.
    If the called function (correct()) is done it will automatically return to the calling function (main()). You could do something like this:
    Code:
    #include <stdio.h>
    
    int correct()
    {
       char buf[BUFSIZ];
       printf( "Well Done. You guess the correct number\n" );
       printf( "Do you want to try again? (y or n): " );
    
       /* I always use fgets to read from stdin */
       /* read a complete line from stdin */
       if(fgets(buf, BUFDIZ, stdin) == NULL)
          return 1;
    
       /* check first character */
       if(buf[0] == 'y' || buf[0] == 'Y')
          return 0;
       else
          return 1;
    }
    
    
    int main(void)
    {
       int done = 0;
    
    
       while(done == 0)
       {
          ...
          done = correct();
          ...
       }
       return 0;
    }

  4. #4
    newprog
    Guest
    eek! where can i find more info on this buf[BUFSIZ], im pretty new to C, and this sort of thing i havent covered just yet, maybe i should just use 1 and 0 for y and n...

  5. #5
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,109
    BUFSIZ is 1024 so a string can be 1024 signs long
    why do we use bufsize and not just use gets???

    because if u use gets u could get a buffer overflow. in this case u expect the user to just enter a Y or N but if some freakyzoid comes along and enters "dhalhfdallabdladhlahdalhdl"
    then u will have a buffer overflow wich will result in a strange-behaving-app

    for a little bit more info u could always check ur library reference that comes along with the compiler
    search for the functions
    gets and fgets then u'll notice the difference

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>BUFSIZ is 1024
    Compiler dependant. Mines only 512
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>BUFSIZ is 1024 so a string can be 1024 signs long
    It doesn't have to be, that's why there's a standard macro hiding the value. :-)

    >>why do we use bufsize and not just use gets???
    Because gets() is yucky, it doesn't check array boundaries, which is just asking for buffer overflow and undefined behavior.

    >>scanf is used for digits but in ur case u need fgets
    Why? In this case reading a character is perfectly natural. I would've used getchar though. :-)
    Code:
    #include <stdio.h>
    
    int correct(void)
    {
      int character;
      
      printf( "Do you want to try again? (y or n): " );
      
      character = getchar();
      while (getchar() != '\n'){}; /* Kill the newline */
      
      switch (character)
      {
      case 'y':
      case 'Y':
        return 1;
      case 'n':
      case 'N':
        printf("Thanks for playing!\n");
        return 0;
      default:
        printf("Huh?\n");
        return 1;
      }
    }
    
    int main(void)
    {
      while (correct())
      {
        /* Do stuff */
      }
    
      return 0;
    }
    *Cela*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. scanf() consideres useless
    By Snafuist in forum C Programming
    Replies: 15
    Last Post: 02-18-2009, 08:35 AM
  2. Help with a basic scanf procedure.
    By killpoppop in forum C Programming
    Replies: 9
    Last Post: 11-03-2008, 04:39 PM
  3. Replies: 2
    Last Post: 02-20-2005, 01:48 PM
  4. Scanf and integer...
    By penny in forum C Programming
    Replies: 3
    Last Post: 04-24-2003, 07:36 AM
  5. scanf - data is "put back" - screws up next scanf
    By voltson in forum C Programming
    Replies: 10
    Last Post: 10-14-2002, 05:34 AM

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