Thread: Mark Virtue question about while loop

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    69

    Mark Virtue question about while loop

    I have put a comment in my code. Please answer this in as much detail as you can. This question has been bugging me. I wish he said the answer in his video.

    Code:
    #include <stdio.h>
    
    
    main()
    {
          char        type;
          int         album; /* boolean */
          
          while (type != 'a' && type != 's')
          {
                printf("Album or single (a for album, s for single)? ");
                fflush(stdin);
                scanf("%c", &type);
                album = type == 'a';
                if (type != 'a' && type != 's') /* How come this has to be an and condition? Why wont the or condition work? */
                    printf("Error\n");
          }
          
          if (album)
             printf("Album\n");
          else
              printf("Single\n");
          
          fflush(stdin);
          getchar();
          
    }
    Thank you very much for any help you can give.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Because if it was one thing, it still isn't the other thing, and therefore entirely false. If you want it to be true when it was neither thing, that's completely different.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    37
    because you want to show an error only when the user gives an input that is not album and also not single.
    If you would put or there it will actually don't show an error never.
    Let's assume there is an or there. then the user inserted 's' (you don't want to give an error). it reaches the if statement and then asks "I am 'a'?" and the answer is not and then it gives an error.

  4. #4
    Registered User
    Join Date
    Dec 2011
    Posts
    69
    Thank you for your answers, I can't believe I didn't realise that. I now see how it works.

    -Cheers.

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675

  6. #6
    Registered User
    Join Date
    Dec 2011
    Posts
    69
    Quote Originally Posted by rags_to_riches View Post
    Can you please explain in more detail why I shouldn't use it? What's bad about it?

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by inu11byte View Post
    Can you please explain in more detail why I shouldn't use it? What's bad about it?
    See the red part of the message (#5)... well, that's a link... click on it and read...

    And FWIW... watching videos is NOT how you learn to program. Get a proper manual and go through it page by page compiling and working with all the examples as you go... People learn by doing, not by watching.

  8. #8
    Registered User
    Join Date
    Dec 2011
    Posts
    69
    Quote Originally Posted by CommonTater View Post
    See the red part of the message (#5)... well, that's a link... click on it and read...

    And FWIW... watching videos is NOT how you learn to program. Get a proper manual and go through it page by page compiling and working with all the examples as you go... People learn by doing, not by watching.
    Sorry, I guess I was tired and didn't realise it was a hyper-link. I do the examples as I am watching. I can't learn by reading, only by seeing and doing.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Not to mention the lousy S/N ratio - like 100MB of low quality video to get about 1K of useful information.
    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.

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by inu11byte View Post
    Sorry, I guess I was tired and didn't realise it was a hyper-link. I do the examples as I am watching. I can't learn by reading, only by seeing and doing.
    In all due respect, that is a tragic flaw in a programmer... How are you going to read the documentation for things like C Library functions and Windows API? The Standard C Library is a couple of hundred functions and the Windows API pushes 30,000 and I can pretty much guarantee there's no videos for any of it.

    Programmers spend a significant amount of their time "lookin' stuff up" to verify the syntax of function calls. In fact the documentation for the C Library is bigger than the library itself...

  11. #11
    Registered User
    Join Date
    Dec 2011
    Posts
    69
    Quote Originally Posted by CommonTater View Post
    In all due respect, that is a tragic flaw in a programmer... How are you going to read the documentation for things like C Library functions and Windows API? The Standard C Library is a couple of hundred functions and the Windows API pushes 30,000 and I can pretty much guarantee there's no videos for any of it.

    Programmers spend a significant amount of their time "lookin' stuff up" to verify the syntax of function calls. In fact the documentation for the C Library is bigger than the library itself...
    I'm only watching videos for the starting part. Once I understand all the basics and the mastered a reasonable understanding of the language, I wont mind looking up functions. I learned Python and did the same with that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strange question mark at EOF
    By Babkockdood in forum C Programming
    Replies: 5
    Last Post: 06-09-2010, 10:43 AM
  2. Loop to mark a string as NULL will not work.
    By m88g88 in forum C Programming
    Replies: 1
    Last Post: 02-21-2010, 07:23 PM
  3. Usage of a Question Mark
    By thetinman in forum C++ Programming
    Replies: 8
    Last Post: 10-01-2008, 01:08 PM
  4. Let the OP mark a thread solved
    By siavoshkc in forum A Brief History of Cprogramming.com
    Replies: 20
    Last Post: 07-20-2006, 12:16 PM
  5. Writing A Dos Shell AkA Win 3.11 Mark 2 :p
    By Arius Myst in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 06-22-2002, 04:45 PM