Thread: string problem

  1. #1
    Registered User Ivory348's Avatar
    Join Date
    Oct 2019
    Posts
    75

    string problem

    Adding the '-' doesn't work. Please advise. Thank you.

    char keys[100][30];
    . . .
    Code:
    void printData(void)
    {
        int x = 0;
        strcat(keys[x],"-");
        for(x = 0;x<cntr+1;cntr++) puts(keys[x]);
    }

  2. #2
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    338
    Code:
        strcat(keys,"-");
        //keys[x] = '-'
    Last edited by Structure; 01-28-2020 at 09:31 AM.
    "without goto we would be wtf'd"

  3. #3
    Registered User Ivory348's Avatar
    Join Date
    Oct 2019
    Posts
    75
    Quote Originally Posted by Structure View Post
    Code:
        strcat(keys,"-");
        //keys[x] = '-'
    No good I am afraid.
    Last edited by Ivory348; 01-28-2020 at 10:48 AM.

  4. #4
    Registered User Ivory348's Avatar
    Join Date
    Oct 2019
    Posts
    75
    Quote Originally Posted by Ivory348 View Post
    No good I am afraid.
    I retract my query. I have solved the problem by changing the code preceding what was displayed as the query code.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You don't tell us what's in keys to begin with, and
    strcat(keys[x],"-");
    is only going to append a - to the FIRST one anyway.

    > void printData(void)
    A poorly named function if it goes around modifying things.

    What if you want to call it several times?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Nov 2018
    Location
    Amberg in upper palatinate, Bavaria
    Posts
    66
    Hallo Ivory348

    Look at line 5:
    [code]
    for(x = 0;x<cntr+1;cntr++) puts(keys[x]);
    [code]
    The integer 'x' has always the same content.
    It can't get bigger.
    The validity of 'x' is only for the function printData:
    example:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    #define NUMBER_OF_KEYS 15
    
    void printData(char keys[NUMBER_OF_KEYS][30])
     {
      int x = 0;
               
        for(x = 0; x < NUMBER_OF_KEYS; x++) 
        {
         strcat(keys[x],"-");    
         puts(keys[x]);
        }
     }
    
    void printData_b(char keys[NUMBER_OF_KEYS][30], char *test)
     {
      int x = 0;
               
        for(x = 0; x < NUMBER_OF_KEYS; x++) 
        {
         strcat(keys[x],test);    
         puts(keys[x]);
        }
     }
    
    int main(int argc, char **argv)
    {
     char keys[NUMBER_OF_KEYS][30] = {0};
    
     printf("strcat-test\n");
        
     printData(keys);
     
     printData_b(keys, "Tica'ti'bu");
        
     return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-01-2013, 10:11 PM
  2. Replies: 0
    Last Post: 04-27-2013, 06:36 AM
  3. Replies: 1
    Last Post: 04-27-2013, 04:36 AM
  4. Replies: 22
    Last Post: 07-28-2011, 01:26 PM
  5. Problem comparing string from text file with string constant
    By XenoCodex Admin in forum C++ Programming
    Replies: 3
    Last Post: 07-25-2002, 10:17 AM

Tags for this Thread