Thread: Hello - First post on this forum

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    20

    Hello - First post on this forum

    Hello everyone...

    I am new to C and I am working for an assignment for school and I was hoping I could get some help if possible.

    for some reason this do while loop doesn't work the way I want it to. Even when I enter "A" or "P" it keeps going over and over.

    Can someone please help me with this?

    Thank you very much.

    Code:
      do{ 
      printf("Please enter A (for AM) or P (for PM): ");
      ampm = getchar();
      }while( (ampm = getchar() ) !='A' ||  (ampm = getchar() ) !='P' );

  2. #2
    Registered User
    Join Date
    Apr 2012
    Posts
    20
    Oh... I tried using a scanf("%c", &ampm);

    still no luck

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    20
    and string also

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Try adding a leading space that eats the newlines before the %c

    scanf(" %c", &ampm);

    Code:
    }while( (ampm = getchar() ) !='A' ||  (ampm = getchar() ) !='P' );
    Likely you want instead; see next post.
    Last edited by stahta01; 04-19-2012 at 12:31 PM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #5
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    too many getchar()'s
    try
    Code:
    do{ 
          printf("Please enter A (for AM) or P (for PM): ");
          ampm = getchar();
      }while( ampm !='A'  &&  ampm  !='P' );
    think about it. or doesnt work here. the input cannot be 'A' and 'P' at the same time

    Kurt

  6. #6
    Registered User
    Join Date
    Apr 2012
    Posts
    20
    Thank you Kurt... I greatly appreciate your help. That worked great. My followup question now is when I run that this is what i see...

    Please enter A (for AM) or P (for PM): Please enter A (for AM) or P (for PM):

    I would like to only ask for the input once. Any tips?

  7. #7
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by EssiJoon View Post
    Thank you Kurt... I greatly appreciate your help. That worked great. My followup question now is when I run that this is what i see...

    Please enter A (for AM) or P (for PM): Please enter A (for AM) or P (for PM):

    I would like to only ask for the input once. Any tips?
    This has to do with what you do before that code.
    using
    scanf(" %c", &ampm);
    instead might help. like stahta01 suggested.
    Kurt

  8. #8
    Registered User
    Join Date
    Apr 2012
    Posts
    20
    Thank you to both of you... You are all amazing. That worked great.

    First impressions are so important and this forum really made a wonderful first impression on me.

    Thank you to both of you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. New to C and forum, need some help!
    By Icebreaker9 in forum C Programming
    Replies: 4
    Last Post: 02-10-2009, 09:45 PM
  2. General forum question - if in wrong forum...
    By ulillillia in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 05-14-2007, 05:00 AM
  3. Replies: 7
    Last Post: 09-08-2002, 02:20 PM