String conversion

This is a discussion on String conversion within the C++ Programming forums, part of the General Programming Boards category; I am having trouble simply using strcmp, I've never had this problem before. All I want to do is something ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    9

    String conversion

    I am having trouble simply using strcmp, I've never had this problem before. All I want to do is something like this...


    Code:
    char talk[100];        // Also tried String *talk
    
    if (strcmp(talk, "Hello"))
    What's wrong with that? It keeps complaining, saying:

    error C2664: 'strcpy' : cannot convert parameter 2 from 'System::String __gc *' to 'const char *'

    It's like the two aren't at all compatible. I've done this many times before with no problems. By the way, this is a C++ Windows Forms Application in Visual Studio.NET if that matters.
    Last edited by tazz25; 08-16-2006 at 09:35 AM.

  2. #2
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712
    Plese use code tags!
    Didi you include <cstring> header?
    Also your error is about strcpy, this is for string copying in C...
    Please post the whole code!
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  3. #3
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,472
    You also seem to have some problem with how your compiler is set up. System::String is managed C++. Not ISO C++.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  4. #4
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    >> It's like the two aren't at all compatible. I've done this many times before with no problems. By the way, this is a C++ Windows Forms Application in Visual Studio.NET if that matters.

    The actual name "Windows Forms Application" denotes managed C++. At least it does in 2003 (ISO C++ Forms apps are called something else).

    strcmp() cannot be used with managed code, to my knowledge.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  5. #5
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,472
    http://msdn2.microsoft.com/en-us/lib...em.string.aspx is the description of the system::string class. There you can find several compare and copy functions. This thread should probably be moved to the managed C++ forum
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  6. #6
    Registered User
    Join Date
    Mar 2006
    Posts
    9
    OK, let me explain better. Sorry, I'm just frustrated. I need to make a C++ app in Visual Studios that allows me drag-and-drop capability with objects like Textboxes, Buttons, etc. I have a 2 year programming degree but our teacher hated Windows so much that he would not teach us how to make a Windows app, only a console app, so I'm having to learn by myself.

    The kind of app that I found in VS that has drag-and-drop is a Windows Forms Application (.NET)
    Question 1: Is there a better one I should be using? (This is VS 2003 by the way.)

    And I did #include <cstring> in stdafx.h. I've tried strings and char arrays. I will be manipulating pieces of text a lot in this program, so...
    Question #2: Should I use char arrays or strings?

    I thought the compiler might be complaining way too much because I'm making a Windows Forms Application, which I've never done before. (I'm used to only console apps.)
    Question #3: Does this type of App make managed code automatically? If so, Is it a good idea to make it unmanaged? All of this stuff is new to me.
    Last edited by tazz25; 08-16-2006 at 09:38 AM.

  7. #7
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,472
    Q1: I don't know if Visual Studio 2003 offers RAD development for win32 apps developed on ISO C++. But Visual Studio 2005 does. Have you tried to start a win32 window project? Is there that option?

    Q2: You should really use strings with C++ if you can. Character arrays should be considered only as a second option.

    Q3: Highly debatable. If you are not after C++ in particular, but instead looking for a programming language to develop windows applications, I personally don't see a reason for you not to choose .Net. If, on the other hand, you prefer to cling to your knowledge of C++ then you must drop C++/CLI or C# and build unmanaged code in straight ISO C++

    EDIT: Actually I went back to confirm over the web, and it seems VS2005 doesn't offer RAD for ISO C++ development. I'm honestly not sure on this bit. But at least, if VS2003 doesn't either, Borland offers it.
    Last edited by Mario F.; 08-16-2006 at 10:18 AM.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    2

    Lightbulb

    u can simply use CString instead of all that just:

    CString s;
    .......
    s.CompareNoCase("Hello");

    this will return 0 if the 2 strings (s and "Hello") are identical, <0 if s is less than "hello", and >0 if s greater than "hello"

  9. #9
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630
    Whats the difference betwen string an CString?

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,325
    CString is an MFC class. It makes sense to use it if you are using MFC, otherwise, you should use string, which is part of the standard library.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  2. Replies: 4
    Last Post: 03-03-2006, 01:11 AM
  3. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 01:45 PM
  5. Again Character Count, Word Count and String Search
    By client in forum C Programming
    Replies: 2
    Last Post: 05-09-2002, 11:40 AM

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