c++ - 将数据输入到具有指针成员变量的结构中

标签 c++ pointers struct cin dot-operator

<分区>

我费了好大劲才弄清楚接下来要做什么。基本上,这个程序只能使用指针变量 (*) 和动态分配的内存(例如,“int *variable = new int”)。我必须在其他一切之上创建一个结构!

我在下面包含了一些代码。在我所有的 cin“>>”运算符下方都有红线,并显示一条消息,“错误:没有运算符”>>“匹配这些操作数”。在代码的“one.score2”部分(最后一行)中,我的“one”下方还有一条红线,并显示一条消息,“错误:表达式必须具有整数或无作用域的枚举类型”。

如何让我的代码工作?

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

// Define structures
struct student
{
    string *name;
    double *score, *score2, *average;
};

void main(void)
{
    // Create our students
    student one, two;

    cout << "Enter the first student's name: ";
    cin >> one.name;
    cout << endl << "Enter " << one.name << "'s first exam: ";
    cin >> one.score;
    cout << endl << "Enter " << one.name << "'s second exam: ";
    cin >> one.score2;
    one.average = ((one.score + one.score2) / 2);

最佳答案

只需改变你的结构如下

struct student
{
    string name;
    double score, score2, average;
};

根据您的示例,它们不需要是指针,因为 Joachim Pileborg 也指出

关于c++ - 将数据输入到具有指针成员变量的结构中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14767328/

相关文章:

c++ - libpcap 中数据包的 vector

c - 分配 (const char *) 指针类型 - 段错误

c++ - 错误 : no matching member function for call to 'push_back'

c++ - 弧形二维碰撞检测

c++ - 在同一线程上 boost 互斥锁

c++ - 加载共享库时 undefined symbol "tbb internal Allocate"

c++ - 如何在 C++ 中创建一个 'dynamic' 指针

c - 结构体中取消引用指针的错误

c - 为什么在初始化结构时出现段错误?

c++ - 如何使getline与ios::exceptions一起玩?