Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 14 of 14

Thread: C++ Strings

  1. #1
    New Coder
    Join Date
    Aug 2007
    Posts
    74
    Thanks
    3
    Thanked 2 Times in 2 Posts

    C++ Strings

    Hi guys ,

    I have this test code :

    #include <iostream>
    #include <string>

    int main()
    {
    char* data = "";
    std::cin >> data;
    std::cout << strlen(data);
    }

    Please test it on your compiler , What's problem of this code ?
    I just want a code to get a string and show length of that string .

    And oh ! , Need a good web site as C++ reference

    Will help instead very much .

  • #2
    Super Moderator sage45's Avatar
    Join Date
    May 2002
    Posts
    1,064
    Thanks
    0
    Thanked 15 Times in 15 Posts
    Quote Originally Posted by Active X View Post
    #include <iostream>
    #include <string>

    int main()
    {
    char* data = "";
    std::cin >> data;
    std::cout << strlen(data);
    }
    I'm not in front of a compiler right now, but it would be easier to do this:
    Code:
    // string::length
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main ()
    {
      string strLngth="";
      cin >> strLngth;
      cout << "The length of strLngth is " << strLngth.length() << " characters.\n";
      return 0;
    }
    One good reference is: http://www.cplusplus.com/

    -saige-
    Last edited by sage45; 08-21-2007 at 05:29 PM.
    HTML & CSS Forum Moderator

    "If you don't know what you think you know, then what do you know."
    R.I.P. Derrick Thomas #58
    1/1/1967 - 2/8/2000

  • Users who have thanked sage45 for this post:

    Active X (08-23-2007)

  • #3
    Senior Coder TheShaner's Avatar
    Join Date
    Sep 2005
    Location
    Orlando, FL
    Posts
    1,126
    Thanks
    2
    Thanked 40 Times in 40 Posts
    There is a stickied thread with program language references. Here's the one for C++: http://www.codingforums.com/showthre...467#post218467

  • #4
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    Quote Originally Posted by Active X View Post
    Hi guys ,

    I have this test code :

    #include <iostream>
    #include <string>

    int main()
    {
    char* data = "";
    std::cin >> data;
    std::cout << strlen(data);
    }

    Please test it on your compiler , What's problem of this code ?
    You can't cin into a pointer like that. That is the problem, you'd have to allocate some memory first and have the pointer pointing to it. However unless you really need to use a pointer, just use the string class like Sage demonstrated.
    OracleGuy

  • Users who have thanked oracleguy for this post:

    Active X (08-23-2007)

  • #5
    New Coder
    Join Date
    Aug 2007
    Posts
    74
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Hi guys ,

    Thank you very very much
    You really helped .

  • #6
    New Coder
    Join Date
    Aug 2007
    Posts
    74
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Oh ! another problem guys ,

    I have this code :

    #include <iostream>
    #include <string>
    using namespace std;
    int main()
    {
    string myStr = "This is some text I write";
    cin >> myStr;
    cout << myStr.length();
    cin >> "";
    return 0;
    }

    but when I insert a space in the string , program is automatically closed .

    Need your emergency help
    Thank you very very much

  • #7
    Super Moderator sage45's Avatar
    Join Date
    May 2002
    Posts
    1,064
    Thanks
    0
    Thanked 15 Times in 15 Posts
    Quote Originally Posted by Active X View Post
    Oh ! another problem guys ,

    I have this code :

    #include <iostream>
    #include <string>
    using namespace std;
    int main()
    {
    string myStr = "This is some text I write";
    cin >> myStr;
    cout << myStr.length();
    cin >> "";
    return 0;
    }

    but when I insert a space in the string , program is automatically closed .
    Try this:
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    int main()
    {
        string myStr = "This is some text I write";
        cin >> myStr;
        cout << myStr.length();
        cout << "Press any key to continue...";
        cin.get();
        return 0;
    }
    HTH,

    -saige-
    HTML & CSS Forum Moderator

    "If you don't know what you think you know, then what do you know."
    R.I.P. Derrick Thomas #58
    1/1/1967 - 2/8/2000

  • #8
    New Coder
    Join Date
    Aug 2007
    Posts
    74
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Thank you sage , but it didn't work too .

    Test THIS code on your compiler :

    // Main file of this project
    #include <iostream>
    #include <string>
    using namespace std;
    int main()
    {
    string myStr;
    cin >> myStr;
    cout << myStr.length();
    cin >> "";
    return 0;
    }

    and when you want to give it the string variable , Insert a space in your string and see what happens .
    When I do that , IDE terminates the program .

    And the cin >> "" is just to not let the program be closed after run

    Thank you for helping

  • #9
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    Quote Originally Posted by Active X View Post
    Thank you sage , but it didn't work too .

    Test THIS code on your compiler :

    // Main file of this project
    #include <iostream>
    #include <string>
    using namespace std;
    int main()
    {
    string myStr;
    cin >> myStr;
    cout << myStr.length();
    cin >> "";
    return 0;
    }

    and when you want to give it the string variable , Insert a space in your string and see what happens .
    When I do that , IDE terminates the program .

    And the cin >> "" is just to not let the program be closed after run

    Thank you for helping
    You can't cin into a constant, which "" is.

    You can use system("pause") if it is only going to be ran on Windows. Otherwise the more platform independent version would be:
    Code:
    cout << "Press enter to continue..." << endl;
    
    cin.ignore(numeric_limits<streamsize>::max(), '\n');
    cin.get();
    OracleGuy

  • #10
    Gox
    Gox is offline
    Regular Coder Gox's Avatar
    Join Date
    May 2006
    Location
    Ontario, Canada
    Posts
    392
    Thanks
    2
    Thanked 20 Times in 20 Posts
    I don't have the means to test, but my guess is it's the way that cin reads the buffer.

    If cin reads until it finds white-space, i.e. a space then it will stop there, thus leaving the rest of the string in the buffer. If this is true then when your program hits the cin >> "" line it's probably reading the remainder of the string that is still in the buffer rather than waiting for input. Given your symptoms this would make some sense.

    Can someone verify this is how cin works?

    This page suggests at the very least that cin may leave the terminating character in the buffer (i.e. <enter> or newline). Their suggestion is to follow cin with a call to cin.ignore, to ignore the terminating character.
    http://www.cprogramming.com/tips/sho...ount=30&page=0
    It's a good idea to follow cin with cin.get() or cin.ignore() because cin can leave a terminating character in the stream, which could case small problems with your code.
    Again, this sounds to me like the issue. Of course there is the matter of whether you can cin to a constant which was pointed out in the post above mine.
    Last edited by Gox; 08-25-2007 at 10:20 PM. Reason: More Info.

  • #11
    New Coder
    Join Date
    Aug 2007
    Posts
    74
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Hi guys ,

    I'm really confused . Could you write me such a program please :

    Read a string , Get it's length , Just this . But it must also be able to read spaces in the input string .

    Thank you very very much

  • #12
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    Quote Originally Posted by Active X View Post
    cin >> myStr;
    Then instead of doing that, use cin.getline() to take in text including spaces. If you have a C++ book look the function up or you can google it to find the parameters it takes.
    OracleGuy

  • #13
    Super Moderator sage45's Avatar
    Join Date
    May 2002
    Posts
    1,064
    Thanks
    0
    Thanked 15 Times in 15 Posts
    Quote Originally Posted by Active X View Post
    Hi guys ,

    I'm really confused . Could you write me such a program please :

    Read a string , Get it's length , Just this . But it must also be able to read spaces in the input string .

    Thank you very very much
    Active X. The code that has been provided will do everything that you want. The only problem you are having is getting it to stay at the command prompt when ran from the debugger. Either you can add-in the code to accomplish this or you can simply drop to a command prompt, go to the location of the executable and run it from the command line.

    Since this is a console application, once it has executed the return 0 line in the code, any time you attempt to run from the debugger or from within a windows explorer screen, you will get the same result, i.e. - you input a string, the application returns the specified value and then closes the window. If however, you run it from a command line interface (If Windows XP: Start --> Run --> CMD) the window for the command line interface remains because this is the environment that the application was designed to run in and does not provide a call to exit the environment.

    HTH,

    -saige-
    HTML & CSS Forum Moderator

    "If you don't know what you think you know, then what do you know."
    R.I.P. Derrick Thomas #58
    1/1/1967 - 2/8/2000

  • #14
    New Coder
    Join Date
    Aug 2007
    Posts
    74
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Thank you very very much


  •  

    Posting Permissions

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