String in C++

Sharky Forums


Results 1 to 6 of 6

Thread: String in C++

  1. #1
    Goldfish qwe's Avatar
    Join Date
    Sep 2002
    Location
    London
    Posts
    69

    String in C++

    I am trying to use a function to save a word to a global string, depending on what the user input was.
    the code goes like this:

    Code:
    char g_string[255]; //declare global sting
    int main(void)
    {
    //main program
    }
    
    int changestring(int y)
    {
       switch (y)
       {
        case 1:
              {
                strcpy (g_string, "word to be used");
                break;
              }
        case 2:
              {
               strcpy (g_string, "other word");
               break;
              }
       }
    return 0;
    }
    When I compile, I get the message 'strcpy' is 'unidentified'
    what am I doing wrong?
    please help.
    thanx.
    Possesions never meant anything to me,
    I'm not crazy.
    Well that's not true I have a bed,
    and a guitar and a dog named dog.

  2. #2
    NullPointerException rock's Avatar
    Join Date
    Sep 2000
    Location
    York, PA
    Posts
    6,203
    I think you need to #include <string.h>

    Open Source is free like a puppy is free.

    It's only when you look at an ant through a magnifying glass on a sunny day that you realise how often they burst into flames.

    Understanding Evolution

  3. #3
    Stormtrooper Mod Pinky's Avatar
    Join Date
    Oct 2000
    Posts
    2,971
    Yep, you need either

    #include <string.h>
    or
    #include <string>
    using namespace std;


    Also, you can try using the actual C++ string type as well instead of using an array. Just declare some variable string variablename.

    Proofread carefully to see if you any words out.

    [the rules]

  4. #4
    Goldfish qwe's Avatar
    Join Date
    Sep 2002
    Location
    London
    Posts
    69
    Thanx guys, I'll try that
    Possesions never meant anything to me,
    I'm not crazy.
    Well that's not true I have a bed,
    and a guitar and a dog named dog.

  5. #5
    Mako Shark dighn's Avatar
    Join Date
    Nov 2000
    Location
    Vancouver, BC Canada
    Posts
    3,171
    Originally posted by Pinky
    Yep, you need either

    #include <string.h>
    or
    #include <string>
    using namespace std;


    Also, you can try using the actual C++ string type as well instead of using an array. Just declare some variable string variablename.
    just a nitpick but <string> in c++ is for the class std::string, for c string functions like the ones found in c's string.h, use <cstring>
    .

  6. #6
    Reef Shark Nephalim's Avatar
    Join Date
    Nov 2000
    Location
    Michigan
    Posts
    463
    I think the problem lies in this little distraction -> g_string
    :P sorry... too much coffee this morning... too little food.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •