我有一段時間想弄清楚下一步該做什麼。基本上,這個程序不能使用指針變量(*)和動態分配的內存(例如「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);
爲什麼你要把這些成員作爲結構中的指針?這聽起來像是一個人爲的家庭作業。 –
你已經回答了你自己的問題:「int * variable = new int」 - 你的代碼在哪裏?實際上你應該創建一個字符串,並在你嘗試讀取某些東西之前,爲一個和兩個加倍。 –
要修復程序,您需要查看:構造函數,析構函數,動態內存分配和指針解引用。 –