Thread: variables problem ! (please help a noob)

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    4

    variables problem ! (please help a noob)

    Hi guys,
    I am new on this forum and I'm a beginner in programming in C and today I feel the need to ask for your help because I am stuck on something that really annoys me a lot.
    I have an issue concerning variables that keep letters (sorry if the terms are not correct, I am french and I don't know how it is called in english). I use code blocks IDE
    Anyway, have a look on this and please tell me why the "error..." message appears TWICE when I don't press 'r' but an other letter instead!?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main()
    {
        char menu=0;
    
        printf("press 'r' \n");
        scanf("%c", &menu);
        while (menu !='r')
        {
            printf("Error!! \n ");
            scanf("%c", &menu);
    
        }
      return 0;
    
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    If you type in say
    x<return>

    Your scanf with %c is going to fetch 'x' on the first call, and then '\n' on the second call. Both of which result in the error message being printed.

    If you always use fgets() to read a line of input, then use sscanf() (or anything else you like) on the resulting buffer, you'll have a lot less difficulty in dealing with a lot of the less obvious side effects of using raw scanf calls.
    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.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    4
    ok, thx for your answer. Could you write the code the way it is supposed to be please so I can understand clearly what you say?
    I am just starting programming so these terms are quite obscure to me for the moment
    Last edited by bingofuel; 03-05-2011 at 10:47 AM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Try adding after each scanf

    printf("The key pressed is %c (%d)\n", menu, menu );
    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.

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    4
    I did what you told me but look what it does:

    Code:
    press 'r'
    i
    the key pressed is i (105)
    error!!
     the key pressed is 
    (10)
    Error!!

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Good - do you understand now where the two characters are coming from (and what they are)?
    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.

  7. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    4
    sorry I don't know

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    So rather than typing in a single letter, try a string like
    hello<return>

    or
    this is wrong<return>
    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.

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by bingofuel View Post
    sorry I don't know
    FWIW... Salem is showing you how to add diagnostic statements to your code, to help discover what the problem is. They're not intended to fix anything, their job is to reveal the errors for you. When the program is complete, you can remove them.

    You already know that what you got is not what you want... Follow Salem's advice to find out why.

    (Hi Salem!)

  10. #10
    Registered User
    Join Date
    Mar 2011
    Posts
    2
    Im just going to state that you shouldnt use while in this situation, instead use if/else.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Noob problem - function call
    By Ultraman in forum C++ Programming
    Replies: 4
    Last Post: 05-20-2006, 02:28 AM
  2. Noob problem with "for" statement
    By Ultraman in forum C++ Programming
    Replies: 4
    Last Post: 05-15-2006, 09:26 PM
  3. noob problem with a number generator
    By Amoreldor in forum C Programming
    Replies: 14
    Last Post: 03-10-2005, 10:39 AM
  4. Replies: 6
    Last Post: 03-06-2005, 02:43 PM
  5. Replies: 4
    Last Post: 10-17-2002, 10:09 PM