Results 1 to 12 of 12

Thread: Strings

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2004
    Location
    Middle of nowhere
    Posts
    45

    Strings

    Ok, I started coding in VB... Now that im switching to C++ i cant figure out how to work strings... How do i store something in a string? how do i declare a string?

    In the book im learning from it talks about comparing strings, but hasnt showed how to actauly make a string. Any help would be appriciated.

    ---Flac
    Everything great once started with the words "That will never catch on, you shouldnt even bother" be great, go against the crowed, do something stupid, you might become famous.

  2. #2
    Hyperactive Member Comreak's Avatar
    Join Date
    Feb 2001
    Location
    Dis
    Posts
    319
    Originally posted by Flac
    Ok, I started coding in VB... Now that im switching to C++ i cant figure out how to work strings... How do i store something in a string? how do i declare a string?

    In the book im learning from it talks about comparing strings, but hasnt showed how to actauly make a string. Any help would be appriciated.

    ---Flac
    There are two general types of strings you can work with in C++: char arrays and strings from the standard library (which are very similar to strings in vb).

    A char array is just an array of characters. They generally have to be a predefined size and you can't compare them the way you would compare two strings in VB. Here's some code that shows how you would create and initialize a char array:

    Code:
    //to declare a char array
    char mycstring[4];
    
    //to initialize a char array
    mycstring = "bob";
    Keep in mind that when you declare a char array, you must leave extra space at the end for the terminating character ('\0'). If you don't, you'll get errors.

    Strings from the standard library are very similar to the strings your used to working with in vb. They can do most of the same things as the ones in vb and then some. Here's how you declare one:

    Code:
    #include <string> //need this to get access to std strings
    
    string myString = "whatever";
    Search the forum or read up on how these strings work at MS's MSDN library:

    http://msdn.microsoft.com/library/default.asp

    Hope that clears some things up. Good luck.
    C.O.M.R.E.A.K.: Cybernetic Obedient Machine Responsible for Exploration and Accurate Killing

  3. #3

    Thread Starter
    Member
    Join Date
    Mar 2004
    Location
    Middle of nowhere
    Posts
    45
    dont mean to continue to pester.

    but is there a way i can take input from a user into a string directly? or do i have to convert it somehow?

    (is there a way i can do a std::cin >> string type statement to get a string, I prefer working with strings cause they are simpler to compare values to.)
    Everything great once started with the words "That will never catch on, you shouldnt even bother" be great, go against the crowed, do something stupid, you might become famous.

  4. #4
    PowerPoster sunburnt's Avatar
    Join Date
    Feb 2001
    Location
    Boulder, Colorado
    Posts
    1,403
    sure, the string class has a stream extraction operator;

    Code:
    #include <string>
    #include <iostream>
    
    using namespace std;
    
    // ...
    string sInput;
    cin >> sInput; // read one word;
    cout << "you wrote the word: " << sInput << endl;
    getline(cin, sInput); // read a line
    cout << "you wrote the line: " << sInput << endl;
    // ...
    Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.

  5. #5

    Thread Starter
    Member
    Join Date
    Mar 2004
    Location
    Middle of nowhere
    Posts
    45
    Thanks a lot... But now i have the problem that the getline(cin, variable) code doesnt read the first word in the string... Any suggestions?

    ---Flac
    Everything great once started with the words "That will never catch on, you shouldnt even bother" be great, go against the crowed, do something stupid, you might become famous.

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Have you copied the example literally?
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  7. #7
    Fanatic Member
    Join Date
    Jan 2003
    Posts
    1,004
    Remember that the line is terminated when you hit <Enter>
    "Can't" and "shouldn't" are two totally separate things.

    All questions should be answered. All answers should be true. That is why I post.

  8. #8

    Thread Starter
    Member
    Join Date
    Mar 2004
    Location
    Middle of nowhere
    Posts
    45
    If writin my own code using your example for basis, and copied example literaly(adding in all the stuff needed to make it compile cleanly.)

    Cod entered(not including return, or includes)


    Code:
    int main()
    {
    std::string sInput;
    std::cin >> sInput; // read one word;
    std::cout << "you wrote the word: " << sInput << std::endl;
    getline(std::cin, sInput); // read a line
    std::cout << "you wrote the line: " << sInput << std::endl;
    when a string is entered, for example : Hi there.

    I get
    You wrote the word: hi
    You wrote the line: there

    if i enter "hi there bob" i get
    You wrote the word: hi
    You wrote the line: there bob

    Its cutting out the first word of the string. Any suggestions?

    ---Flac
    Everything great once started with the words "That will never catch on, you shouldnt even bother" be great, go against the crowed, do something stupid, you might become famous.

  9. #9
    PowerPoster sunburnt's Avatar
    Join Date
    Feb 2001
    Location
    Boulder, Colorado
    Posts
    1,403
    That's because the first line:
    Code:
    cin >> sInput;
    reads the first word that you input and leaves the left in the stream to be read later.

    try putting in just one word for the first case; then it will prompt you to enter more in the second case.
    Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.

  10. #10

    Thread Starter
    Member
    Join Date
    Mar 2004
    Location
    Middle of nowhere
    Posts
    45
    I understand the first part getting one word. But is there a way i can make

    getline(std::cin, sInput); get the full inputed line, including the first word?

    As is its taking everything but the first word, why is that?


    ---Flac
    Everything great once started with the words "That will never catch on, you shouldnt even bother" be great, go against the crowed, do something stupid, you might become famous.

  11. #11
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Because once the first part takes the first word, it's no longer there for the second part to take.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  12. #12
    Fanatic Member
    Join Date
    Jan 2003
    Posts
    1,004
    If you comment out the line:

    cin >> sInput;

    it should work to your liking.
    "Can't" and "shouldn't" are two totally separate things.

    All questions should be answered. All answers should be true. That is why I post.

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