为什么我的循环完整收集数据不在动态分配的数组中



我的功能内部的循环无法完成其完整运行。要求学生的地址之后,它只是输出:

"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 键中时,不会提取销售线。打电话给operator>>后,您必须致电cin.ignore(),尤其是在阅读整数后,例如:

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>>在看到任何whitespace字符时停止阅读(或在整数中,当它看到它时非综合字符(。

首先:问题是使用cin读取包含空间的字符串。例如,读取pStudents[i].sName,如果输入" John Kim"。然后,John将在变量pStudents[i].sName中,Kim将存储在pStudents[i].address.sCity中。原因是cin停止阅读并移至下一个变量,如果它找到了空间。

其次:如果您的输入已终止,则可以使用getline,例如getline(cin, pStudents[i].sName);,请查看getline的定义:

istream& getline (istream& is, string& str, char delim);

您可以将划界字符指定为DELIM,getline的默认值是newline。

第三: getline接受参数为string,因此您的字符数组被字符串替换为:

   string sStreet;
   string sCity;
   string sName;

更新的代码下面给出:

#include <iostream>
#include <string>
#include <iomanip>
#include <cstring>
using namespace std;
struct Address
{
   string sStreet;
   string sCity;
};
struct Student
{
   string sName;
   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: ";
    getline(cin, pStudents[i].sName);
    cout << "Enter Street Address: ";
    getline(cin, pStudents[i].address.sStreet);
    cout << "Enter City: ";
    getline(cin, pStudents[i].address.sCity);
    cout << "Enter Age: ";
    cin >> pStudents[i].nAge;
  }
}

相关内容

  • 没有找到相关文章

最新更新