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;
}
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