2016-04-15 3 views
-1

私は生徒の構造を作成しようとしていますが、その構造の中には、どのようにマークを入力したかに基づいて等級(またはマーク)の配列があります。構造要素の動的配列を作成し、その構造の動的配列を作成します。 C++

次に、Student構造の動的配列を作成しようとしています。

私はこれらのポインタと対話したいと思います。つまり、学生の情報と成績を入力してください。しかし、私は正しくこれをやっているとは思っていません。ここに私のコードの最初の部分があります。私の主な問題は、一連のマークを作成することです。教科書に宣言する方法は見つけられません。

#include <string.h> 
#include <iostream> 
#include <sstream> 
using namespace std; 


struct Student 
{ 
    string name; 
    int id; 
    int* mark; 
    ~Student() 
{ 
    delete [] mark; 
    mark = NULL; 
}; 
}; 

void initStudent(Student* ptr, int markNum, int studentNum); // function prototype for initialization 
void sayStudent(Student* ptr, int markNum, int studentNum);  // function prototype for printing 

//*********************** Main Function ************************// 
int main() 
{ 
    int marks, studentNum; 
    Student stu;   // instantiating an STUDENT object 
    Student* stuPtr = &stu; // defining a pointer for the object 
    cout << "How many marks are there? "; 
    cin >> marks; 
    cout << "How many students are there?"; 
    cin >> studentNum; 
    Student* mark = new int[marks]; 
    Student* students = new Student[studentNum]; 

    initStudent(students,marks,studentNum);  // initializing the object 
    sayStudent(students,marks,studentNum);  // printing the object 
    delete [] students; 

return 0; 

} // end main 

//-----------------Start of functions----------------------------// 

void initStudent(Student* ptr, int markNum, int studentNum) 
{ 
    for (int i = 0; i < studentNum; i++) 
    { 

     cout << "Enter Student " << i+1 << " Name :"; 
     cin >> ptr[i].name; 
     cout << "Enter Student ID Number :"; 
     cin >> ptr[i].id; 
     for (int j = 0; j < markNum; j++) 
     { 
     cout << "Please enter a mark :"; 
     cin >> ptr[i].mark[j]; 
     } 
    } 
    } 
+1

*私はそれを宣言するためにどのように私の教科書で見つける傾ける* - 。 'のstd ::ベクトル' 'のstd ::ベクトル' - 別のテキストブックを取得します。 – PaulMcKenzie

+0

@PaulMcKenzie私は動的にメモリを割り当てる必要があります。ベクトルを作成するだけではありません。 –

+1

@RoyGunderson _ "私は動的にメモリを割り当てる必要があります" _ 'std :: vector'は実際にはどう思いますか? –

答えて

0

あなたは、各Student要素でmarks配列を割り当てる必要があります。

Student* students = new Student[studentNum]; 
for (int i = 0; i < studentNum; i++) { 
    students[i].mark = new int[marks]; 
} 

initStudentでも可能です。

void initStudent(Student* ptr, int markNum, int studentNum) 
{ 
    for (int i = 0; i < studentNum; i++) 
    { 

     cout << "Enter Student " << i+1 << " Name :"; 
     cin >> ptr[i].name; 
     cout << "Enter Student ID Number :"; 
     cin >> ptr[i].id; 
     ptr[i].mark = new int[markNum] 
     for (int j = 0; j < markNum; j++) 
     { 
      cout << "Please enter a mark :"; 
      cin >> ptr[i].mark[j]; 
     } 
    } 
    } 
+0

ありがとうございます。彼女は完璧に働く。 –

関連する問題