I have been reading the head first book about C and I am having a bit of problem with one of the given examples. The example is shown talking about a "code magnet" for a game of blackjack. I went to try out the code and tried to make my own tweaks to it and i don't understand the "errors" I am getting.

Here is the code I have been playing around with:

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

}

Someone, please explain to me what I am getting wrong. I am a bit lost on what my mistakes are. I am new to C for the heads up. (I am only about 3-5 days experienced).

At line 9 change the sign from "&2s" to "%s"

also , it is a better practice to write like
int main(void){} when your not passing any arguments.

when you write even something like main(){..} it works because in C , when ever the compiler sees a name that has not yet been seen , in this case main , followed by an open parenthesis , it assumes it to be a function of a return type int . which is what it would be when you use int main().
and regarding the void , when not passing any arguments to main , its a good practice ( as per K&R ) to pass it a void. because not passing anything makes the compiler turn off all types of parameter checking for the function.

@zeroliken, I have made the change, but i am still seeing an error.

@somjit{}, i will make sure I will follow your techniques and methods (the best that i can).

Unfortunatelly, i am still getting errors...

This is my current code:

#include <stdio.h>
#include <stdlib.h>

int main(void)
{

    char card_name[3];
        puts("Enter the card name: ");
            scanf_s("%s", card_name);
                int val = 0;
                if (card_name[0] == 'K') 
                    {
                        val = 10;
                    } 
                else if (card_name[0] == 'Q')
                    {
                        val = 11;
                    }
                else if (card_name[0] ==  'J')
                    {
                        val = 12;
                    }
                else if (card_name[0] == 'A')
                    {
                        val = 13;
                    }
                else 
                    {
                        val = atoi(card_name);
                    }
                    printf("The card value is: %i\n", val);
                    return 0;

}

Could the errors come from the ways I use "val" in the else if statements?

i don't think there is any function scanf_s in the library. replace scanf_s with scanf and then try to run it. I think everything else is looking fine to me. hope it helps! thanks.

You mean like this?:

#include <stdio.h>
#include <stdlib.h>

int main(void)
{

    char card_name[3];
        puts("Enter the card name: ");
            scanf("%s", card_name);
                int val = 0;
                if (card_name[0] == 'K') 
                    {
                        val = 10;
                    } 
                else if (card_name[0] == 'Q')
                    {
                        val = 11;
                    }
                else if (card_name[0] ==  'J')
                    {
                        val = 12;
                    }
                else if (card_name[0] == 'A')
                    {
                        val = 13;
                    }
                else 
                    {
                        val = atoi(card_name);
                    }
                    printf("The card value is: %i\n", val);
                    return 0;

}

3 problems...

  1. I don't know if this is done correctly (i am a newbie....)
  2. It says that a PDB file is missing.
  3. (a bit off topic) When i compile this code, it keeps on compiling code of another file, not the current one.

Sorry about all my questions, I am just trying to learn this the best that i can.

okay! for the time being, leave all that stuff and use this for your compilation use. Click Here

And i have compiled your code and see results Click Here

Oh okay, i will use that. I am also seeing some additional errors on top of my current errors.

Other errors i am recieving are:
error C2065: 'val' : undeclared identifier

error C2143: syntax error : missing ';' before 'type'

how is it possible ? I have compiled your latest code and it is even giving correct results and I have also put link in my previous to the result. see my above post, and see your own code , it is working correctly.

I see that, but that wasn't what i was hoping to see on visual studios. It says that there was an error, so it compiles the wrong file...

On the link you provided, it shows that it compiles but it doesn't allow me to put an output and it marks the card value as 0. I think i am getting a little confusing (sorry about that, i tend to do that). Any other clues?

no probs! I remeber my time also few years back :p at that link, you will see a link at middle of page "upload with new input" and a button "ideone it!", after adding input, click ideone it!, you will have output infront of you. :-) try it once. hope it helps! thanks.

oh okay i see what's going on. I am going to expand on this code and tweak it a bit more. Do you have any other suggestions in making my code better?

your trying to read a string into card_name , but unlike java or some oop languages , C doesnt by default support strings , a string is a char array much like the one you have here , but , you have to populate the array one char at a time.
like

char c , s[100] ;
int i = 0;
while((c = getchar())!= '\n'){
    s[i++] = c;
}
s[i] = '\0';

this is to give you a basic idea how things work.
once you understand this , and get the hang of stuff , id suggest you take a look at fgets-sscanf combo for taking in string inputs.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.