strings in c++

This is a discussion on strings in c++ within the C++ Programming forums, part of the General Programming Boards category; Hi, This is what I am used to from C. Code: void func1(char *str); int main () { char str[80]; ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    223

    Unhappy strings in c++

    Hi,

    This is what I am used to from C.

    Code:
    void func1(char *str);
    
    int main ()
    {
          char str[80];
       
          strcpy(str, "Hello Friend");
          func1(str);
    
          cout << str;
    
          return 0;
    }
    
    void func1(char *str)
    {
         while(*str)
         {
              if (isupper(*str))
              *str = tolower(*str);
    
             else if (islower(*str))
             *str = toupper(*str);
    
             str++;
          }
    }

    Now that I am looking into and learning C++ I think I read somewhere that strings are easier to use. Can anyone assist?

    Thanks

  2. #2
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    There are a lot of tutorials if you google.

    To give you the code with strings to convince you that strings are easier in C++:
    Code:
    using namespace std;
    void func1(char *str);
    
    int main ()
    {
          string str;
       
          str = "Hello friend";
          func1(string);
    
          cout << str;
    
          return 0;
    }
    
    void func1(string& str)
    {
         for(i=0; i<str.size(); ++i)
         {
              if (isupper(str[i]))
              str[i] = tolower(str[i]);
    
             else if (islower(str[i])
             str[i] = toupper(str[i]);
          }
    }

    Just keep these in mind:
    1) You don't have to give a size. Everything is done automatically for you
    2) You can use = to copy, don't need strcpy(). You can use + combine strings
    3) A lot useful functions http://www.cppreference.com/wiki/string/start

    EDIT: (hope nobody realized what I edited )
    Last edited by C_ntua; 10-06-2008 at 05:24 AM.

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    223

    Talking

    Excellent! Thank you.

    I see they are much, much better.

    Howver, I have just used one in conjunction with a getchar() immediately after.
    It meant that the getchar() didnt.

    1. Can anyone know why this happens?
    2. What can be done to overcome?

    Thanks

  4. #4
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    didn't what?
    getchar() gets a char from the standard input. I don't see what that has to do with strings

    1) You use [] with strings as you do with arrays
    2) You can make a string into a char* with string.c_str()

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    223
    Code:
    void displayText (string str1)
    {
    	cout << str1;
    	getchar ();
    	system("cls");
    }
    After I pass in this string and display it. The program doesnt wait for user to press a key.

  6. #6
    Registered User
    Join Date
    Nov 2006
    Posts
    223
    Its ok. It appears that the getchar() is accepting the ENTER from a previous user input within the main. Thus I have just slipped an fflush(stdin); in before the getchar() is called.

    thanks for help

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,955
    It appears that the getchar() is accepting the ENTER from a previous user input within the main. Thus I have just slipped an fflush(stdin); in before the getchar() is called.
    fflush(stdin) is undefined as fflush() works on output streams. Use one of the methods for "flushing" the input stream as described in the FAQ.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Nov 2006
    Posts
    223
    Ok. Thanks.

    But the FAQ doesn't offer a soln, just:

    "So, if the file stream is for input use, as stdin is, the behaviour is undefined, therefore it is not acceptable to use fflush() for clearing keyboard input.
    As usual, there are some exceptions, check your compiler's documentation to see if it has a (non-portable) method for flushing input. "

    Can anyone offer an alternative for fflush?

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,955
    Try this FAQ: How do I... Flush the input buffer.

    Actually, there's a possibly better solution:
    Code:
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    std::cin.get(); // for the "pausing" effect that you want
    You should #include <limits> to std::numeric_limits.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by C_ntua View Post
    Code:
    using namespace std;
    void func1(char *str);
    EDIT: (hope nobody realized what I edited )
    One more thing to edit... You forgot to change the prototype to string& also.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  11. #11
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,788
    Though it's likely a little bit advanced for you, just to show you a tiny bit of what's possible down the road if you continue to explore the language here's an example of one possible solution to your code that demonstrates the use of what is known as a function object and a helper function called transform (in the <algorithm> header)... just to give you an idea of the myriad different ways that this can be done.
    Code:
    #include <string>
    #include <iostream>
    #include <algorithm>
    #include <cctype>
    
    struct flipcase
    {
        inline char operator()(char in) const
        {
            if(islower(in)) return toupper(in);
            else return tolower(in);
        }
    };
    
    int main()
    {
        std::string str("Hello My Friend");
    
        std::cout << "Before: " << str << std::endl;
    
        std::transform(str.begin(),str.end(),str.begin(),flipcase());
    
        std::cout << "After : " << str << std::endl;
    
        return 0;
    }
    Output should be:
    Code:
    Before: Hello My Friend
    After : hELLO mY fRIEND
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 02:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 07:07 PM
  3. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  4. damn strings
    By jmzl666 in forum C Programming
    Replies: 10
    Last Post: 06-24-2002, 02:09 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM

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