How to use the getline command in c++?

前端 未结 5 1335
难免孤独
难免孤独 2021-02-03 14:22

I\'m trying to turn a cout command into a getline command in c++.

This is my code that I\'m trying to changes....

for (int count=0; count < numberOfEm         


        
5条回答
  •  隐瞒了意图╮
    2021-02-03 15:10

    You can use getline function that takes input stream as first argument. It's prototype looks like this: basic_istream<...>& getline(basic_istream<...>& _Istr, basic_string<...>& _Str)

    But then you have to think about that you are dealing with std::string and parse it as a type that you actually need (char*, int, whatever):

    #include 
    #include 
    #include 
    using namespace std;
    //...
    
    for (int count=0; count < numberOfEmployees; count++)
    {
        string name, title, sSSnum, sSalary, s;
    
        cout << "Name: ";
        getline(cin, name); 
        employees[count].name = name.c_str();
    
        cout << "Title: ";
        getline(cin, title);
        employees[count].title = name.c_str();
    
        cout << "SSNum: ";
        getline(cin, sSSnum);
        istringstream is(sSSnum);
        is >> employees[count].SSNum;
    
        cout << "Salary: ";
        getline(cin, sSalary);
        is.clear();
        is.str(sSalary);
        is >> employees[count].Salary;
        //...
    }
    

提交回复
热议问题