2016-06-17 20 views
0

私は自分の質問に対する答えを探していて、見つけられませんでした。cin.getline()は空白に応じて入力を区切りませんか?

cout << "Enter student's first name and last name:\n"; 
cin.ignore(100, '\n'); 
cin.getline(name, 20, ' '); 
cin.getline(family, 20, ' '); 

をしかし、[など:デビッド・ジョーンズ]入力は: 私が使用している分離、そして唯一の「名前」であるされていません。

getline()がデリミタで入力を分離しないのはなぜですか? ありがとう!

フル機能:あなたはcin.ignore(100, '\n')を呼ぶ理由

//add a Student to Students.txt 
void addStudent(fstream &f1) 
{ 
    //exception 
    if (!f1) 
     return; 
    int id; 
    char family[20]; 
    char name[20]; 
    bool courses[5]; 
    cout << "Enter student's ID:\n"; 
    id = getInteger(id, 1, 100); // id range: (1 <= id <= 100) 
    cout << "Enter student's first name and last name:\n"; 
    cin.ignore(100, '\n'); 
    cin.getline(name, 20, ' '); 
    cin.getline(family, 20, ' '); 
    cout << "Enter 0/1 for each of the student's 5 courses\n"; 
    int boolean; // 0/1 input 
    for (int i = 0; i < 5; i++) 
     courses[i] = getInteger(boolean, 0, 1); 
    //find out if this id is already taken [=> return] 
    if (!f1) 
     throw "Error opening -Students.txt- from Project directory.\n"; 
    if (isInFile("Students.txt", id)) 
     return; 
    /*continue in case the opening was successful AND the id doesn't already exist*/ 
    Student s(id, family, name, courses); 
    //write to our file 
    f1.seekp((id - 1) * sizeof(Student)); //(id-1): ids start from 1 
    f1.write((char*)&s, sizeof(Student)); 
} 
+0

[私のための作品](http://ideone.com/YaI7bo) –

答えて

0

私は理解していません。私はそれがあなたを混乱させていると思います。

ソリューションはignore()を避け、可能性があり、単純に

cin >> name >> family; 

を書きますが、あなたがgetline()を使用したい場合は、私がそうでなければ、姓の後にスペースを追加する必要があります

cin.getline(name, 20, ' '); 
cin.getline(family, 20, '\n'); 

を示唆しています。

+0

ありがとうございます:) – Yair

関連する問題