CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 24
  1. #1
    Join Date
    Dec 2002
    Location
    La Plata, Buenos Aires
    Posts
    615

    Unhappy why this if statements fails??!?

    Look at the following code which compares for an argument string in the command line:

    Code:
    if (argc <= 1 || argc >= 3)
    		DisplayHelp();
    
    	// interpretar la linea de comandos
    	
    	char* param = new char[strlen(argv[1])+1];
    	strcpy (param, argv[1]);
    
    	if (param == "-s")
    	{
    		cout << "SAVeonly" << endl;
    	}
    	exit(1);
    I enter -s to test in the command line and the if statement fails!!!
    Where I can't compare param == "-s"?? There is anything related to the way i create char*param=new... etc... ???

    Thank you!

  2. #2
    Join Date
    May 2000
    Location
    Phoenix, AZ [USA]
    Posts
    1,347
    param is a char*; that's basically just an integer as far as the
    computer is concerned. Basically, what's happening is that the
    compiler is comparing the addresses of the two pointers. To
    compare char*'s, you must use the function strcmp():
    Code:
    char* str1 = "hi";
    char* str2 = "hi2";
    
    if (strcmp(str1, str2) == 0)
    {
       // strings are equal in here
    }
    else if (strcmp(str1, str2) < 0)
    {
       // str1 is less than str2 in h ere
    }
    else
    {
       // str2 is less than str1 in here
    }
    --Paul

  3. #3
    Join Date
    May 2000
    Location
    Phoenix, AZ [USA]
    Posts
    1,347
    Oh, and as an aside, is there any reason why you're not using
    std::string in lieu of using those char*'s? You've already run into
    memory management problems [I don't see any exception
    handling so if an exception is thrown, param will never be
    delete'd]. Obviously, you've also run into the operator== dilemma
    ... where you can't use an interface which is intuitive.

    std::string solves all of these problems [and more]:
    Code:
    if (argc <= 1 || argc >= 3)
       DisplayHelp();
    
    // interpretar la linea de comandos
    	
    std::string param(argv[1]);
    if (param == "-s")
    {
       cout << "SAVeonly" << endl;
    }
    If an exception is thrown, now you won't have to worry about a
    memory leak; also, you get the syntactic sugar of the operator==
    comparison.

    --Paul

  4. #4
    Join Date
    Dec 2002
    Location
    La Plata, Buenos Aires
    Posts
    615
    Yeah, thanks, i fixed it...

    Im so stupid, I dont know why Im still stick with the char* types instead of std::string type.

    Aynway, there is any advantage in C++ for using char* instead of string????

  5. #5
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470
    Originally posted by indiocolifa
    Aynway, there is any advantage in C++ for using char* instead of string????
    No.
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  6. #6
    Join Date
    Dec 2002
    Location
    La Plata, Buenos Aires
    Posts
    615
    My next (easy.. ) question is how to convert to uppercase a string.

    The following code (of course) wont do it:
    Code:
    string pm = "hello";
    strupr(pm);
    i tried the following and failed (because STRUPR expects a char* not a const char*)

    Code:
    strupr (pm.c_str() );

  7. #7
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725
    Code:
    #include <algorithm>
    
    
    std::transform(pm.begin(),pm.end(),pm.begin(),toupper);
    Last edited by Philip Nicoletti; April 15th, 2003 at 11:33 AM.

  8. #8
    Join Date
    May 2000
    Location
    Phoenix, AZ [USA]
    Posts
    1,347
    There are many, many ways to achieve this. One of the easiest
    to understand might be:
    Code:
    #include <algorithm>
    #include <iostream>
    #include <string>
    
    void convertToUpper(char& toConvert)
    {
       toConvert = toupper(toConvert);
    }
    
    int main(int argc, char* argv[])
    {
       string temp("this is a test string");
       for_each(temp.begin(), temp.end(), convertToUpper);
    
       cout << temp << endl;
    }
    --Paul

  9. #9
    Join Date
    Dec 2002
    Location
    La Plata, Buenos Aires
    Posts
    615

    ive found another similar solution...

    I found in DEVX.com the following code snippet that will do the trick.
    Code:
    std::transform(pm.begin(), pm.end(), pm.begin(), (int(*)(int)) toupper);
    I think its more compact...

    Thank you Paul for your help!

    (also, i dont know if this is good technique, I ve made the following macro to easier use)

    Code:
    #define STRTOUPPER(x) (std::transform((x).begin(), (x).end(),\
    					   (x).begin(), (int(*)(int)) toupper));
    Is this good technique? or bad programming practice? Many sites tell that C Macros are a bad technique in C++.
    Last edited by indiocolifa; April 15th, 2003 at 11:46 AM.

  10. #10
    Join Date
    May 2000
    Location
    Phoenix, AZ [USA]
    Posts
    1,347
    I wouldn't bother with the macro; I'd just use std::transform. If
    the idea of algorithms bothers you [as I thought it would] you
    should wrap the entire thing in an inline function: you'd get all of
    the benefits of the macro, but would still retain the type safety
    of the compiler.

    --Paul

  11. #11
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470
    Don't use macros. Macros are evil. Use an inline if you must, but I would say: keep the original as it is. Anyone who knows their way around STL will immediately recognise the transform call and know what's happening. And if they don't know their way around STL...... there's still time to learn.

    For one thing, if you keep the original form in your code then, should you do something slightly different nearby (say, only transform part of the string), then the correspondance between the two bits of code will be apparent. Of course, you could modify the inline function to do partial strings, but then it starts to look like the original, so what's the point?
    Code:
    string::iterator si1 = s.begin(), si2 = s.end();
    transform(si1, si2, si1, MakeUpper());
    //...
    // Modify si1 and si2 so that they mark out a substring....
    transform(si1, si2, si1, MakeUpper());
    compared to:
    Code:
    UppercaseWholeString(s);
    //...
    string::iterator si1, si2; // a subrange of the string
    transform(si1, si2, si1, MakeUpper());
    and, of course, then you find you need MakeLower.
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  12. #12
    Join Date
    Mar 2003
    Location
    India {Mumbai};
    Posts
    3,871
    What about performance measurements with these STD 'string's. They could'nt serve you any better than core str... counterparts.
    My latest article: Explicating the new C++ standard (C++0x)

    Do rate the posts you find useful.

  13. #13
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470
    Originally posted by Ajay Vijay
    What about performance measurements with these STD 'string's. They could'nt serve you any better than core str... counterparts.
    Are you sure about that?
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  14. #14
    Join Date
    Apr 1999
    Posts
    27,449
    Originally posted by Ajay Vijay
    What about performance measurements with these STD 'string's. They could'nt serve you any better than core str... counterparts.
    Why not write code to test this? Take concatenation for example:

    Make sure that when you write the equivalent of concatenation for the string.h functions you:

    a) determine if the source is large enough to hold the destination.

    b) If not large enough, call malloc() or new[] to allocate the memory.

    c) Copy the source to the destination

    d) Make sure that you call free() or delete[] to clean up the memory when done.

    Now time the above steps with doing an operator += with a std::string. Of course, to do these steps, it would be wise to write a class that wraps the str... functions. But wait a minute -- that's already been done with std::string. So all you would be doing is reinventing the wheel -- a wheel that has been written by some of the best C++ programmers in the business.

    edit :[I also forgot to add that old string.h functions and char * leads to more bugs, especially if that char * is a class member. You have to write a copy constructor and assignment statement for the class to make sure the char * is copied correctly. The std::string can be used safely in a class without writing a copy ctor or assignment functions to take care of the copy.]

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; April 16th, 2003 at 05:22 AM.

  15. #15
    Join Date
    Apr 1999
    Posts
    27,449
    Originally posted by Graham

    Are you sure about that?
    Personally, I believe the old <string.h> functions should be relegated to a small section in the appendix of every C++ programming book.

    Regards,

    Paul McKenzie

Page 1 of 2 12 LastLast

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured