Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/157.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++;丢弃输入缓冲区问题中的剩余换行符 我正试图把这个作业分解成C++类。我发现do/while循环无法正常工作,有人建议添加行cin.ignore(2,“\n”);在InputData函数中,用户在其中要求输入学生姓名。这起作用了,do/while现在起作用了。但是,我不能100%确定cin.ignore(2,'\n');在第一次检查期间,我遇到了一个问题,用户输入的“名称”的前两个字符被丢弃。如果我将2改为0,它不会切断名称的前两个字符,但如果用户输入“y”他们想继续,程序将跳过第一个问题“输入学生的姓名”_C++_Newline_Do While - Fatal编程技术网

C++;丢弃输入缓冲区问题中的剩余换行符 我正试图把这个作业分解成C++类。我发现do/while循环无法正常工作,有人建议添加行cin.ignore(2,“\n”);在InputData函数中,用户在其中要求输入学生姓名。这起作用了,do/while现在起作用了。但是,我不能100%确定cin.ignore(2,'\n');在第一次检查期间,我遇到了一个问题,用户输入的“名称”的前两个字符被丢弃。如果我将2改为0,它不会切断名称的前两个字符,但如果用户输入“y”他们想继续,程序将跳过第一个问题“输入学生的姓名”

C++;丢弃输入缓冲区问题中的剩余换行符 我正试图把这个作业分解成C++类。我发现do/while循环无法正常工作,有人建议添加行cin.ignore(2,“\n”);在InputData函数中,用户在其中要求输入学生姓名。这起作用了,do/while现在起作用了。但是,我不能100%确定cin.ignore(2,'\n');在第一次检查期间,我遇到了一个问题,用户输入的“名称”的前两个字符被丢弃。如果我将2改为0,它不会切断名称的前两个字符,但如果用户输入“y”他们想继续,程序将跳过第一个问题“输入学生的姓名”,c++,newline,do-while,C++,Newline,Do While,非常感谢您的帮助 我是超级编程新手,特别是C++。请友善点,哈哈 #include <iostream> #include <cstdlib> #include <string> using namespace std; class Student { public: Student(); ~Student(); // Input all info of user void InputData(); // Output clas

非常感谢您的帮助

我是超级编程新手,特别是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
#包括
#包括
#包括
使用名称空间std;
班级学生{
公众:
学生();
~Student();
//输入用户的所有信息
void InputData();
//输出类列表
void OutputData();
//重置类列表
void ResetClasses();
学生和操作员=(常量学生和右侧);
私人:
字符串名;
弦乐特技;
int类;
字符串*类列表;
};//末班学生
//将变量初始化为空,将数组初始化为空
学生::学生(){
数量级=0;
classList=NULL;
name=“”;
}//结束变量初始化
//释放分配给阵列的所有内存。
学生::~Student(){
if(类列表!=NULL){
删除[]类列表;
}//如果结束
}//端空闲存储器
//删除类列表
void Student::ResetClasses(){
if(类列表!=NULL){
删除[]类列表;
classList=NULL;
}//结束if块
数量级=0;
}//结束重置类
这里是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
//从用户输入信息。
void Student::InputData(){
int i;
//如果再次调用方法且未清除数组,请重置类列表
重置类();

cout我建议您在使用
std::getline()
之前,删除所有
cin.ignore(2,'\n');
语句,并跳过空白(空格和返回)。您可以使用
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
//从用户输入信息。
void Student::InputData(){
int i;
//如果再次调用方法且未清除数组,请重置类列表
重置类();
cout std::ws,name);//注意:>>std::ws跳过空白
cout类;
//从输入缓冲区中丢弃剩余的换行符
//cin.忽略(2,'\n');
如果(类别>0){
//构造足够大的数组以容纳#个类
classList=新字符串[numbClasses];
//循环遍历#类,将每个类的名称输入数组
对于(i=0;i<0;i++){
cout>std::ws跳过空白
}//循环结束
}//结束if块

下次请使用MCVE。请。MCVE是为了您的利益,也是为了我们的利益。您应该始终尝试单独开发新功能,编写最小、最简单的程序来练习您试图掌握的技术(例如
getline(cin,…)
)。一旦它工作正常,你可以将它合并到更大的文件中。只需使用
std::cin.ignore();
而不是指定大小和字符。。它会工作的。嘿,布兰登,谢谢你的帮助。不过它仍然在删除一个字符。Beta/Chantola,我不确定我是否理解?使用
std::cin.ignore()
就在
do while
循环结束之前,就在
cin>>选项之后;
InputData()删除对
std::cin.ignore()的第一个调用
。谢谢Galik!我要去看看。它看起来比我现在拥有的更有效。嘿Galik,所以我也不需要在numbClasses下添加std::ws?@user3862586不确定“under”(之后?之前?)是什么意思。但你不应该在其他任何地方需要它。我提议的更改对你的代码似乎很好。
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