读入动态创建的指针数组结构数组

问题描述:

这是一个必须使用动态创建的Course数组完成的类指派。我正试图读取每个成员变量里面的我的for循环,但我不知道如何去做。我用我的学生结构做了它,但是这是一个数组的差异让我感到困惑,因为我不知道如何继续。读入动态创建的指针数组结构数组

我的问题是在尝试读取struct成员时在readCourseArray函数中。如果有人能告诉我我该怎么做,我会很感激。我知道使用new经营者不得与许多指针是不必要沿着理想,但它是我的教练是多么需要分配中被打开。

#include <iostream> 
#include <string> 
using namespace std; 

struct Student 
    { 
     string firstName, lastName, aNumber; 
     double GPA; 
    }; 
struct Course 
    { 
     int courseNumber, creditHours; 
     string courseName; 
     char grade; 
    }; 

Student* readStudent(); 
Course* readCourseArray(int); 
int main() 
{ 
    int courses = 0; 
    Student *studentPTR = readStudent(); 
    Course *coursePTR = readCourseArray(courses); 


    delete studentPTR; 
    delete coursePTR; 
    system ("pause"); 
    return 0; 
} 

Student* readStudent() 
{  Student* student = new Student; 
    cout<<"\nEnter students first name\n"; 
    cin>>student->firstName; 
    cout<<"\nEnter students last name\n"; 
    cin>>student->lastName; 
    cout<<"\nEnter students A-Number\n"; 
    cin>>student->aNumber; 


    return student; 
} 

Course* readCourseArray(int courses) 
{ 
    cout<<"\nHow many courses is the student taking?\n"; 
    cin>>courses; 
    const int *sizePTR = &courses; 
    Course *coursePTR = new Course[*sizePTR]; 

    for(int count = 0; count < *sizePTR; count++) //Enter course information 
    { 
     cout<<"\nEnter student "<<count<<"'s course name\n"; 
     cin>>coursePTR[count]->courseName>>endl; 
     cout<<"\nEnter student "<<count<<"'s course number\n"; 
     cin>>coursePTR[count]->courseNumber; 
     cout<<"\nEnter student "<<count<<"'s credit hours\n"; 
     cin>>coursePTR[count]->creditHours; 
     cout<<"\nEnter student "<<count<<"'s grade\n"; 
     cin>>coursePTR[count]->grade>>endl; 
    } 


    return coursePTR; 
} 
+1

你应该知道的一件直接的事情是,在返回指针的函数中,你不应该返回一个局部变量。当你在函数中创建一个指针时,当函数结束时它会超出范围,当你尝试使用现在已故的数据时会遇到问题。当你使用'cin'读取变量时,'coursePTR [count]'被视为一个普通变量,所以使用'。 operator'。你会使用'coursePTR'上的箭头。 'coursePTR [count]'已经被解除引用,因为'coursePTR [count]'被读为'*(coursePTR + count)' – chris 2012-02-22 23:30:21

数组下标运算符将返回数组的元素。

coursePTR[count]相当于将指针递增到数组的开始并取消引用结果,如:*(coursePTR + count)。你得到的是一个类型为Course的对象(或一个对象的引用)。所以你需要使用“点”操作符,而不是“箭头”操作符来访问的元素:

cin >> coursePTR[count].creditHours; 

你已经有了另一个错误:

cin >> coursePTR[count].courseName >> endl; 
             ^^^^ 

这不会编译。 endl只能用于输出流。

Course* readCourseArray(int &courses); // Update the definition to pass "courses" by reference. 

Course* readCourseArray(int &courses) // Pass the courses by reference so that your main() has the value updated. 
{ 
    cout<<"\nHow many courses is the student taking?\n"; 
    cin>>courses; 
    /* 
     You don't need this line. 
    */ 
    // const int *sizePTR = &courses; 

    /* 
     You've allocated space for "courses" no. of "Course" objects. 
     Since this is essentially an array of "Course" object, you 
     just have to use the "." notation to access them. 
    */ 
    Course *coursePTR = new Course[courses]; 

    /* 
     "endl" cannot be used for input stream. 
    */ 
    for(int count = 0; count < courses; count++) //Enter course information 
    { 
     cout<<"\nEnter student "<<count<<"'s course name\n"; 
     cin>>coursePTR[count].courseName; 
     cout<<"\nEnter student "<<count<<"'s course number\n"; 
     cin>>coursePTR[count].courseNumber; 
     cout<<"\nEnter student "<<count<<"'s credit hours\n"; 
     cin>>coursePTR[count].creditHours; 
     cout<<"\nEnter student "<<count<<"'s grade\n"; 
     cin>>coursePTR[count].grade; 
    } 

    return coursePTR; 
}