2014-09-30 96 views
1

我想为C++类取得这个任务。我遇到了do/while循环无法正常工作的问题,并且有人建议添加一行cin.ignore(2,'\ n');在用户要求输入学生名称的InputData函数中。这工作和做/现在正在工作。但是,我不是100%确定cin.ignore(2,'\ n');工作和我有一个问题,在第一次去用户输入的“名称”的前两个字符被丢弃。如果我将该2更改为0,它不会切断名称的前两个字符,但如果用户输入'y',他们想要继续,程序将跳过第一个问题“输入学生的姓名”。C++放弃从输入缓冲区问题剩余的换行

任何帮助非常感谢!

仅供参考,我对编程一般来说超级新,特别是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,'\ n');位于

// 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 

主,其中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 
+0

HTTP://www.cplusplus .com/doc/tutorial/control /,下次请使用MCVE。请。 – Chantola 2014-09-30 02:14:55

+0

MCVE和你一样为你着想。你应该总是试着单独开发新的功能*,编写最小,最简单的程序,它将使用你正在努力掌握的技术(比如'getline(cin,...)')。一旦它完美的工作,你可以将它合并到更大的东西中。 – Beta 2014-09-30 02:25:14

+1

只需使用'std :: cin.ignore();'而不是指定大小和字符..它会起作用。 – Brandon 2014-09-30 02:37:04

回答

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 
+0

谢谢Galik!我要检查一下。它看起来比我现在拥有的效率更高。 – NoobCoderChick 2014-09-30 03:39:20

+0

嘿Galik,所以我不需要在numbClasses下添加std :: ws以及? – NoobCoderChick 2014-09-30 03:43:39

+0

@ user3862586不确定你的意思是“之下”(之前?之后?),但你不应该在其他地方需要它。您的代码似乎对我所提出的更改有效。 – Galik 2014-09-30 03:45:30