-
getline command
I'm a newbe to c++ and have been researching the getline command
in Xcode I have gotten to this stage
std::string handle, title;
std::cout << "Enter your name: " << std::endl;
std::getline (std::cin , handle ) ;
std::cout << "Enter your favourite movie: " << std::endl;
std::getline ( std::cin , title) ;
std::cout << handle << "'s favourite movie is " << title << std::endl;
but the
std::cout << "Enter your name: " << std::endl;
std::getline (std::cin , handle ) ;
doesn't give a prompt for user input but
std::cout << "Enter your favourite movie: " << std::endl;
std::getline ( std::cin , title) ;
does. Can anyone tell me why
-
Is there any code that you aren't showing us? Put it another way: is this the entire content of the main function?
-
Thankyou all for your input but I have solved my problem using this format
and example I found in a book hope this helps someone else out::
Code:
#include <iostream>
#include <string>
usingnamespacestd;
int main(int argc, const char * argv[]) {
// insert code here...
string greatstring ("Hello std::string ! ");
cout << greatstring << endl;
cout << " Enter a line of text " << endl;
string firstLine;
getline (cin, firstLine);
cout << " Enter another " << endl;
string secoundLine;
getline (cin, secoundLine);
cout << " Result of concatenation: " << endl;
string concatString = firstLine + " " +secoundLine;
cout << concatString << endl;
cout << " Copy of concatenated string: " << endl;
string aCopy;
aCopy = concatString;
cout << aCopy << endl;
cout << " Length of concat string: " << concatString. length() << endl;
return0;
}
-
the code in the first post is working! without any problems .
there are some typo errors in the second code .
like
should be
Code:
using namespace std;
and
should be
happy coding :).