visual Using A Delete Keyword With Arrays C++

visual Using A Delete Keyword With Arrays C++

本文关键字:With Arrays C++ Keyword Delete Using visual      更新时间:2023-10-16

因此,在我的程序中添加了另一个功能,我决定包含一个可以从myCourses[10]中删除所有信息的函数。最新代码:

#include <iostream>
#include <string>
#include <fstream>
using namespace std;
struct Course
{
string name;
double grade;
int block;
};
Course enter_course()
{
Course foo;
cout << "What is the name of the course you wish to enter? (Use this format: ExampleFormat)n";
cin >> foo.name;
cout << "What block is " << foo.name << " ?n";
cin >> foo.block;
cout << "What is your current grade as a percent?n";
cin >> foo.grade;
return foo;
}
void display_courses(Course courseList[10], int courseCount)
{
for (int i=0; i<courseCount; i++){
cout << i+1 << "t" << courseList[i].name 
<< "ttBlock: " << courseList[i].block
<< "tGrade: " << courseList[i].grade << "%" << endl;
}
}
double get_gpa(Course courseList[10], int courseCount)
{
double gradePoints;
double total = 0.0;
for (int i=0; i<courseCount; i++){
if (courseList[i].grade < 100){
gradePoints = 4;
} 
if (courseList[i].grade < 90){
gradePoints = 3;
}
if (courseList[i].grade < 80){
gradePoints = 2;
}
if (courseList[i].grade < 70){
gradePoints = 1;
}
if (courseList[i].grade < 60){
gradePoints = 0;
}
total += gradePoints;
}
return total*1.0/courseCount;
}
void display_options()
{
cout << "1. Exitn";
cout << "2. Enter a Coursen"; 
cout << "3. Display Coursesn";
cout << "4. Display GPAn";
cout << "5. Request a text file outputn";
cout << "6. Delete Gradesn";
cout << "nn";
}
int main()
{
bool exit=0;
int option;
int courseCount=0;
Course myCourses[10]; //nobody should ever take more than 10 courses! 
while (exit == 0)
{
cout << "GradeBook 2.0n";
display_options();
cout << "Enter a command.n";
cin >> option;
switch (option)
{
case 1: 
exit = 1;
break;
case 2:
myCourses[courseCount] = enter_course();
courseCount++;
break;
case 3:
display_courses(myCourses, courseCount);
break;
case 4:
cout << get_gpa(myCourses, courseCount) << endl;
break;
case 5:
ofstream outputFile;
outputFile.open("userGrades.txt");
for (int i = 0; i < courseCount; i++)
{
outputFile << myCourses[i].name << " " << myCourses[i].grade << " " << myCourses[i].block << endl;
}
outputFile.close();
cout << "Grades saved to file!" << endl;
break;
case 6:
cout << "Removing data...n";
delete[] myCourses;
break;
}
}
return 0;
}

但是,我收到以下错误:错误C2360:"case"标签跳过了"outputFile"的初始化(第114行)IntelliSense:控制权的传输绕过:变量"outputFile"(在第105行声明)的初始化(第89行)警告C4154:删除数组表达式;转换为提供的指针(第116行)有人知道在开关块中使用delete[]关键字有什么问题吗?

问题是您正在尝试delete,而不是用new创建的东西。也就是说,阵列myCourses不是动态分配的。您可以使用new动态分配一些内容,然后使用delete解除分配。如果你不动态地分配它,你只会让它超出范围。

只需去掉delete[] myCourses;行。

不能从数组中删除元素。它们从声明数组的那一刻起就存在,直到数组超出范围。如果你希望元素有某种清除状态,你需要决定它是什么。例如,你可以给Course一个标志,说明它是否被清除。然而,一个更好的方法是使用像std::vector这样的标准库容器——这样,你就可以真正地从中删除元素

另一个错误是由于试图在switch语句中声明一个变量而没有将其包含在自己的块中。可以通过在case 5:的内容周围放置大括号来解决此问题。

你不能delete你没有new。它们必须对应。您正在尝试delete一个从未分配过new的对象。

由于其他人已经对不明智的delete[]发表了评论,让我们也解决另一个问题:如果你想在switch-语句的块内构造一个对象,你需要将它放入自己的块中:

switch (option)
{
case 5:
{
ofstream outputFile;
outputFile.open("userGrades.txt");
for (int i = 0; i < courseCount; i++)
{
outputFile << myCourses[i].name << " " << myCourses[i].grade << " "
<< myCourses[i].block << endl;
}
outputFile.close();
cout << "Grades saved to file!" << endl;
break;
}
}

(注意std::ofstream信息块周围的额外括号)。另外,请注意,不必要地使用std::endl会导致性能问题!除非您真的想刷新流,否则请使用'n'。。。当您想要刷新流时,请使用CCD_ 22,而不是使用std::endl隐藏它。