Noob having trouble storing strings

This is a discussion on Noob having trouble storing strings within the C++ Programming forums, part of the General Programming Boards category; Hi, Im doing this tutorial that was made for a slightly different program (im using MS Visual Studio) and my ...

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    9

    Unhappy Noob having trouble storing strings

    Hi,
    Im doing this tutorial that was made for a slightly different program (im using MS Visual Studio) and my code doesn't work:
    I can't store stuff in a string!
    Code:
    #include <iostream.h>
    #include <string.h>
    #include <sstream>
    
    using namespace std;
    
    
    string name = "";  // " " means an empty string! ;D
    
    int main(void)
    {
    	cout<< "What is your name?\n";
    	cin>> name;
    	cout<< "Hello " << name.c_str();
    return 0;
    }
    I don't know where I have gone wrong/what is wrong with MS VS

    Any ideas??

  2. #2
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    You haven't used <sstream>.

    Don't use global variables.

    I'm fairly certain you don't need to use the c_str() function for that.

    Most importantly, what is your compiler telling you? You should learn to understand compiler error messages, and we'll be better able to help you if we know the error.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  3. #3
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    >>#include <string.h>
    #include <string>

    <string> and <string.h> are two completely different things. <string> contains the C++ string class, whereas <string.h> contains the C-string functions.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  4. #4
    Registered User
    Join Date
    Nov 2003
    Posts
    9
    I haven't used <sstream>?!
    How do I use it?!

    What are global variables?

    The compiler's error message says that it isn't a valid right-hand operator (totally of topic)!

    thanks anyway

  5. #5
    Registered User
    Join Date
    Nov 2003
    Posts
    9
    I got rid of the ".h" in #include <string.h> but still comes up with the error:

    error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)


  6. #6
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Change <iostream.h> to <iostream>.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  7. #7
    Registered User
    Join Date
    Nov 2003
    Posts
    9

    Talking YAY!


    Thanks xSquared
    it Works!

    Some day I wish to be as leet as you.....

    ps thanx joshdick

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Storing strings in a linked list
    By dws90 in forum C Programming
    Replies: 1
    Last Post: 02-21-2009, 06:06 PM
  2. storing strings in arrays
    By smp in forum C Programming
    Replies: 6
    Last Post: 12-16-2008, 08:37 AM
  3. help with fopen() and strings. i'm a noob.
    By xhoangx in forum C Programming
    Replies: 3
    Last Post: 12-04-2005, 07:40 PM
  4. file saving: trouble with strings and strncpy()
    By psychopath in forum C++ Programming
    Replies: 10
    Last Post: 09-17-2005, 10:26 PM
  5. array of strings + more
    By null in forum C Programming
    Replies: 10
    Last Post: 10-01-2001, 03:39 PM

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