[英]Create a structure that contains a pointer to an array
我不能在作業中使用矢量
我必須創建一個存儲以下信息的結構:學生姓名、身份證號碼、指向測試分數數組的指針、平均測試分數、字母等級。
該計划應保留一組學生的考試成績清單。 它應該詢問用戶班上有多少學生以及有多少考試成績。每個學生的考試成績數量相同。 程序應該動態分配一個結構數組。 每個結構 Tests 成員都應該指向一個動態分配的數組,該數組將保存測試分數。 數組動態分配后,程序應向用戶詢問每個學生的 id 號和所有考試成績。 應計算平均分數並將其存儲在結構的平均成員中。 顯示學生信息。
當用戶在inputScores
函數中輸入測試分數時,我遇到了問題。 我不知道如何正確訪問結構成員。 你能看看我的代碼並幫助我弄清楚我做錯了什么嗎?
這是我的結構:
struct Grade
{
string name;
int idNum;
int* tests;
double average;
char grade;
};
這是我的功能:
//user enters number of students which is then used as the size of the dynamically allocated struct array
int inputStudents();
//user enters number of tests for each student which is then used as the size of the dynamically allocated array of test scores in the structure
int inputTests();
string inputName();
int inputID();
//user inputs scores for each student. this is a function i'm struggling with
int *inputScores(int *testScores, int numTests);
double calcAvg(int *testScores, int numTests);
char calcGrade(double);
void display(Grade*, int);
這是我的主要內容:
int main()
{
Grade *studentList;
int size = inputStudents();
studentList = new Grade[size]; //dynamically create array of Grade struct
int numTests = inputTests(); //number of test scores stored in numTests
for (int i = 0; i < size; i++) //loop to store all student's info
{
studentList[i].name = inputName();
studentList[i].idNum = inputID();
studentList[i].tests = inputScores(studentList[i].tests, numTests, i);
studentList[i].average = calcAvg(studentList[i].tests, numTests);
studentList[i].grade = calcGrade(studentList[i].average);
}
display(studentList, size);
delete[] studentList;
return 0;
}
這是我正在努力的功能。 每次我引用studentList
出現不同的錯誤。 我的程序甚至無法編譯,我很確定這個函數就是原因。 這里我傳遞的是tests
結構成員,而不是整個結構。 我還傳遞了測試次數和main
循環中的計數器,該計數器告訴我們用戶正在輸入哪個學生的測試分數。
int *inputScores(int *studentList, int numTests, int count)
{
cout << endl;
cout << "Enter the test scores:\n";
for (int i = 0; i < numTests; i++)
{
studentList[count].tests = new int[numTests]; //my error says the studentList must have a class type
cout << "Score " << (i + 1) << ": ";
cin >> studentList[count]->tests[i]; //my error says studentList must have a pointer type
cout << endl;
}
return studentList->tests; //my error says studentList must have a point-to-class type
}
您將指向每個學生的測試數組的指針傳遞給inputScores()
但將其作為指向學生的指針進行訪問。 您必須更改inputScores()
函數,如下所示:
void inputScores(int *tests, int numTests, int count)
{
cout << endl;
cout << "Enter the test scores:\n";
for (int i = 0; i < numTests; i++)
{
cout << "Score " << (i + 1) << ": ";
cin >> tests[i];
cout << endl;
}
}
另外,我又做了一個修改。 如果調用者函數分配內存並傳遞對數組的引用,它總是更好。 所以我們需要調用 inputScores() 函數,如下所示
studentList[i].tests = new int[numTests];
inputScores(studentList[i].tests, numTests, i);
這是我收到的輸出:
How many students are there?
3
How many tests does each student have?
2
Enter the student's name:
Joohn
Enter the student's ID number:
1
Enter the test scores:
Score 1: 12
Score 2: 13
Enter the student's name:
Jack
Enter the student's ID number:
2
Enter the test scores:
Score 1: 67
Score 2: 45
Enter the student's name:
Jill
Enter the student's ID number:
3
Enter the test scores:
Score 1: 56
Score 2: 89
--------------------------------------------------------
Here is all the info I have for each student:
Student #1:
Name: Joohn
ID Number: 1
Test Scores: Average Grade: 12
Letter Grade: F
Student #2:
Name: Jack
ID Number: 2
Test Scores: Average Grade: 56
Letter Grade: F
Student #3:
Name: Jill
ID Number: 3
Test Scores: Average Grade: 72
Letter Grade: C
--------------------------------------------------------
你的函數inputScores
是這樣聲明的
int *inputScores(int *studentList, int numTests, int count)
這意味着studentList
是一個指向int
的指針(可以像數組一樣對待)。 稍后在函數中,您嘗試執行以下操作:
studentList[count].tests = new int[numTests];
如果studentList
是一個int*
(或int
數組),那么studentList[count]
只是一個int
。 您正在嘗試訪問int
的成員變量tests
。 但int
是一種原始類型。
當你在 main 中調用這個函數時,你正確地傳遞了一個int*
:
studentList[i].tests = inputScores(studentList[i].tests, numTests, i);
因此,將變量視為實際上是int *
而不是Grade *
應該可以解決您的第一個問題。 您還應該更改變量的名稱,因為您並沒有真正傳遞studentList
,它更像是一個tests
變量。 這應該有助於減少一些混亂。
int *inputScores(int *tests, int numTests, int count)
之后,您會意識到即使將studentList[i].tests
傳遞給inputScores
函數,您也會出現內存泄漏,並且無法訪問您認為剛剛填充的內存。 您確實填充了內存,但是當您嘗試通過tests = new int[numTests];
為tests
分配新值時tests = new int[numTests];
,那只改變了tests
的本地副本。
要解決該問題,您需要通過引用傳遞tests
,如下所示:
int *inputScores(int*& tests, int numTests, int count)
這將允許您在函數中對int*
、 tests
所做的任何更改保留在函數之外。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.