I'm following along the Head First C (2012) and am running into an issue with their initial "cards.c" project in the first chapter.
Basically, the code asks for an input for a type of card then outputs a value based on whatever the user entered. However, I find that the command prompt exits automatically after executing the corresponding if statement even if I tell the program to pause or check for a key input at the end of the main() function.
I've made a half-fix by including the output of the card's value within the if statement such as
if (card_name[0] == 'K') {
val = 10;
printf("The card value is: %i\n", val);
system("pause");
}
Yet I find the output will occur twice if I leave the printf statement and system pause both within the for bracket and at the end of main. As such, I'm confused why the command prompt is automatically closing when I only output at the end of main rather than inside of each if/else if/else statement.
For reference, I am running through Visual Studio Code on Windows 10 and am using MinGW to compile (Which VSCode allows me to build with, but I run through a command prompt).
#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);
system("pause");
return 0;
}
For anyone reading, this is exactly how the code appears in the book outside of the system pause at the end of main(). I apologize if this is too basic of a question, but I have yet to find an answer after an hour of searching on this site.