c++ - 为什么我的 For 循环没有完成收集数据以保存在动态分配的数组中?

标签 c++ function for-loop struct dynamic-arrays

我的函数内的 for 循环没有完成它的完整运行。在询问学生的地址后,它只是输出:

"Enter City: Enter Age: Enter Name: Enter Street Address: Enter City: Enter 
Age: Enter Name: Enter Street Address: Enter City: Enter Age:"

而不是让用户输入他们的其余信息。基本上 for 循环应该为每个学生循环。所以如果有 3 个学生,它应该循环 3 次,这样每个学生都可以输入他们的信息。 这是我到目前为止的代码:

#include <iostream>
#include <string>
#include <iomanip>
#include <cstring>
using namespace std;

struct Address
{
   char sStreet[255];
   char sCity[255];
};

struct Student
{
   char sName[255];
   Address address;
   int nAge;
};

void initializeData(Student * pStudents, int size);
// void sortData(Student * pStudents, int size);
// void displayData(Student * pStudents, int size);

int main()
{
  int size = 0;

  cout << "Enter the number of students in your classroom: ";
  cin >> size;

  Student * pStudents = new Student[size];

  initializeData(pStudents, size);

  delete [] pStudents;
  pStudents = NULL;

  return 0;
}

void initializeData(Student * pStudents, int size)
{
  for(int i = 0; i < size; i++)
  {
    cout << "Enter Name: ";
    cin >> pStudents[i].sName;

    cout << "Enter Street Address: ";
    cin >> pStudents[i].address.sStreet;

    cout << "Enter City: ";
    cin >> pStudents[i].address.sCity;

    cout << "Enter Age: ";
    cin >> pStudents[i].nAge;
  }
}

我真的很困惑为什么它没有完成它的循环 :( 这个任务还有更多,但我被困在这里。我不确定为什么 for 循环没有完全完成,我试图得到在上课时间提供帮助,但没有人可以帮助我解决这个问题。

更新:感谢最先发表评论的两位用户!你的两个建议对我都有帮助,在修复我的代码后,这就是现在的工作方式:

#include <iostream>
#include <string>
#include <iomanip>
#include <cstring>
using namespace std;

struct Address
{
  char sStreet[255];
  char sCity[255];
};

struct Student
{
  char sName[255];
  Address address;
  int nAge;
};

void initializeData(Student * pStudents, int size);
// void sortData(Student * pStudents, int size);
// void displayData(Student * pStudents, int size);

int main()
{
  int size = 0;

  cout << "Enter the number of students in your classroom: ";
  cin >> size;

  cin.ignore();

  Student * pStudents = new Student[size];

  initializeData(pStudents, size);

  delete [] pStudents;
  pStudents = NULL;

  return 0;
}

void initializeData(Student * pStudents, int size)
{
  for(int i = 0; i < size; i++)
  {
    cout << "Enter Name: ";
    cin.getline(pStudents[i].sName, 255);

    cout << "Enter Street Address: ";
    cin.getline(pStudents[i].address.sStreet, 255);

    cout << "Enter City: ";
    cin.getline(pStudents[i].address.sCity, 255);

    cout << "Enter Age: ";
    cin >> pStudents[i].nAge;

    cin.ignore();
  }
}

最佳答案

operator>>当用户键入 ENTER 键时,不会提取换行符。你必须调用cin.ignore()打电话后 operator>> ,尤其是在读取整数之后,例如:

cout << "Enter the number of students in your classroom: ";
cin >> size;
cin.ignore(numeric_limits<streamsize_t>::max(), '\n');

您可以使用 cin.getline()而不是 operator>>将整行文本读入 char[] .它会为你跳过换行符,例如:

cout << "Enter Name: ";
cin.getline(pStudents[i].sName, 255);

或者更好,使用 std::getline而不是将一行读入 std::string ,它也会为您跳过换行符,然后您可以使用 std::istringstream根据需要解析字符串,例如:

struct Student
{
    std::string sName;
    ...
};

cout << "Enter the number of students in your classroom: ";
std::string tmp;
getline(cin, tmp);
istringstream(tmp) >> size;

...

cout << "Enter Name: ";
getline(cin, pStudents[i].sName);

无论如何,您都应该考虑其中一种方法,这样您就可以读取包含多个单词的名称、地址和城市,如 operator>>当它看到任何空白字符时停止读取(或者在整数的情况下,当它看到非整数字符时)。

关于c++ - 为什么我的 For 循环没有完成收集数据以保存在动态分配的数组中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49620582/

相关文章:

c++ - 为 C++ 二叉搜索树使用智能指针

c++ - 我想使用 C++ 从目录中获取字符串中的文件名

c++ - 双指针**src和运算(*(*src)++ = reg_offset)

javascript - 自动背景图像更换器

r - get.inducedSubgraph 和循环函数

c++ - 涉及全局对象的循环依赖 C++

c++ - Lua - 反射 - 获取对象上的函数/字段列表?

c++ - 错误 : two or more data types in declaration of main?

c++ - 循环遍历 vector 时将对象添加到 vector

Javascript 改进了原生 for 循环