Why isn't this working? using scanf and stuff

This is a discussion on Why isn't this working? using scanf and stuff within the C Programming forums, part of the General Programming Boards category; I made this code: Code: #include <stdio.h> int main() { char y; do( printf("\nYou can run this program again. Do ...

  1. #1
    jumboman
    Guest

    Why isn't this working? using scanf and stuff

    I made this code:
    Code:
    #include <stdio.h>
    
    
    int main()
    {
      char y;
    
      do(
         printf("\nYou can run this program again. Do you want to?"
                "\nType \"y\" or \"n\" and press enter:");
         scanf("%c", &y);
    
        }while( y != 'n' );
    
      return 0;
    }
    And I get this output:
    [quote]
    Do you want to run the program again?
    Type "y" or "n" and press enter:y

    Do you want to run the program again?
    Type "y" or "n" and press enter:
    Do you want to run the program again?
    Type "y" or "n" and press enter:
    [quote]

    Why is it doing a double print after I type 'y'?
    Can someone quickly re-write it, and tell me why it didn't work?

    Thanx

  2. #2
    Registered User
    Join Date
    Jun 2003
    Posts
    124
    Because it takes the enter as another character after the one you have entered.

    You can ship the enter be writting somthing like:
    scanf( " %c", &var );
    Loading.....
    ( Trying to be a good C Programmer )

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    scanf("%FOO%*[^\n]%*c", &BAR );

    I'm rather fond of that one myself, where FOO is whatever type you're trying to read, and BAR is where you're trying to put it..

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. scanf vs fgets?
    By Matus in forum C Programming
    Replies: 65
    Last Post: 11-17-2008, 03:02 PM
  2. program not working...please look at this
    By JOlszewski in forum C Programming
    Replies: 3
    Last Post: 01-30-2006, 09:33 PM
  3. need help with some basic stuff
    By JOlszewski in forum C Programming
    Replies: 2
    Last Post: 01-22-2006, 01:18 AM
  4. need help with scanf("%*c") stuff.
    By Krupux in forum C Programming
    Replies: 5
    Last Post: 07-24-2002, 03:36 AM
  5. weird scanf()
    By trekker in forum C Programming
    Replies: 1
    Last Post: 06-24-2002, 08:35 AM

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