From: James Dennett on 4 Dec 2006 02:17 cozofdeath wrote: > Thank you both for your suggestions and answers. Although I don't truly > understand why the get line would become std::getline(std::cin, line); One part of the explanation is simple, the other more convoluted. Luckily the part you need to understand to use the right getline isn't hard, and if you get it wrong you'll get a compiler error message. The input stream classes (std::istream and std::iostream, and classes derived from them) happen to include a member function (or some number of overloaded member functions) called getline which know how to read into a character array. They do not have a member function that knows how to read into a std::string, but that functionality is provided by a non-member (aka "free") function std::getline. Arguably std::istream::getline (or its more general templated form) should have been a non-member function in the first place, but the IOStreams library on which the standard I/O facilities are based was created fairly early in the life of C++, when it was more common to make functions members of classes. It was also created at a time when there was not a single standard string class, and so it had no dependencies on such string classes. The relationship between the streams libraries (if we include locales there) and strings have grown complicated over time, and could use some tidying up in a future revision of C++, but fortunately there are only a few quirks that crop up in common use -- such as the two forms of getline, and the need to call .c_str() on a string in order to use it as a filename when opening a file. > you've helped me out greatly.Thanks again. My pleasure. Happy learning. -- James
From: Ulrich Eckhardt on 4 Dec 2006 13:35 cozofdeath wrote: > void leetspeak(char &array); This is not an array that is passed but a reference to a single char. > char line[100]; > char &array=line[100]; [...] > leetspeak(line[100]); Two things: - In order to pass that array by reference, you need this syntax: void l337sp33k( char (&array)[100]); ....but you don't want that. - Use a string object instead (std::string from <string>): void leetspeak( std::string& str) { for( std::string::size_type i=0; i!=str.size(); ++i) ... } std::string line; std::getline( std::cin, line); leetspeak(line); > system("PAUSE"); Non-portable, and what for anyway? Is it for some stupid IDE that closes the terminal window without waiting for you to read the stuff? In that case, this function comes handy: void wait_for_enter(){ std::cout << "<enter>" << std::endl; std::string line; std::getline( std::cin, line); } ....and that one is portable, although unnecessary on most systems. Uli -- FAQ: http://ma.rtij.nl/acllc-c++.FAQ.html
From: cozofdeath on 4 Dec 2006 21:52 Very nice info guys. I actually wasn't going to post this source code, I was just going to delete it but now I'm glad I posted. I've learned much more than what I expected. Thank you all. I don't think I would of learned this stuff in the book I'm reading.
First
|
Prev
|
Pages: 1 2 Prev: Visual Studio 2005 Professional, errors C2039 C2873 Next: Problem in Socket Programming |