1

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.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 5
    Since you only read two characters from `stdin`, it's likely that the newline character is still in the buffer and gets eaten by the `pause` command. Use `fgets` to read a line of input. – paddy Aug 21 '19 at 02:19
  • Call your program from an already open commandline prompt. Then it will stay open afterwards. – Yunnosch Aug 21 '19 at 06:13
  • @paddy Sounds good enough to be an answer, even one which contradicts the "dupe" feeling I had. – Yunnosch Aug 21 '19 at 06:14
  • 1
    Unrelated to your question, you should always use `strtol` and never `atoi`. The `atoi` is a dangerous function with no error handling. – Lundin Aug 21 '19 at 08:52
  • Read through the suggestions, I'll try them when I get home from work. For @Lundin I searched more into strtol vs atoi and it does seem to be an outdated function which the book is referencing from 2012 when it was published so I'll change that as well - thanks for the correction. – Reese Evans Aug 21 '19 at 15:26
  • before calling `system( "pause" );` always clear out `stdin`, typically via: `int ch; while( (ch = getchar() ) != EOF && ch != '\n' ){;}` – user3629249 Aug 21 '19 at 21:34
  • @ReeseEvans Some guide lines about which standard functions to avoid or be extra careful with: [Which functions from the standard library must (should) be avoided?](https://stackoverflow.com/a/46563868/584518). – Lundin Aug 22 '19 at 06:49

1 Answers1

1

I was able to get it working using the advice provided by paddy to use fgets instead of just printing as my character was being eaten by the buffer while passing. I also launched directly through the command line as my setup for VSCode wasnt done properly and was running into issues itself trying to run the compiled exe.

  • 1
    You will also find using the *VS Developers Command Prompt* and compiling from the command line by invoking the compiler directly (`cl.exe`) allows you to tailor your build simply by specifying options on the command line (in addition to avoiding the need to set up a project in VS) See, e.g. [Use the Microsoft C toolset from the command line](https://learn.microsoft.com/en-us/cpp/build/walkthrough-compile-a-c-program-on-the-command-line?view=vs-2019) – David C. Rankin Aug 22 '19 at 00:46