Thread: msvc++ and strings ?

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    83

    msvc++ and strings ?

    hi,

    i'm using msvc++ and when i declare a string:
    string my_string;

    shouldn't the word string turn to blue like int or double?

    also, what header file do i use? i've tried <string> and <cstring> and i'm still having trouble with strings.

    lets say i want to get someone's full name as one large string, is it possible? for example i have:
    Code:
    string full_name;
    cout << "enter full name: "; cin >> full_name;
    what happens is that when i type "first last" during run-time, i only output "first"

    how can i get all of it?

    thanks,
    barneygumble742

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by barneygumble742
    i'm using msvc++ and when i declare a string:
    string my_string;

    shouldn't the word string turn to blue like int or double?
    That's only for keywords and primitive data types (ints, floats, etc...)

    Quote Originally Posted by barneygumble742
    also, what header file do i use? i've tried <string> and <cstring> and i'm still having trouble with strings.
    You need to use <string>. Your problems may be due to the namespace issue. To get around this you can do one of three things:

    1)
    Code:
    #include <string>
    
    using namespace std;
    
    ...
    
    string my_string;
    2)
    Code:
    #include <string>
    
    using std::string;
    
    ...
    
    string my_string;
    3)
    Code:
    #include <string>
    
    ...
    
    std::string my_string;
    Of those, #3 is the prefered method while #1 is the easiest for newbs to deal with at this stage.

    Quote Originally Posted by barneygumble742
    lets say i want to get someone's full name as one large string, is it possible? for example i have:
    Code:
    string full_name;
    cout << "enter full name: "; cin >> full_name;
    Using cin along with the stream extraction operator (>>) will stop getting input at the first whitespace (space/tab/newline) so it would likely only get the first name and you would have to "cin >>" into a couple different strings and then concatenate them into one big "name" string. You would need to use the getline function to do it in one statement.

    Code:
    string full_name;
    getline(cin,full_name);
    That would get a full line of input up till the first newline character and stuff it into the full_name string.

    Quote Originally Posted by barneygumble742
    what happens is that when i type "first last" during run-time, i only output "first"

    how can i get all of it?

    thanks,
    barneygumble742
    I just explained what's going on with that up above.
    "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

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    83
    hi,

    thanks for the reply...tons of info.
    the code you provided:
    Code:
    string full_name;
    getline(cin,full_name);
    blew up on me.

    shouldn't it be:
    Code:
    char full_name;
    cin.getline(full_name, # of characters);
    and even when i do that it works normally. however when i implement it into my class, it blows up on me once again.

    thanks.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> shouldn't it be

    No, it shouldn't. If you are using the C++ string class like your original post indicated, you should use the code that uses string.

    Explain what you mean by blew up, and show the code that you tried to run.

  5. #5
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    it seems that getline in MSVC++ has a bad implementation
    http://cboard.cprogramming.com/showt...643#post466246

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    MSVC++6, yes. The problem is solved in later compilers.

    You might have to include <iostream> as well.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Quote Originally Posted by xErath
    it seems that getline in MSVC++ has a bad implementation
    http://cboard.cprogramming.com/showt...643#post466246
    I use MSVC++6 and it doesn't blow up on me. There is a bug in getline(), which can be fixed, but if I remember correctly the bug just makes you have to hit enter twice.

  8. #8
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    Yes, and that is the bug that gets fixed if you follow the link and then the link again.
    STL Util a small headers-only library with various utility functions. Mainly for fun but feedback is welcome.

  9. #9
    Registered User
    Join Date
    Sep 2004
    Posts
    83
    i got that patch for msvc++ and it also solved a problem i had where msvc got stuck at compilation. even after the patch my code still doesn't work. can someone take a look at it for me to see what i'm doing wrong?..if you have time.

    thanks

    Code:
    //file.h
    #include <iostream>
    #include <string>
    using namespace std;
    
    class guitar
    {
    public:
    	guitar();			//default constructor
    	guitar(string name);		//overloaded constructor
    	int input();
    	void output();
    
    private:
    	string name;
    	int year_made;
    };
    Code:
    //file.cpp
    #include <iostream>
    #include "file.h"
    using namespace std;
    
    int main()
    {
    guitar temp;
    temp.input();
    temp.output();
    
    return 0;
    }
    
    int guitar::input()
    {
    	cout << "Enter the make: ";	cin >> name;
    return 0;
    }
    
    void guitar::output()
    {
    	cout	<<	name			<<	endl;		//does not display multiple words. only displays one word.
    }
    
    guitar::guitar(){}

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    The problem is that you only read one word to begin with. Search the forum for "getline".
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  11. #11
    Registered User
    Join Date
    Sep 2004
    Posts
    83
    i just tried:

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main()
    {
    string name;
    	cout << "enter something: ";
    	getline(cin, name);
    	cout << name << endl;
    return 0;
    }
    it prints two words but i have to press enter again...weird.

  12. #12
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    There is a bug in the implementation of the getline function in the string header file.

    Fix to <string>
    The header <string> contains a definition for template function getline. It has a lookahead problem -- typing a delimiter to end the input doesn't return control until you type yet another character. Change the code as indicated by the comment:

    Code:
        else if (_Tr::eq(_C, _D))
            {_Chg = true;
            _I.rdbuf()->snextc();    // replace snextc with sbumpc
            break; }
    Did you make the suggested change?
    "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

  13. #13
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    It makes you hit enter twice? You do realize that such a problem was just mentioned earlier in this thread, right? Look up a couple posts, follow the link, read that thread. Follow the link in there to the fix at dinkumware. You only need to fix <string> and if you're feeling spunky <istream>.

  14. #14
    Registered User
    Join Date
    Jun 2005
    Posts
    8
    There is a CString type, you could always use that I think. Im a noob to VC++ as well

  15. #15
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    No, you couldn't. CString is part of the big MFC framework, and unless you want that in your app...
    CString also doesn't interoperate well with the Standard C++ Library.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Parsing Strings
    By Hunter2 in forum C++ Programming
    Replies: 29
    Last Post: 12-05-2004, 07:48 PM
  2. setting msvc 6 to use unicode strings
    By reanimated in forum Windows Programming
    Replies: 6
    Last Post: 01-03-2004, 10:08 PM