C ++从输入缓冲区问题中丢弃剩余的换行符

时间:2014-09-30 02:12:19

标签: c++ newline do-while

我正在努力为c ++类完成这项任务。我遇到了do / while循环无法正常工作的问题,有人建议添加行cin.ignore(2,' \ n');在InputData函数中,用户询问输入学生姓名的位置。这很有效,而do / while现在正在运作。但是,我并不是100%肯定cin.ignore(2,' \ n');工作和我有一个问题,在第一次走动的地方,"名称"的前两个字符。用户输入被丢弃。如果我将2更改为0,则它​​不会切断名称的前两个字符,但如果用户输入' y'他们想继续,程序会跳过第一个问题"输入学生的姓名"。

非常感谢任何帮助!!

仅供参考,我对编程一般都很陌生,尤其是c ++。请好,请大声笑。

#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;

class Student {
public:
   Student();
   ~Student();
   // Input all info of user
   void InputData();
   // Output class list
   void OutputData();
   // Reset class list
   void ResetClasses();
   Student& operator =(const Student& rightSide);

private:
    string name;
    string stuName;
    int numbClasses;
    string *classList;
};//end student class

//Initialize variables to empty and array to NULL
Student::Student() {
    numbClasses = 0;
    classList = NULL;
    name = "";
}//end variable initialization

//Frees up any memory allocated to array.
Student::~Student() {
    if (classList != NULL) {
    delete [ ] classList;
}//end if
}//end free memory

//Delete the class list
void Student::ResetClasses() {
    if (classList != NULL) {
       delete [] classList;
       classList = NULL;
    }//end if block
    numbClasses = 0;
}//end reset classes

这里是行cin.ignore(2,&#39; \ n&#39;);位于

// Inputs info from user.
void Student::InputData() {

int i;
// Reset the class list in case method is called again and array isn't cleared
ResetClasses();

cout << "Enter student name." << endl;
//Discards the leftover newline from input buffer
cin.ignore(2,'\n');
getline(cin, name);


cout << "Enter number of classes." << endl;
cin >> numbClasses;
//Discards the leftover newline from input buffer
cin.ignore(2,'\n');
if (numbClasses > 0) {
    // Construct array big enough to hold # of classes
    classList = new string[numbClasses];
    // Loop through the # classes, input name of each one into array
    for (i = 0; i < numbClasses; i++) {
        cout << "Enter name of class " << (i+1) << endl;
        getline(cin, classList[i]);
    }//end for loop
}//end if block
cout << endl;
}//end input data

输出数据

//Output info entered by user.
void Student::OutputData() {

int i;
cout << "Name: " << name << endl;
cout << "Number of classes: " << numbClasses << endl;
for (i=0; i<numbClasses; i++) {
    cout << "  Class " << (i+1) << ":" << classList[i] << endl;
}//end for loop
cout << endl;
}//end Output data

//overload this operator so there aren't two references to same class list.
Student& Student::operator =(const Student& rightSide) {

int i;
// Erase list of classes
ResetClasses();
name = rightSide.name;
numbClasses = rightSide.numbClasses;

// Copy the list of classes
if (numbClasses > 0) {
    classList = new string[numbClasses];
    for (i=0; i<numbClasses; i++) {
        classList[i] = rightSide.classList[i];
    }//end for loop
}//end if block
return *this;
}//end overload

Main,do / while循环所在的位置。

// main function
int main() {

    char choice;
    //Do/While loop to ask user if they'd like to continue or end program.
do {
    // Test code with two student classes
    Student s1, s2;
    // Input for s1
    s1.InputData();
    cout << "Student 1's data:" << endl;
    // Output for s1
    s1.OutputData();
    cout << endl;

    s2 = s1;
    cout << "Student 2's info after assignment from student 1:" << endl;
    // Should output same info as student 1
    s2.OutputData();

    s1.ResetClasses();
    cout << "Student 1's info after the reset:" << endl;
    // Should have no classes
    s1.OutputData();

    cout << "Student 2's info, should still have original classes:" << endl;
    // Should still have original classes
    s2.OutputData();
    cout << endl;

    cout << "Would you like to continue? y/n" << endl;
    cin >> choice;

} while(choice == 'y'); //end do/while
return 0;
}//end main

1 个答案:

答案 0 :(得分:1)

我建议您删除所有cin.ignore(2,'\n');语句,然后在使用std::getline()之前立即跳过空格(空格和返回)。您可以使用std::ws操纵者执行此操作:请参阅: std::ws

所以你的std::getline()陈述变为:

getline(cin >> std::ws, name); // NOTE: >> std::ws skips whitespace

所以喜欢这样:

// Inputs info from user.
void Student::InputData() {

int i;
// Reset the class list in case method is called again and array isn't cleared
ResetClasses();

cout << "Enter student name." << endl;
//Discards the leftover newline from input buffer
//cin.ignore(2,'\n');
getline(cin >> std::ws, name); // NOTE: >> std::ws skips whitespace

cout << "Enter number of classes." << endl;
cin >> numbClasses;
//Discards the leftover newline from input buffer
//cin.ignore(2,'\n');
if (numbClasses > 0) {
    // Construct array big enough to hold # of classes
    classList = new string[numbClasses];
    // Loop through the # classes, input name of each one into array
    for (i = 0; i < numbClasses; i++) {
        cout << "Enter name of class " << (i+1) << endl;
        getline(cin >> std::ws, classList[i]); // NOTE: >> std::ws skips whitespace
    }//end for loop
}//end if block
cout << endl;
}//end input data