Results 1 to 7 of 7

Thread: [RESOLVED] cin >> string

  1. #1

    Thread Starter
    Frenzied Member Rick Bull's Avatar
    Join Date
    Apr 2002
    Location
    England
    Posts
    1,444

    [RESOLVED] cin >> string

    I haven't done much with C++ and even less with string.h. I'm trying to get user input into a string, how do I do it? Again here's what I have:

    Code:
    void go()
    {
    	cout << "URI: ";
    	string	uri;
    	cin >> uri;
    }
    Last edited by Rick Bull; Sep 14th, 2002 at 04:42 AM.

  2. #2
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    What doesn't work for you?

    If your string is going to have spaces in it, you're probably going to need to use cin.getline(), but that doesn't work directly with the string header, so...it's been awhile.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  3. #3

    Thread Starter
    Frenzied Member Rick Bull's Avatar
    Join Date
    Apr 2002
    Location
    England
    Posts
    1,444
    It's giving me an error about string being undeclared, so that means it's not in the string header, right? As you can tell, I don't know what I'm doing here

  4. #4
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    How are you including the header? I do:

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    I've seen other people do <iostream.h> and <string.h>, but this is how I've always done it.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  5. #5
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276
    try this:
    Code:
    #include <string>
    #include <iostream>
    using namespace std;
    
    void main(){
    
    	string str;
    	char c;
    
    	while(!cin.eof()){
    		c = cin.get();
    		str += c;
    	}
    	
    	cout << str << endl;
    
    }
    end of file is ^Z (control-Z)

    or you could use a newline char instead.
    Code:
    #include <string>
    #include <iostream>
    using namespace std;
    
    void main(){
    
    	string str;
    	char c;
    
    	while(c != '\n'){
    		c = cin.get();
    		str += c;
    	}
    	
    	cout << str << endl;
    
    }
    You always need to use standard headers.
    Code:
    #include <string>
    #include <iostream>
    using namespace std;

  6. #6

    Thread Starter
    Frenzied Member Rick Bull's Avatar
    Join Date
    Apr 2002
    Location
    England
    Posts
    1,444
    Thanks guys. I'll try that and see if it works. Although I've found with MS Visual C++ that if I use the use namespace std and miss of the .h in the includes it doesn't compile I think VC is a bit ****, because it work in Dev C++ OK.

  7. #7

    Thread Starter
    Frenzied Member Rick Bull's Avatar
    Join Date
    Apr 2002
    Location
    England
    Posts
    1,444
    I got it to work. I think it's because I was putting .h at the end and adding using namespace std. It seems to be OK now I took off the .h bit.

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