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

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

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

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

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

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

显示数组p

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

这是我的代码:

#include "stdafx.h"

#include

#include

#include

#include

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值