-1

I've searched this website and I have tried most things and they all don't work If anyone knows what I am doing wrong please help, This is my code.

 string getlinetest;
 cout << "What is the string?" << endl;
 cin >> getlinetest;
 getline(cin >> getlinetest);
 cout << getlinetest << endl;
brc-dd
  • 10,788
  • 3
  • 47
  • 67
RandomPerson
  • 1
  • 1
  • 5
  • Can you tell what is happening in the 4th line? What dou want to pass to getline? Maybe you can figure it out, on yourself – RoQuOTriX Jul 08 '20 at 08:23
  • 2
    Either `cin>>getline;` or `getline(cin, getlinetest);`. Beware of [mixing these two options](https://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction). – Yksisarvinen Jul 08 '20 at 08:28
  • [std::getline](https://en.cppreference.com/w/cpp/string/basic_string/getline) – David C. Rankin Jul 08 '20 at 08:34

1 Answers1

2

std::getline is not used as you are trying to use it. (I think you just made a typo by using >> instead of ,.)

You need to call it like this:

std::string getlinetest;
std::cout << "What is the string?" << std::endl;
std::getline(std::cin, getlinetest);
std::cout << getlinetest << std::endl;


PS:

And I don't get any sense behind using cin >> getlinetest; before using getline. If you want to remove preceding whitespaces, then you probably need to use std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); instead of putting a cin statement before.

Check out these threads:

  1. Using getline(cin, s) after cin
  2. Why does std::getline() skip input after a formatted extraction?
  3. cin and getline skipping input
brc-dd
  • 10,788
  • 3
  • 47
  • 67