endless loop for scanf - plz help

This is a discussion on endless loop for scanf - plz help within the C Programming forums, part of the General Programming Boards category; you could always try something like this.. i've found it to work as desired. Code: /* Function makes sure the ...

  1. #16
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378
    you could always try something like this.. i've found it to work as desired.
    Code:
    /* Function makes sure the user entered a valid entry
    and returns it */
    int Get_Line_Amount()
    {
    	int amm_of_lines, ch, cont = 1;
    	
    	do {
    		printf("How many lines do you want to store?\n");
    		/* in case the user doesn't want any lines */
    		printf("(Enter 9999 if you dont want any):\n");
    		scanf("%d", &amm_of_lines);
    		/* clear the input buffer */
    		while ((ch = getchar()) != '\n' && ch != EOF);
    		
    		if (amm_of_lines == 0)
    		{
    			printf ("\nYou did not enter a valid number, Please re-enter.\n");
    		}
    		
    		else if (amm_of_lines == 9999) 
    		{
    			/* to indicate 0 lines were wanted when returned */
    			amm_of_lines = 0;
    			cont = 0;
    		}			
    		
    		else { cont = 0; }
    		
    		} while (cont != 0);
    
    	return amm_of_lines;
    }
    BUT, what are you going to do if the user enters: -10
    ?
    Registered Linux User #380033. Be counted: http://counter.li.org

  2. #17
    Registered User
    Join Date
    Mar 2005
    Posts
    36
    thank willc0de4food !!!
    it worked perfectly!!

  3. #18
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    > scanf("%d", &amm_of_lines);
    If scanf doesn't convert anything, the value pointed at is not written to, which in your case is uninitialised.

    I put a simple main around your code
    Code:
    int main ( ) {
        printf( "%d\n", Get_Line_Amount() );
        return 0;
    }
    And got these results
    Code:
    How many lines do you want to store?
    (Enter 9999 if you dont want any):
    hello
    -1073743572
    I got a different answer with different compiler options.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #19
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378
    so in the declaration of amm_of_lines, initialize it to 0.

    also i think, that if you dont use the code in the way that you did, and declare an int and have the returned number sent to the int, then less problems would occur. but who knows? it works for him and it works for me, so why doesn't it work for you? and i dont play with compiler options as i'm not sure how i would go about doing that.
    Code:
    [tim@shibby ~]$ gcc --help
    Usage: gcc [options] file...
    Options:
      -pass-exit-codes         Exit with highest error code from a phase
      --help                   Display this information
      --target-help            Display target specific command line options
      (Use '-v --help' to display command line options of sub-processes)
      -dumpspecs               Display all of the built in spec strings
      -dumpversion             Display the version of the compiler
      -dumpmachine             Display the compiler's target processor
      -print-search-dirs       Display the directories in the compiler's search path  -print-libgcc-file-name  Display the name of the compiler's companion library
      -print-file-name=<lib>   Display the full path to library <lib>
      -print-prog-name=<prog>  Display the full path to compiler component <prog>
      -print-multi-directory   Display the root directory for versions of libgcc
      -print-multi-lib         Display the mapping between command line options and
                               multiple library search directories
      -print-multi-os-directory Display the relative path to OS libraries
      -Wa,<options>            Pass comma-separated <options> on to the assembler
      -Wp,<options>            Pass comma-separated <options> on to the preprocessor  -Wl,<options>            Pass comma-separated <options> on to the linker
      -Xassembler <arg>        Pass <arg> on to the assembler
      -Xpreprocessor <arg>     Pass <arg> on to the preprocessor
      -Xlinker <arg>           Pass <arg> on to the linker
      -save-temps              Do not delete intermediate files
      -pipe                    Use pipes rather than intermediate files
      -time                    Time the execution of each subprocess
      -specs=<file>            Override built-in specs with the contents of <file>
      -std=<standard>          Assume that the input sources are for <standard>
      -B <directory>           Add <directory> to the compiler's search paths
      -b <machine>             Run gcc for target <machine>, if installed
      -V <version>             Run gcc version number <version>, if installed
      -v                       Display the programs invoked by the compiler
      -###                     Like -v but options quoted and commands not executed
      -E                       Preprocess only; do not compile, assemble or link
      -S                       Compile only; do not assemble or link
      -c                       Compile and assemble, but do not link
      -o <file>                Place the output into <file>
      -x <language>            Specify the language of the following input files
                               Permissible languages include: c c++ assembler none
                               'none' means revert to the default behavior of
                               guessing the language based on the file's extension
    
    Options starting with -g, -f, -m, -O, -W, or --param are automatically
     passed on to the various sub-processes invoked by gcc.  In order to pass
     other options on to these processes the -W<letter> options must be used.
    
    For bug reporting instructions, please see:
    <URL:http://bugzilla.redhat.com/bugzilla>.
    ? i do gcc -o filenameIwant filename.c
    Registered Linux User #380033. Be counted: http://counter.li.org

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Rewriting a for loop as a while/do-while loop
    By Ashfury in forum C++ Programming
    Replies: 7
    Last Post: 04-27-2007, 02:20 PM
  2. loop the loop - feeling foolish
    By estos in forum C Programming
    Replies: 2
    Last Post: 04-07-2007, 02:45 AM
  3. cin >> buf endless loop
    By cfriend in forum C++ Programming
    Replies: 2
    Last Post: 10-07-2005, 04:01 PM
  4. 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
  5. Endless loop!
    By Paninaro in forum C Programming
    Replies: 3
    Last Post: 06-23-2002, 08:15 PM

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