简体   繁体   中英

Getting a string input from user in C++

This is the piece of code I am getting error in. Yes I have `using namespace std


string age2;

cin.ignore();

getline(cin, age2);

cout << "Your age is " << age2;

This is giving me error -> "getline is not defined"

I tried searching up and tried all the solutions but none of them worked.

This code here works fine:

#include <iostream>
#include <string>

int main(){

std::string age;

std::cin.ignore();

std::getline(std::cin, age);

std::cout << "Your age is " << age;

return 0;
}

so there must be something your missing in your example

also if your doing age, you should use an int instead of a string, like this:

#include <iostream>
#include <string>

int main(){

int age;

std::cin.ignore();

std::cin >> age;

std::cout << "Your age is " << age;

return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM