Problem with strncat() function (newbie)

This is a discussion on Problem with strncat() function (newbie) within the C Programming forums, part of the General Programming Boards category; In my search to create a program to convert C++ strings into C chars, I came across this funtion. Code: ...

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    2

    Problem with strncat() function (newbie)

    In my search to create a program to convert C++ strings into C chars, I came across this funtion.
    Code:
    char *strncat(char *string1, char *string2, size_t n) - Append n characters from string2 to stringl.
    Here's what I had so far. This generates quite a few compile errors, and I'm not quite sure what I'm doing wrong, as although I'm a newb in C++, I know almost no C :0

    Code:
    #include <iostream.h>
    #include <stdlib.h>
    #include <lvp\string.h>
    
    char StringToChar(String StringWord)
    {
    
      char CharWord[50], AddOnChar;
      String OneLetterString;
      int Size, Counter;
      Size = StringWord.length();
      if (Size <= 50) {
        for (Counter = 1; Counter <= Size; Counter++) {
          OneLetterString = StringWord.substr(Counter,1);
    
          if (OneLetterString == "A")
            AddOnChar = 'A';
          else if (OneLetterString == "B")
            AddOnChar = 'B';
          else if (OneLetterString == "C")
            AddOnChar = 'C';
          else if (OneLetterString == "D")
            AddOnChar = 'D';
          else if (OneLetterString == "E")
            AddOnChar = 'E';
          else if (OneLetterString == "F")
            AddOnChar = 'F';
          else if (OneLetterString == "G")
            AddOnChar = 'G';
          else if (OneLetterString == "H")
            AddOnChar = 'H';
          else if (OneLetterString == "I")
            AddOnChar = 'I';
          else if (OneLetterString == "J")
            AddOnChar = 'J';
          else if (OneLetterString == "K")
            AddOnChar = 'K';
          else if (OneLetterString == "L")
            AddOnChar = 'L';
          else if (OneLetterString == "M")
            AddOnChar = 'M';
          else if (OneLetterString == "N")
            AddOnChar = 'N';
          else if (OneLetterString == "O")
            AddOnChar = 'O';
          else if (OneLetterString == "P")
            AddOnChar = 'P';
          else if (OneLetterString == "Q")
            AddOnChar = 'Q';
          else if (OneLetterString == "R")
            AddOnChar = 'R';
          else if (OneLetterString == "S")
            AddOnChar = 'S';
          else if (OneLetterString == "T")
            AddOnChar = 'T';
          else if (OneLetterString == "U")
            AddOnChar = 'U';
          else if (OneLetterString == "V")
            AddOnChar = 'V';
          else if (OneLetterString == "W")
            AddOnChar = 'W';
          else if (OneLetterString == "X")
            AddOnChar = 'X';
          else if (OneLetterString == "Y")
            AddOnChar = 'Y';
          else if (OneLetterString == "Z")
            AddOnChar = 'Z';
          else if (OneLetterString == "a")
            AddOnChar = 'a';
          else if (OneLetterString == "b")
            AddOnChar = 'b';
          else if (OneLetterString == "c")
            AddOnChar = 'c';
          else if (OneLetterString == "d")
            AddOnChar = 'd';
          else if (OneLetterString == "e")
            AddOnChar = 'e';
          else if (OneLetterString == "f")
            AddOnChar = 'f';
          else if (OneLetterString == "g")
            AddOnChar = 'g';
          else if (OneLetterString == "h")
            AddOnChar = 'h';
          else if (OneLetterString == "i")
            AddOnChar = 'i';
          else if (OneLetterString == "j")
            AddOnChar = 'j';
          else if (OneLetterString == "k")
            AddOnChar = 'k';
          else if (OneLetterString == "l")
            AddOnChar = 'l';
          else if (OneLetterString == "m")
            AddOnChar = 'm';
          else if (OneLetterString == "n")
            AddOnChar = 'n';
          else if (OneLetterString == "o")
            AddOnChar = 'o';
          else if (OneLetterString == "p")
            AddOnChar = 'p';
          else if (OneLetterString == "q")
            AddOnChar = 'q';
          else if (OneLetterString == "r")
            AddOnChar = 'r';
          else if (OneLetterString == "s")
            AddOnChar = 's';
          else if (OneLetterString == "t")
            AddOnChar = 't';
          else if (OneLetterString == "u")
            AddOnChar = 'u';
          else if (OneLetterString == "v")
            AddOnChar = 'v';
          else if (OneLetterString == "w")
            AddOnChar = 'w';
          else if (OneLetterString == "x")
            AddOnChar = 'x';
          else if (OneLetterString == "y")
            AddOnChar = 'y';
          else if (OneLetterString == "z")
            AddOnChar = 'z';
        }
        *strncat(*CharWord, *AddOnChar, 1);
        //CharWord+=AddOnChar;
        cout << CharWord << endl;
      }
      cout << "Complete";
      getch();
      return(*CharWord);
    }

    Any help would be appreciated

    --Kettch

  2. #2
    Registered User Strider's Avatar
    Join Date
    Aug 2001
    Posts
    149
    Why are you making the task so lengthy? You are using the string.h header from the lvp library. This file is similar to what is used for Visual C++ strings in that it incorporates a c_str() function to handle such a conversion for you. Here is what I mean.
    Code:
    // using Dev-C++ 4.01
    #include <stdio.h>
    #include <stdlib.h>
    #include <lvp/string.h>
    
    int main(int argc, char *argv[])
    {
        String MyString = "My Name is Bubba";
        char MyChar[50];
        strcpy(MyChar, MyString.c_str());
        cout << MyChar << endl;
    
        return 0;
    }
    There is not much else to it.

    David
    One Ring to rule them all, One Ring to find them,
    One Ring to bring them all and in the darkness bind them
    In the Land of Mordor where the Shadows lie.

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    2
    Thanks!! I'm fairly new to C/C++ and I haven't fully explored the libraries.. this was all I could come up with on my own :0

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie problem with scanf function...
    By Mithal in forum C Programming
    Replies: 1
    Last Post: 11-13-2005, 09:28 PM
  2. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 08:10 AM
  3. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  4. Replies: 6
    Last Post: 03-02-2005, 01:45 AM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21