【问题标题】:Why doesnt my For-Loop Complete Collecting Data to hold in a Dynamically Allocated Array?为什么我的 For-Loop 没有完成收集数据以保存在动态分配的数组中?
【发布时间】:2018-04-03 00:30:30
【问题描述】:

我的函数中的 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();
  }
}

【问题讨论】:

  • 您有问题吗?

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


【解决方案1】:

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

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

您可以使用cin.getline() 而不是operator&gt;&gt; 将整行文本读入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&gt;&gt; 在看到任何空白字符时会停止读取(或者在整数的情况下,当它看到非整数字符)。

【讨论】:

    【解决方案2】:

    首先:问题是使用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 默认为换行符。

    第三: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;
      }
    }
    

    【讨论】:

    • 您好,我最初使用的是 getline,但使用 getline 它会跳过名称输入并直接转到街道地址。所以这是我使用 cin.getline 的输出:“输入名称:输入街道地址:”
    • 您可以复制粘贴我的代码进行测试。如果您有任何其他困惑,那么您可以在帖子中询问。
    • 我在每次输入后放置 cin.ignore() 后它就开始工作了,但是假设我有 3 个学生。在我输入第一个学生的信息后,它会执行相同的输出:“输入年龄:输入姓名:输入街道地址:输入城市:输入年龄:输入姓名:输入街道地址:输入城市:输入年龄:”
    猜你喜欢
    • 1970-01-01
    • 2020-02-12
    • 1970-01-01
    • 2021-04-16
    • 2012-12-02
    • 1970-01-01
    • 1970-01-01
    • 2020-05-31
    • 1970-01-01
    相关资源
    最近更新 更多