scanf is confusing me.

This is a discussion on scanf is confusing me. within the C Programming forums, part of the General Programming Boards category; Hello, First the code- Code: #include<stdio.h> int main() { char input; input = NULL; while(input != 'Q') { printf("Enter Letter:"); ...

  1. #1
    Registered User
    Join Date
    Jul 2007
    Location
    Grand Junction, CO, USA
    Posts
    2

    Question scanf is confusing me.

    Hello,

    First the code-

    Code:
    #include<stdio.h>
    
    int main()
    {
    
    	char input;
    	
    	input = NULL;
    
    	while(input != 'Q') {
    		printf("Enter Letter:");
    		scanf("%c", &input);
    	}
    	
    	return 0;
    }
    I am very new to C but not new to programming (although it has been a long time since I did any of it). I have the K&R book and have found it to be as confusing as it is informative. Right now I am attempting the above as an exercise to control the return of the program. I have no idea if this is a very stupid way of doing it, but it's what I have managed so far.

    The problem- Whenever I run this program it does as it should in my terminal window (I'm using a bash terminal on mac os x btw) and prints "Enter Letter:" to the screen and waits for my input. If I enter "Q" it exits the loop and returns the program perfectly. However, if I try entering any other type of data it newlines and prints "Enter Letter:" once, and then again for each character I have inputted.

    For example-

    Code:
    Enter Letter:A
    causes the next line to be

    Code:
    Enter Letter:Enter Letter:
    I am figuring that the problem is my lack of understanding of how scanf actually works and I must admit I am a bit lost in how C seems to handle data. I have also tried getchar() and it gives the same result with the testing I have done so far.

    Also, I am new to posting on message boards so I hope I have not posted anything incorrectly. Any tips for making my future postings easier for people to read are greatly appreciated.

    Thank you to any and all who reply.

    Babel

  2. #2
    Eager young mind
    Join Date
    Jun 2006
    Posts
    342
    This has been asked quite a few times here. The ' \n' gets into the input buffer when you hit the enter key. That is why you are getting that kind of an output. It would be better to use fgetc() / fgets() to accept chars or strings from the user.
    In the middle of difficulty, lies opportunity

  3. #3

  4. #4
    life is a nightmare
    Join Date
    Apr 2007
    Posts
    127
    also
    input=NULL;
    assignment makes integer from pointer without a cast
    you cannot assign NULL to ordinary char like that

  5. #5
    Registered User
    Join Date
    Jan 2007
    Posts
    72
    i think using do while statment is better to solve this problem if you don't know it send me a private email
    proud to be from aui www.aui.ma and also a great elton john's fan

  6. #6
    Registered User
    Join Date
    Jan 2006
    Posts
    34
    guys wouldnt fflush(stdin) before the scanf solve the problem ??

    I am no pro so feel free to flame me :P

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,650
    guys wouldnt fflush(stdin) before the scanf solve the problem ??
    From the FAQ: Explanations of Why fflush(stdin) is wrong.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Jul 2007
    Location
    Grand Junction, CO, USA
    Posts
    2
    So far the replies have been helpful, but I'm still having a hard time understanding how to make my wannabe command line work without breaking.

    Basically, what I am trying to do is create a Sudoku game in a terminal window. Here is what I want the screen to look like.

    Code:
         A   B   C   D   E   F   G   H   I
       *************************************
     A *   | 8 |   *   |   | 2 * 9 |   |   *
       *-----------*-----------*-----------*
     B *   |   | 6 * 5 | 3 | 8 *   |   | 1 *
       *-----------*-----------*-----------*
     C * 2 |   |   *   | 1 |   * 3 |   | 8 *
       *************************************
     D * 9 | 1 |   *   |   | 6 *   | 3 |   *
       *-----------*-----------*-----------*
     E *   |   | 7 *   | 4 | 1 *   |   |   *
       *-----------*-----------*-----------*
     F * 3 | 6 |   *   |   |   * 4 |   |   *
       *************************************
     G * 7 |   | 9 *   | 8 |   *   | 4 | 2 *
       *-----------*-----------*-----------*
     H * 6 |   |   * 1 |   |   *   | 7 |   *
       *-----------*-----------*-----------*
     I * 8 |   | 1 * 2 |   | 3 * 5 |   |   *
       *************************************
    
    Enter command:
    So Q will quit, S will save the puzzle, N will generate a new puzzle, or they can enter the vertical and horizontal coordinates along with a number between 1-9. Zero will delete a number if one is present. So for example AA1 puts a 1 in the box located at vertical A and horizontal A.

    I have already got the puzzle grid to print and any other things I need to show up on the screen I know I can do. Where my problem has been is with the input at the game's pseudo command line. Anybody have any ideas on how to make this work?

    Also, as I've been reading through the K&R book, the FAQs on here, and various other places I have realized it is not possible to do a refresh of a terminal window's screen using anything in the standard library. Having it reprint the puzzle continously down terminal kind of sucks, but if it's the only way then it's the only way.

  9. #9
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    babelosopher a terminal library will make your life a lot easier.

    Windows: PDcurses (Ports to the X-terminal too)
    *nix: nCurses

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    230
    I think the problem is that scanf() reads the next character from the stream, which would be '\n'.

    When you type anything other than Q, then press enter both characters are read by scanf.
    e.g. The first time, it'll read T and only T. Then scanf will check the stream for any characters ready to be taken and find the '\n' you input to tell it to read the T.

    I don't think I explained it in the best way possible, but I hope you understand.

    As for how to fix it, I think another member would be able to help better than I can. I am not that experienced with C yet.

  11. #11
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Personally I wouldent use scanf to get the input, I'd use fgets() instead. By getting th input as a sting first your program won't crash if the user enters something wrong by accident. You could then read the first three characters in sequence to represent something like:

    - x position on grid
    - y position
    - number to change cell to

    For drawing you grid theres also some ascii characters for borders you can use. Heres an example of a basic function I made to draw a grid for a chess board:
    Code:
    #include <stdio.h> 
    
    void DrawBoard()
    {
        for(int y=0; y<18; y++)
            for(int x=0; x<26; x++)
                if(y==0)
                    if(x==25)       printf(" \n");
                    else if(x%3==2) putchar(65+(x/3));   
                    else            putchar(' ');
                else if (y==1)
                    if(x==0)        putchar(' ');
                    else if(x==1)   putchar(218);
                    else if(x==25)  printf("%c\n", 191);
                    else if(x%3==1) putchar(194); 
                    else            putchar(196);
                else if(y==17)
                    if(x==0)        putchar(' ');
                    else if(x==1)   putchar(192);
                    else if(x==25)  printf("%c\n", 217);
                    else if(x%3==1) putchar(193); 
                    else            putchar(196);
                else if(y%2==1)
                    if(x==0)        putchar(' ');
                    else if(x==1)   putchar(195);
                    else if(x==25)  printf("%c\n", 180);
                    else if(x%3==1) putchar(197); 
                    else            putchar(196);
                else  
                    if (x==0)       putchar(48+(y/2));
                    else if(x==25)  printf("%c\n", 179);
                    else if(x%3==1) putchar(179); 
                    else if(x%3==2) printf("  ");//contents goes here
    }  
    
    int main()
    {
        DrawBoard();
        getchar();
    }
    Its only 8*8 so you would need an extra tile in each dim. Also theres some double border chars to which would look good to divide the 3*3 sections. If you want you could find thier codes on an ascii chart.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with a basic scanf procedure.
    By killpoppop in forum C Programming
    Replies: 9
    Last Post: 11-03-2008, 03:39 PM
  2. Replies: 2
    Last Post: 02-20-2005, 12:48 PM
  3. scanf issue
    By fkheng in forum C Programming
    Replies: 6
    Last Post: 06-20-2003, 07:28 AM
  4. Scanf and integer...
    By penny in forum C Programming
    Replies: 3
    Last Post: 04-24-2003, 06: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, 04:34 AM

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