Click here to Skip to main content
15,886,049 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am developing a simple program in C,
but I encountered a problem :
My Code :
Objective-C
#include<stdio.h>
int main()
{
    int menuinpt;
    printf("Welcome to C game!!!!\n");
    printf("1. Play Game\n");
    read:
    printf("Enter a Command to start : ");
    scanf("%d", &menuinpt);
    if (menuinpt != 1)
    {
        goto read;
    }
    if (menuinpt == 1)
    {
        char * name;
        printf("Hello Dude! Looks like you are gonna play this games\n");
        printf("I am Richard your guide of this game.\n");
        printf("Now you gotta tell me your name\n>");
        scanf("%d", &name);
        printf("Okay your name is %d", name);
        getchar();
    }

}

I am using GCC(Code:Blocks) to compile this but whenever I enter "Sid" it says :
Your Name is 48

instead of "Your Name is Sid"
How can I solve this?
Any help would be appreciated......

~Sids123
Posted

1 solution

You're using the wrong format code %d (Scan an integer as a signed decimal number) it should be %s (Scan a character string).
Read here to learn more: http://en.wikipedia.org/wiki/Scanf_format_string[^]

Regards,

— Manfred
 
Share this answer
 
v2
Comments
sid2x 8-Aug-13 6:50am    
Thanks but I'd like to know what does the brackets mean after the declaration like
char name[20];
If I remove the [20] from there, I get a dialog saying :
Game.exe has stopped working.
Thanks for the help again
~Sids123
Maciej Los 8-Aug-13 7:03am    
It means: length of array of char ;)
Return to basics... http://www.learncpp.com/[^]
Maciej Los 8-Aug-13 7:04am    
+5
Manfred Rudolf Bihy 8-Aug-13 8:42am    
Thanks Maciej!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900