【发布时间】:2020-02-17 20:56:00
【问题描述】:
我正在为一个 OO C++ 课程分配作业,但我很难过。任务是将等级 (int) 添加到动态数组并将该数组的大小增加 1。该数组必须从零开始。以下是给我们的规格:
规范包。
- 规范 B1 - 动态阵列 在堆上创建一个数组。将学生成绩存储在其中。
- 规范 B2 - 添加元素 以 0 大小开始数组,每次添加时将其增加一 带有菜单选项 1 的新乐谱。
我在我们的书中查看了网络上的许多示例,但我只是把头撞到墙上。我已经移动了一些东西,试图把东西放在不同的顺序等等。我想出了下面的代码,它就是行不通。我很确定有经验的人会告诉我我忘记了逗号,但是就这样。我理解基本概念是创建动态数组,创建一个大一号的临时数组,然后将原始数组的元素复制到临时数组,然后将动态数组指向临时数组的内存地址,删除旧阵列然后清洗冲洗重复,但我似乎不能这样做。我需要帮助 :) 目前我收到一条堆损坏错误消息。这是代码。
#include <iostream>
#include <cstdlib> // do not use
#include <ctime> // for seeding random numbers
#include <string>
using namespace std;
//Global Variables
//Function Prototypes
void ProgramGreeting(); // All programs will have this method - Draw a happy litte tree
int mainMenu(int[], int*, int*); //Main Menu
void addGrade(int[], int*, int*); // Add a grade to the list. Takes the array and the number of items in the array
void displayGrades(int[], int); // Display all the grades. Takes the array and the number of items in the array
void processGrades(int[], int); // Process all the grades. Takes the array and the number of items in the array
char letterGrade(int); // Return a letter grade
void Unittest(); // All CISP400 programs should have this.
int main()
{
// Specification B1 - Dynamic Array
int* grades = NULL;
int max = 0;
int numofGrades = 0; //A counter for the size of the dynamic array
grades = new int[max];
ProgramGreeting();
mainMenu(grades, &numofGrades, &max);
delete[] grades;
return 0;
}
void ProgramGreeting()
{
// Specification C1 - Program Greeting Function
cout << "Welcome to GPA Analyzer!" << endl;
cout << "Written by William Graves" << endl;
cout << "This assignment is due on February 16, 2020" << endl;
}
int mainMenu(int grades[], int *numofGrades, int *max)
{
char ans;
do {
cout << "Main Menu" << endl;
cout << "--------------" << endl;
cout << "1) Add Grade" << endl;
cout << "2) Display All Grades" << endl;
cout << "3) Process All Grades" << endl;
cout << "4) Quit" << endl << endl;
cout << "Enter your choice: ";
cin >> ans;
switch (ans) {
case '1': //The user selected add grade
addGrade(grades, numofGrades, max);
break;
case '2': //Display all grades
displayGrades(grades, *numofGrades);
break;
case '3': //Display all grades
processGrades(grades, *numofGrades);
break;
case '4': //The user chose to exit.
cout << "Exit time. ";
return 1000;
break;
// Specification C4 - Bulletproof Menu
default:
cout << "Your selection of '" << ans << "' is invalid. Try again." << endl;
break;
}
} while (1);
}
void addGrade(int grades[], int *numofGrades, int *max) // Add a grade to the list. Takes the array and the number of items in the array
{
int gradeEntry = 0;
cout << "Enter the grade: ";
cin >> gradeEntry;
if (gradeEntry <= 100 && gradeEntry >= 0) //verify that the grade entered is between 0 and 100
{
grades[*numofGrades] = gradeEntry;
cout << "Grade of " << gradeEntry << " added successfully.";
*numofGrades = *numofGrades + 1;
if (*numofGrades >= *max)
{
*max += 1;
//create a temporary array a size bigger:
int* tempArray = new int[*max];
//copy the contents of the old array to the newly allocated array
for (int i = 0; i < *numofGrades; i++)
{
tempArray[i] = grades[i];
}
//get rid of the old array.
delete[] grades;
//change the memory location.
grades = tempArray;
}
return;
}
else
{
cout << "Error occured. User entered: " << gradeEntry << " The grade must be an integer between 0 and 100. No grade added." << endl;
return;
}
}
【问题讨论】:
-
一个问题是您正在修改
grades的本地副本。您的更改不会传回给调用者。 -
所有这些东西都用指针填充而不是使用 std::vector 有什么原因吗?还有,
new int[0]= ? -
@MichaelChourdakis - 也许是因为他们正在做课堂作业。我认为讨论最佳实践的最佳地点是在答案中,这样您就可以充分充实您的担忧。