2017-02-07 5 views
1

を使用してオブジェクトを作成する:
Iユーザー値nを受け入れ、オブジェクトのn数を作成し、Iは初期化関数にこれらのオブジェクトを渡したいですそれらに変数。Iは、例えばユーザ<br> からの入力を使用して、構造オブジェクトの数を作成するユーザ入力

#include <iostream> 
#include<string> 
#include "stdio.h" 

using namespace std; 

struct student 
{ 
    int roll_no; 
    char name[20]; 
}; 

void get_input(student p[],int n1) 
{ 
    for(int i=1;i<=n1;i++) 
    { 
     cout<<"Enter Roll Number "; 
     cin>>p[i].roll_no; 
     cout<<"\n Enter Name of the student: "; 
     cin>>p[i].name; 
    } 
} 

int main() 
{ 

    int n; 
    cout<<"How many student details would you want to enter: "; 
    cin>>n; 
    //Want to create number of object based on input n 
    student p[n]; 
    get_input(student p[],n); 

    return 0; 
} 
+5

可変長配列はC++では使用できません。代わりに 'std :: vector 'を使用してください。 – nwp

+0

@nwpどういう意味ですか? 'cin >> n; student p [n]; 'は完璧に動作します。 OPに:うーん、質問は何ですか?私は多くの誤植/基本的なエラーを見ていますが、実際には存在しない質問への回答を投稿することはできません。 – Fureeish

+3

@Fureeish次に、スタンドアロン以外のコンパイラ拡張を使用している可能性があります。 –

答えて

1

あなたの例で多くの問題があります。

最初の問題はstudent p[n];です。これは厳密にはC++ではありません。いくつかのコンパイラでは、としてを使用できます。どのコンパイラを使用しているか、どんなフラグを使用しているかわからなければ、これは問題の一部であると仮定します。この問題の典型的な解決策はstd::vectorです。 std::vectorは、可変サイズの配列のように多くの方法で機能します。 std::vector<student> p(n); という名前のベクトルを作成し、nがデフォルトで構築されたstudentのオブジェクトを作成します。

次の問題はget_input(student p[],n);です。引数を渡すときに型の名前を付ける必要はありません。 get_input(p,n);と書いてください。結局、get_inputと呼んだときにnintと指定されていませんでした。ただし、pは現在std::vectorであるため、.data()を追加して実際のデータへのポインタを取得する必要があります。 get_input(p.data(), n);になります。

最後に重要な問題は、ループfor (int i = 1; i <= n1; i++)です。 nが3であると想像してください。値iは1,2および3です。ただし、0から始まる配列のインデックスが付けられます。nが3の場合、要素0,1および2にアクセスします。正しいループはfor (int i = 0; i < n1; i++)です。

これらの変更により、の作業はになりますが、改善がまだたくさんあります。

#include <iostream> 
#include <vector> 

using namespace std; 

struct student 
{ 
    int roll_no; 
    char name[20]; 
}; 

void get_input(student p[], int n1) 
{ 
    for (int i = 0; i < n1; i++) 
    { 
     cout << "Enter Roll Number "; 
     cin >> p[i].roll_no; 
     cout << "\n Enter Name of the student: "; 
     cin >> p[i].name; 
    } 
} 

int main() 
{ 

    int n; 
    cout << "How many student details would you want to enter: "; 
    cin >> n; 
    //Want to create number of object based on input n 
    std::vector<student> p(n); 
    get_input(p.data(), n); 


    return 0; 
} 

std::string代わりのchar name[20]を使用することを検討してください。あなたは名前の長さを推測する必要はなく、定義されていない動作にはより長い名前を付ける危険性はありません。

struct student 
{ 
    int roll_no; 
    std::string name; 
}; 

ポインタとサイズを使用する代わりに、pを参照渡しすることを検討してください。

// Declaration/definition 
void get_input(std::vector<student> & p) 

// Usage 
get_input(p); 

通常のforループの代わりにranged forループを使用することを検討してください。

void get_input(std::vector<student> & p) 
{ 
    // for each student in p 
    for (student & s : p) 
    { 
     cout << "Enter Roll Number "; 
     cin >> s.roll_no; 
     cout << "\n Enter Name of the student: "; 
     cin >> s.name; 
    } 
} 
0

studentvectorを使用します。ここではあなたがそれを行うことができる方法のいくつかのサンプルコードです:

#include <iostream> 
#include <string> 
#include <vector> 
#include "stdio.h" 

using namespace std; 

struct student 
{ int roll_no; 
    char name[20]; 
}; 

void get_input(vector<student> & p1, int n1) 
{ 
    for (int i=0; i<n1; i++) 
    { 
     student s; 
     cout<<"Enter Roll Number: "; 
     cin>>s.roll_no; 
     cout<<"\n Enter Name of the student: "; 
     cin>>s.name; 
     p1.push_back(s); 
    } 
} 

int main() 
{ 

    int n; 
    cout<<"How many student details would you want to enter: "; 
    cin>>n; 
    //Want to create number of object based on input n 
    vector<student> p; 
    get_input(p, n); 


    return 0; 
} 
+0

@Francoisは多くのコンセプトをクリアするのに役立ちました。私に違ったアプローチを教えてくれたラマに感謝します – Step

関連する問題