Like Tree3Likes
  • 1 Post By stahta01
  • 2 Post By ZuK

Hello - First post on this forum

This is a discussion on Hello - First post on this forum within the C Programming forums, part of the General Programming Boards category; Hello everyone... I am new to C and I am working for an assignment for school and I was hoping ...

  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
    2,369
    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.
    EssiJoon likes this.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  5. #5
    ZuK
    ZuK is offline
    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
    stahta01 and EssiJoon like this.

  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
    ZuK
    ZuK is offline
    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, 08: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

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