Multiple messages

This is a discussion on Multiple messages within the C Programming forums, part of the General Programming Boards category; This program works okay except it prints the same messages more than once for unmatched letter. Can anyone give a ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    3

    Question Multiple messages

    This program works okay except it prints the same messages more than once for unmatched letter. Can anyone give a solution to this?The programs very easy....
    Code:
    #include<stdio.h>
    int main(void)
    {
        int tries = 0;
        char pass;
        
        
        for(tries = 0;tries<=5;tries++)
    
        {    
            printf("Enter a letter to pass");
    
           pass = getchar();
            
            if(pass == 'b')
            {printf("welcome aboard");
            break;}
            else
            {printf("try again \n");}
            
        }
    
        return 0;
    
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,535
    Perhaps add
    Code:
    else if ( pass == '\n' ) {
      // ignore newlines
    }
    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.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I would change the getchar() line to
    scanf(" %c",&pass);

    Note the space before the %c - that will eliminate your troublesome newline, each time.

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    3
    Well it removed one of the messages...but the first message "Enter a letter to pass" pops up twice as before...

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    3
    I used scanf but the program stops by giving the message "This program has stopped working" message...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting multiple messages on OnReceive() event?
    By Yasir_Malik in forum Windows Programming
    Replies: 2
    Last Post: 05-11-2006, 08:35 AM
  2. MFC: Multiple clicks shows multiple bitmaps - how?
    By BrianK in forum Windows Programming
    Replies: 0
    Last Post: 06-20-2004, 07:25 PM
  3. multiple inputs of multiple types
    By frontz in forum C Programming
    Replies: 8
    Last Post: 01-19-2004, 01:57 PM
  4. Replies: 1
    Last Post: 05-01-2003, 02:52 PM
  5. Messages not getting through
    By PsychoBrat in forum Windows Programming
    Replies: 4
    Last Post: 03-14-2002, 11:27 PM

Tags for this Thread


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