2017-02-07 52 views
1

我想使用從用戶
例如輸入以創建結構的對象的數目創建對象他們的變數。使用用戶輸入

#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;學生p [n];'工作得很好。對於OP:那麼問題是什麼?我看到很多錯別字/基本錯誤,但不能真正發佈對不存在問題的答案 – Fureeish

+3

@Fureeish然後,您可能正在使用一些非標準的編譯器擴展。 –

回答

1

有許多與你的榜樣的問題。第一個問題是student p[n];。這不是嚴格有效的C++。一些編譯器允許它作爲擴展。不知道你在使用哪種編譯器,並且使用什麼標記,我會認爲這是問題的一部分。此問題的典型解決方案是使用std::vector。一個std::vector在許多方面工作,像一個可變大小的數組。 std::vector<student> p(n);將創建一個名爲p的向量,其中包含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); 

考慮使用基於範圍的循環而不是常規的循環。

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非常感謝,它幫助我清楚了很多概念。感謝Rama讓我知道不同的方法 – Step