c++ - 在数组 C++ 中使用删除关键字

标签 c++ visual-c++ visual-studio-2012

因此,我决定在我的程序中加入一个功能,您可以在其中删除 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 
             << "\t\tBlock: " << 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. Exit\n";
    cout << "2. Enter a Course\n"; 
    cout << "3. Display Courses\n";
    cout << "4. Display GPA\n";
    cout << "5. Request a text file output\n";
    cout << "6. Delete Grades\n";

    cout << "\n\n";
}

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.0\n";
        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 行) 有谁知道在 switch block 中使用 delete[] 关键字有什么问题?

最佳答案

问题是您正在尝试删除 不是您使用new 创建的内容。也就是说,数组 myCourses 不是动态分配的。您使用 new 动态分配某些内容,然后使用 delete 取消分配。如果你不动态分配它,你就让它超出范围。

简单地删除 delete[] myCourses; 行。

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

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

关于c++ - 在数组 C++ 中使用删除关键字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20752163/

相关文章:

c++ - Adobe Adam and Eve,C++ : How to create a clickable button, 轨迹栏、文本输入字段和图像?

c++ - 如何判断 C++ 模板类型是否为 C 样式字符串

c++ - Gcc 预处理器和粘贴

C++字符串,什么时候用什么?

visual-studio-2010 - 在 Visual Studio 2012 中编译 Moles 时出现错误 "The type or namespace xxxx does not exist"

来自链表的 C++ 队列

c++ - 在课后使用 typedef ClassName< >

c++ - 在 C++ 中使用异常基类导致应用程序崩溃

c# - 使用 C# 的字符串数组的总和

visual-studio-2012 - Visual Studio 2012 重定向 Microsoft.TeamFoundation.Client.dll 的绑定(bind)