Thread: Just starting

  1. #1
    Registered User
    Join Date
    Jan 2021
    Posts
    9

    Just starting

    Hello everyone I am just starting to learn C programming. I got the following book at a second hand store:

    Head First C
    isbn: 9781449399917

    I know nothing of programming or concepts, but have a great interest in C and programming, and hope to teach myself something new.

    I am doing chapter 1 and I am confused at some of the logic. Please note - I trimmed down the code so it is short, to highlight my question.

    Here is the text book code
    Code:
    /*
     * Program to evaluate face values.
     * Released under the Vegas Public License.
     * (c)2014 The College Blackjack Team.
     */
    #include <stdio.h>
    #include <stdlib.h>
    int main()
    {
     char card_name[3];
     puts("Enter the card_name: ");
     scanf("%2s", card_name);
     int val = 0;
     if (card_name[0] == 'K') {
     val = 10;
     } else if (card_name[0] == 'Q') {
     val = 10;
     } else if (card_name[0] == 'J') {
     val = 10;
     } else if (card_name[0] == 'A') {
     val = 11;
     } else {
     val = atoi(card_name);
     }
     printf("The card value is: %i\n", val);
     return 0;
    }
    Code:
    card_name[0]
    I understand it is looking at the first position in the input string, but I want to do this to check position [1].

    Code:
    // Modified code
    // 
    #include <stdio.h>
    #include <stdlib.h>
    int main()
    {
     char card_name[3];
     puts("Enter the card_name: ");
     scanf("%2s", card_name);
     int val = 0;
     if (card_name[0] == 'K') {
     val = 10;
     } 
     else if ((card_name[0] == 'Q') && (card_name[1] == 'Q')){
         val = 33;
    } else {
     
    val = atoi(card_name);
     }
     printf("The card value is: %i\n", val);
     return 0;
    }
    But it does not seem to be checking position [0] and [1]. What or how is this working?

    Code:
      
     0    1    /0
    +----+----+----+
    |  K |  Q | /0 |
    +----+----+----+
    I hope to have a conversation and be able to understand why the logic does not work, or what I think should work.

    Thank you for your help
    Last edited by kid8n1; 01-27-2021 at 05:55 PM.

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,104
    The diagram you show is for the string "KQ", but your code is looking for "QQ".
    But even if you change the code to this
    Code:
      else if (card_name[0] == 'K' && card_name[1] == 'Q')
    it still won't print 33 because the 'if' condition just before this 'else if' will be true since the first position is 'K'. Maybe you should put this test first.

    Or you could use a "nested if"
    Code:
    #include <stdio.h>
    #include <stdlib.h>
     
    int main()
    {
      char card_name[3];
      puts("Enter the card_name: ");
      scanf("%2s", card_name);
      int val = 0;
      if (card_name[0] == 'K') {
        if (card_name[1] == 'Q') {
          val = 33;
        }
        else {
          val = 10;
        }
      } else {
        val = atoi(card_name);
      }
      printf("The card value is: %d\n", val);
      return 0;
    }
    Last edited by john.c; 01-27-2021 at 07:05 PM.
    The best argument against democracy is a five minute conversation with the average voter. - Churchill

  3. #3
    Registered User
    Join Date
    Jan 2021
    Posts
    9
    That was a typing mistake on my part.

    I tried the following code, and it worked.

    Code:
    if ((card_name[0] == 'q') && (card_name[1] == 'q')) {
        val = 50;
        }
    else if (card_name[0] == 'q') {
        val = 10;
        }
    else {
        val = atoi(card_name);
        }
         printf("The card value is: %i\n", val);
         return 0;
    }
    You were right, the order was not correct. Why is that? Does the logic or if statement stops, since the condition was TRUE?

    I have not tried you 'nested if' yet, since I am not familiar with it. I am working through it, so give me sometime to understand it.

    Thank you for your help, and insight.

  4. #4
    Registered User
    Join Date
    Dec 2017
    Posts
    1,104
    Does the logic of if statement stops, since the condition was TRUE?
    Yes. A chain of "if / else if" will result in only one of the blocks of code controlled by the if's being executed. For example, the following code will only execute one of the printf statements no matter what value you enter for x, even though obviously if x is less than 10 than it is also less than 20 and less than 30.
    Code:
    #include <stdio.h>
     
    int main()
    {
        int x;
     
        printf("Enter x: ");
        scanf("%d", &x);
     
        if (x < 10)
        {
            printf("x is less than 10\n");
        }
        else if (x < 20)
        {
            printf("x is 10 to 19\n");
        }
        else if (x < 30)
        {
            printf("x is 20 to 29\n");
        }
        else
        {
            printf("x is 30 or greater\n");
        }
     
        return 0;
    }
    The best argument against democracy is a five minute conversation with the average voter. - Churchill

  5. #5
    Registered User
    Join Date
    Jan 2021
    Posts
    9
    So it is very important on how you work out the IF/Else logic it seems. In my case, how does one know what comes first or conditions? Maybe it come with practice, but is there any rule of thumb for when trying to test against single and double entries like in my modified code to experiment?

    Also, maybe I need more resources. Do you know of any books that are good for say College 101?

  6. #6
    Registered User
    Join Date
    Dec 2017
    Posts
    1,104
    It's pretty easy to figure out the correct order if you remember that the tests are done in order. So if the first test is for one q and the second is for two, the second will never be reached since if there's two q's there's also one q. As another example, if you have this
    Code:
    if (x < 100)
        printf("less than 100\n");
    else if (x < 50)
        printf("less than 50\n");
    it should be obvious that the second printf will never execute, since if x is < 50 then it will also be < 100.

    Often the tests are not connected in this way and the order doesn't matter, like in your first post. The only order necessary there is that the final 'else' come last, but that's always the case as it's part of the language syntax that a plain 'else' (without an 'if' after it) must come last.

    I don't have any book recommendations. The main thing is to get a relatively modern book, preferably one that covers C++17 or at least C++14 (or, at a bare minimum, C++11).
    The best argument against democracy is a five minute conversation with the average voter. - Churchill

  7. #7
    Registered User
    Join Date
    Sep 2020
    Posts
    79
    @john.c
    why do you mention books about C++ when he wants to learn C ?

  8. #8
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    805
    Quote Originally Posted by kid8n1 View Post
    Also, maybe I need more resources. Do you know of any books that are good for say College 101?
    You need to study a good up to date book on the C Programming Language, (Not C++) cover to cover, and do all the exercises at the end of each chapter! Choose one of the three listed below:

    C Programming, A Modern Approach
    Author: K. N. King

    C Primer Plus, 6th Edition
    Stephen Prata

    C How to Program, 8/e
    Deitel & Deitel

  9. #9
    Registered User
    Join Date
    Dec 2017
    Posts
    1,104
    @thmm, Oops! You're right. I must've had C++ on the brain.
    @kid8n1, Ignore my "recommendations".
    The best argument against democracy is a five minute conversation with the average voter. - Churchill

  10. #10
    Registered User
    Join Date
    Jan 2021
    Posts
    9
    Quote Originally Posted by john.c View Post
    It's pretty easy to figure out the correct order if you remember that the tests are done in order. So if the first test is for one q and the second is for two, the second will never be reached since if there's two q's there's also one q. As another example, if you have this
    Code:
    if (x < 100)
        printf("less than 100\n");
    else if (x < 50)
        printf("less than 50\n");
    it should be obvious that the second printf will never execute, since if x is < 50 then it will also be < 100.

    Often the tests are not connected in this way and the order doesn't matter, like in your first post. The only order necessary there is that the final 'else' come last, but that's always the case as it's part of the language syntax that a plain 'else' (without an 'if' after it) must come last.

    I don't have any book recommendations. The main thing is to get a relatively modern book, preferably one that covers C++17 or at least C++14 (or, at a bare minimum, C++11).
    Ok... understand what you mean. I guess I will need to focus on the logic for my if statements when working with If/Else.

    Thank you.

  11. #11
    Registered User
    Join Date
    Jan 2021
    Posts
    9
    Quote Originally Posted by rstanley View Post
    You need to study a good up to date book on the C Programming Language, (Not C++) cover to cover, and do all the exercises at the end of each chapter! Choose one of the three listed below:

    C Programming, A Modern Approach
    Author: K. N. King

    C Primer Plus, 6th Edition
    Stephen Prata

    C How to Program, 8/e
    Deitel & Deitel
    I will see if I can find these books. I know C is not used a lot to day, and is hard to learn. My school friends say to learn Python, but looking over some of the code, it really does not teach the concepts I am looking for. I do not mind, hard to learn since it will be worth more to learn it. I am sure the if/else logic could be easier to learn in Python, but I will still learn logic with C, and be able to learn memory management as well.

    Thank you

  12. #12
    Registered User
    Join Date
    Jan 2021
    Posts
    9
    Quote Originally Posted by rstanley View Post
    You need to study a good up to date book on the C Programming Language, (Not C++) cover to cover, and do all the exercises at the end of each chapter! Choose one of the three listed below:

    C Programming, A Modern Approach
    Author: K. N. King

    C Primer Plus, 6th Edition
    Stephen Prata

    C How to Program, 8/e
    Deitel & Deitel
    Thank you for the links. I will check these books out.

  13. #13
    Registered User
    Join Date
    Sep 2020
    Posts
    79
    I am sure the if/else logic could be easier to learn in Python
    I don't think it has anything to do with the language, it has more to do about thinking clearly. Python would be easier to learn because you don't need manual memory management, it also has build-in data structures, the same could be said baout C++ too.

  14. #14
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,625
    > I know C is not used a lot to day
    Really?

    Every OS you've ever used.
    Why the C Programming Language Still Runs the World | Toptal

    Or a code quality scanner surveying over 1Bn lines of code.
    index | TIOBE - The Software Quality Company

    Or all the projects on an open source repository.
    Compare Free Open Source Software
    Programming Language
    Java 54,902
    C++ 44,401
    PHP 34,335
    C 32,608
    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.

  15. #15
    Registered User
    Join Date
    Jan 2021
    Posts
    9
    Quote Originally Posted by Salem View Post
    > I know C is not used a lot to day
    Really?

    Every OS you've ever used.
    Why the C Programming Language Still Runs the World | Toptal

    Or a code quality scanner surveying over 1Bn lines of code.
    index | TIOBE - The Software Quality Company

    Or all the projects on an open source repository.
    Compare Free Open Source Software
    Thank you Salem for you inspiration. I did not know C is used and still runs the world. All my friends in school tell me to use Python, because it is so easy; however, I am not worried about it being easy - I want to learn. I wanted to make TI-84 apps and I have read that Arduino runs C applications. Also I wanted to learn about computers. I was watching NASA TV and they mentioned C on one of their satellite or rovers (Cant remember now), and was like "oh neat I want to do that", and I went to a second hand book store with my family and saw a C book.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. starting with C?
    By kk01 in forum C Programming
    Replies: 20
    Last Post: 01-01-2008, 07:03 AM
  2. Starting Out
    By Toireht in forum C Programming
    Replies: 3
    Last Post: 04-30-2006, 03:16 PM
  3. just starting
    By Dr Spud in forum Game Programming
    Replies: 3
    Last Post: 04-26-2006, 05:21 PM
  4. Starting age
    By cboard_member in forum A Brief History of Cprogramming.com
    Replies: 32
    Last Post: 12-23-2005, 11:31 PM
  5. HELP with starting out.
    By sworc66 in forum C Programming
    Replies: 4
    Last Post: 09-05-2002, 10:09 AM

Tags for this Thread