简体   繁体   中英

C++ File Cin.ignore() function

It could not work if I type the Name with the space such as Peter Stroll. How to use cin.ignore() in this case?

Thank you in advance.

int main()
{
    ofstream File("player.txt");

    cout << "Enter player ID, Name, and Money:" << endl;
    cout << "Press Ctrl + Z to quit \n" << endl;

    int id;
    string name;
    int age;

    while(cin >> id >> name >> age)
    {
        File << id << " " << name << " " << age << endl;
    }
    return 0;
}

cin won't accept any more character if it encounters tab or space key. use getline instead

char name[64] = {0}; cin.getline(name, 64);

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