Thread: reading a character string

  1. #1
    Registered User
    Join Date
    Oct 2002

    Unhappy reading a character string

    char lots[18]={'1', '2', '3', '4', '5', '6', '7', '8', '9', '10E', '10N', '10S', 'BB', 'CC', 'EE', 'GG', 'LL','EOB'};

    char num[2];
    int number, x, size;

    printf("Enter parking permit number (1 through 999 inclusive):\t");
    scanf("%s", &num);
    if(number <=0 || number >=1000){
    printf("\nInvalid number entered\n");
    printf("it works");
    printf("\"%s\"\n", lots);
    printf("This sting is %i characters long.\n", size);
    for(x=0; x<size; x++)
    printf("lots[%i]='%c'\n", x, lots[x]);


    I dont know what im doing wrong ity doesn't output the character string could some1 please help me.

  2. #2
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Use code-tags

    Also, why you take the number as a string? dude, declare it as an integer and than take the number... you don't need to atoi() and so on... or else, if you still want this way, change the size of the buffer, it's small, put something like 10 or 20, or if you need more exactly 4-5.
    Last edited by Vber; 03-08-2003 at 11:38 AM.

  3. #3
    Casual Visitor
    Join Date
    Oct 2001
    char lots[18]={ '10E', '10N', '10S', 'BB', 'CC', 'EE', 'GG', 'LL','EOB'};

    This isn't right. A character array as you're using it would be a single char. '1', '0', 'E' etc.

    char lots[18] = "10E10N10SBB...";

    char lots[][20] = { "1", "2", .... , "10E", "10N" ... };

    char *lots[] = { "1", "2", .... "10N" };

    Check your string in num[] before using it as you could overflow the array. fgets(num, sizeof(num), stdin);

    if(number < 1 || number > 999) ...

    printf("\"%s\"\n", lots); isn't right either. That is the format for a single string... not an array of strings.

    printf("%s", lots[index]); for an array of strings
    printf("%c", lots[index]); for an array of chars

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. Interpreter.c
    By moussa in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 05:59 PM
  3. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  4. Remove character from string
    By nooksooncau in forum C Programming
    Replies: 11
    Last Post: 06-05-2006, 09:37 AM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM