Thread: Probably simple mistake (newbie)

  1. #1
    Registered User
    Join Date
    Dec 2023
    Posts
    13

    Probably simple mistake (newbie)

    Hi,

    I just started with C like 1 week ago and Ive never coded in my life so bear with me as I always try to code stuff from the top of my head that Ive already learned so its really just a very simple code ...

    Code:
    #include<stdio.h>
    
    int main(void)
    {
    
      char c;
    
      //ask user
    
      while (!(c == 'y' || c == 'Y' || c == 'n' || c == 'N')) {
        printf("Do you agree?: ");
        scanf("%c", &c);
    
        if (c == 'y' || c == 'Y')
          printf("I agree\n");
    
        else if(c == 'n' || c == 'N')
            printf("I disagree\n");
      }
    
    }
    The purpose of this code is to ask the user if he "Agrees" and if he types in anything else than "Y/y or N/n" it asks the user again in a loop.

    Now the problem is that if you enter any other charakter than "Y/y /N/n" it always prints "Do you agree?: " twice in a row and I cant figure out why.

    Thanks in advance.
    Last edited by Salem; 12-28-2023 at 06:15 AM. Reason: Removed crayola

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Welcome to the forum.

    First thing, when you paste code, make sure you use "copy as text" from your IDE and/or "paste as text" in your browser.
    The colour markup that comes along for the ride from most IDEs is pathetic.

    > char c;
    Initialise your variables.
    If by chance the random initialisation matches your loop exit condition, you're stuck.

    > scanf("%c", &c);
    %c is rather special in the scanf conversions in that it respects white space (which includes newlines).
    So typically you go round your loop twice, once with c='Y' say, and then again with c='\n'.

    If you want to avoid this, add a space to the format.
    Code:
    scanf(" %c", &c);
    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
    Dec 2023
    Posts
    13
    Hi,

    thanks for your reply, such a little error but now it works thanks alot. And sorry for including the code wrong I will do better in the future .

  4. #4
    Registered User
    Join Date
    May 2020
    Posts
    23
    Here's a more concise loop condition

    while (strrchr("YN", topper(c)) == NULL)

    After topper returns c''s value in uppercase, strrchr looks for the uppercase letter in "YN". If it's not there, strrchr returns NULL.

    To use strrchr, you'll need to include ctype.h.

  5. #5
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    "toupper" (note the "u") is in ctype.h, but strchr is in string.h.

  6. #6
    Registered User
    Join Date
    May 2020
    Posts
    23
    Quote Originally Posted by christop View Post
    "toupper" (note the "u") is in ctype.h, but strchr is in string.h.
    Thank you. I should have noticed the typo and corrected it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 01-04-2012, 11:00 PM
  2. some simple c questions iam a newbie
    By scimitar in forum C Programming
    Replies: 3
    Last Post: 07-05-2011, 08:12 AM
  3. Newbie needing simple help
    By ggilmore in forum C Programming
    Replies: 2
    Last Post: 10-29-2009, 03:44 PM
  4. Newbie Mistake?
    By Thegnome in forum C++ Programming
    Replies: 12
    Last Post: 09-28-2007, 08:10 AM
  5. VERY simple mistake
    By blankstare77 in forum C++ Programming
    Replies: 6
    Last Post: 07-30-2005, 02:17 PM

Tags for this Thread