首页 文章

将用户输入数据存储在动态结构阵列中/显示所述数据 . C

提问于
浏览
0

所以对于我的项目,我必须......

  • 使用以下格式创建结构:struct PERSON {string name;年龄; float gpa;};

  • 要求用户输入他们想要输入的记录数 .

  • 创建PERSON类型的动态数组p,以便能够保存第2部分中的所有记录

  • 在第2部分中将数据读入数组p

  • 显示数组p

但是,每当我运行程序并开始输入数据时,它就不会让我输入多组数据,输出似乎没什么意义 .

这是我的代码:

#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib>
using namespace std;

struct PERSON
{
  string name;
  int age;
  float gpa;
};
PERSON *p;

int main()
{
  int count;
  cout << "Please enter the number of records you wish to enter: ";
  cin >> count;

  p = new (nothrow) PERSON[count];

  if (p == nullptr)
    cout << "Error: memory could not be allocated";

  for (int i = 0; i < count; i++)
  {
    cout << "Enter name, age, and gpa: ";

    getline(cin, p[i].name);
    cin >> p[i].age;
    cin >> p[i].gpa;

    cout << endl;
  }cout << endl;

  cout << "This is the content of array p: " << endl;
  cout << right << setw(8) << "NAME" << setw(7) << "AGE" << setw(7) << "GPA"  << endl;
  cout << "--------------------------" << endl;

  for (int i = 0; i < count; i++)
  {
    cout << p[i].name << " " << p[i].age << " " << p[i].gpa << endl;
  }

  return 0;
}

现在,当我将数据从文件复制到动态数组结构中时,这种格式似乎工作得很好,但是我现在似乎无法使用它来处理用户输入 .

以下是该程序的试运行:

Please enter the number of records you wish to enter: 3
Enter name, age, and gpa: Robert 21 2.1 // This is the info I tested

Enter name, age, and gpa:
Enter name, age, and gpa:

This is the content of array p:
    NAME    AGE    GPA
--------------------------
 -842150451 -4.31602e+008 //?
 -842150451 -4.31602e+008 //?
 -842150451 -4.31602e+008 //?
Press any key to continue . . .

我现在可能已经超过了代码二十多次,并且已经找了一段时间的答案了 . 任何建议/分析/评论将不胜感激 .

So @AndyG suggested that I change the getline(cin, p[i].name) to cin >> p[i].name and that seemed to clear things up. Thank you!

Sorry to trouble everyone over such a silly muck-up.

2 回答

  • 0

    问题

    你的 getline(cin, p[i].name) 实际上是在同一行上抓取你所有的输入,后来的 cin 语句将不匹配 .

    std::getline 应保留用于将整行输入字符缓冲区或 std::string ,即它并不总是用于读取字符串的适当工具 .

    解决方案

    相反,您只需要将 getline(cin, p[i].name) 替换为 cin >> p[i].name .

    Live Demo


    其他细微变化

    删除 nullptr 检查:

    if (p == nullptr)
        cout << "Error: memory could not be allocated";
    

    因为在C中,如果 new 运算符无法分配,则会抛出异常 . 就像提到的bku_drytt一样,你可以将它包装在try catch块中,尽管我个人只是完全删除了它 .

    #include <new>
    // ...
    Person* p = null;
    try
    {
       p = new Person[count];
    } catch(std::bad_alloc& ex)
    {
       cerr << "Unable to allocate memory for Person! Error msg: " << ex.what() << "\n";
       return 1;
    }
    

    最后,不要忘记 delete[]p 分配的内存:

    // ...
       delete[] p;
       return 0;
    } //end of int main()
    

    即使您的操作系统肯定会为您清理内存,您仍应确保手动清理它 . 至少是好的做法 . 或者,您可以改为使用 std::arraystd::vector ,它仍然可以像您已经拥有的_样式数组_1811466一样随机访问 .

  • 0

    std::getline() 函数与您的意图相关"misbehaving"因为(我认为)在流输入队列中留下了新的行字符 .

    您可以使用以下命令 cin.sync() 丢弃此类字符 .

    为什么会这样?因为 operator>> 不读取空格,但 std::getline() 会读取空格,所以当执行 cin >> count; 语句并按Enter键时,新行字符(按下回车键)将保留在流输入队列中,执行时会自动读取 std::getline() .

    还有其他功能可以实现这一点,也许更有信息的人可以告诉我们哪个是最佳选择 .

    这是一个固定版本:

    #include "stdafx.h"
    #include <iostream>
    #include <iomanip>
    #include <string>
    #include <cstdlib>
    using namespace std;
    
    struct Person
    {
        string name;
        int age;
        float gpa;
    };
    
    int main()
    {
        cout << "Please enter the number of records you wish to enter: ";
    
        int count;
        cin >> count;
    
    
        Person* p = new Person[ count ];
    
        for ( int i = 0; i < count; i++ )
        {
            cin.sync(); // added this to discard unused characters from input queue
    
            cout << "Enter name: ";
            getline( cin, p[ i ].name );
    
            cout << "Enter age: ";
            cin >> p[ i ].age;
    
            cout << "Enter gpa: ";
            cin >> p[ i ].gpa;
    
            cout << endl;
        }cout << endl;
    
        cout << "This is the content of array p: " << endl;
        cout << right << setw( 8 ) << "NAME" << setw( 7 ) << "AGE" << setw( 7 ) << "GPA" << endl;
        cout << "--------------------------" << endl;
    
        for ( int i = 0; i < count; i++ )
        {
            cout << p[ i ].name << " " << p[ i ].age << " " << p[ i ].gpa << endl;
        }
    
        return 0;
    }
    

    注意:您不需要检查Person * p是否为 nullptr ,因为如果无法分配足够的内存,new运算符将抛出std :: bad_alloc异常 . 如果你真的想要,可以考虑将这个内存分配包装在try catch块中 .

相关问题