Thread: Probably simple mistake (newbie)

Threaded View

Previous Post Previous Post   Next Post Next Post
  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

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