2013-03-14 100 views
0

似乎我不明白爲什麼我得到一個:數組與結構C++

Segmentation fault (core dumped) 

當輸入我的「電影」進入電影結構。是否有任何明顯的邏輯錯誤或者什麼?

隨着分割故障我爲無效set_movies()似乎只返回4個提示爲電影時,它應該返回5因的#define NUM_MOVIES內環路5.

由於一噸!

#include <iostream> 
#include <string> 
#include <sstream> 

#define NUM_MOVIES 5 

using namespace std; 

struct movie{ 
    string name[]; 
    double copies[]; 
    double rating[]; 
    string description[]; 
    string genre[]; 
} films [NUM_MOVIES]; 

void set_movies(); 
int which_movies_to_view(); 
int get_movies(); 
int rent_movie(); 
int printmovie(); 
int choice; 

int main(){ 
    set_movies(); 
    which_movies_to_view(); 
    get_movies(); 
    rent_movie(); 

    return 0; 
} 

void set_movies(){ 
    movie set; 

    for(int i=0; i<NUM_MOVIES; i++){ 
     cout << "Enter movie title: " << endl; 
     cin >> set.name[i]; 
     cout << "Enter how many copies: " << endl; 
     cin >> set.copies[i]; 
     cout << "Enter the rating: " << endl; 
     cin >> set.rating[i]; 
     cout << "Enter a description: " << endl; 
     cin >> set.description[i]; 
     cout << "Enter the genre: " << endl; 
     cin >> set.genre[i]; 
    } 
} 

int which_movies_to_view(){ 
    movie set; 

    cout << " " << set.name[1] << set.name[2] << set.name[3] << set.name[4] << set.name[5] << endl; 
    cout << "Which movie would you like to view?: [1, 2, 3, 4, or 5]" << endl; 
    cin >> choice; 

    return choice; 
} 

int get_movies(){ 

    movie set; 

    if(choice == 1){ 
     cout << set.name[1] << endl; 
    } 
    if(choice == 2){ 
     cout << set.name[2] << endl; 
    } 
    if(choice == 3){ 
     cout << set.name[3] << endl; 
    } 
    if(choice == 4){ 
     cout << set.name[4] << endl; 
    } 
    if(choice == 5){ 
     cout << set.name[5] << endl; 
    } 

    return 0; 
} 

int printmovie(){ 

    int n; 
    for(int n = 0; n<NUM_MOVIES; n++) 
    cout << films[n].name; 
    cout << films[n].copies; 
    cout << films[n].rating; 
    cout << films[n].description; 
    cout << films[n].genre; 

    return 0; 
} 

int rent_movie(){ 
    movie set; 

    if(choice == 1){ 
      set.copies[0] - 1; 
      cout << set.copies[0] << " copies left!" << endl; 
    } 
    if(choice == 2){ 
      set.copies[1] - 1; 
      cout << set.copies[1] << " copies left!" << endl; 
    } 
    if(choice == 3){ 
      set.copies[2] - 1; 
      cout << set.copies[2] << " copies left!" << endl; 
    } 
    if(choice == 4){ 
      set.copies[3] - 1; 
      cout << set.copies[3] << " copies left!" << endl; 
    } 
    if(choice == 5){ 
      set.copies[4] - 1; 
      cout << set.copies[4] << " copies left!" << endl; 
    } 

    return 0; 
} 
+1

你寫未初始化的內存 - 經典[未定義行爲(HTTP://en.wikipedia .ORG /維基/ Undefined_behavior)。 – ildjarn 2013-03-14 01:02:29

+0

當你聲明'double copies []'時,編譯器如何知道要保留多少個字節? – Shoe 2013-03-14 01:03:59

回答

6

您正在將結構的成員聲明爲空數組,並且還聲明瞭這些結構的數組。

我覺得你真的想這樣:

struct movie{ 
    string name; 
    double copies; 
    double rating; 
    string description; 
    string genre; 
} films [NUM_MOVIES]; 

然後用films[i].moviefilms[i].copies

+0

這幫了我很多,感謝一噸! :) – 2013-03-14 01:17:49

4

「字符串名稱[];」這定義空數組,然後當你寫爲 「set.name [I]」,它會導致核心轉儲。所以結構電影的其他成員。

實際上,您可以使用gdb來讀取核心文件,它會告訴您發生核心轉儲的位置。

1

您正確使用您的數據結構的唯一功能是printmovie()。在其他地方,你使用諸如set.name[i]之類的東西是不正確的。此外,您應該從結構中的定義中刪除[]。否則,我相信它們被視爲指針類型。

0

這是因爲你沒有在電影結構聲明數組的大小。 請記住,C++從C繼承了許多功能,C強制聲明每個函數開始時要分配多少內存。

1

struct可以看看這個:

struct Movie 
{ 
    std::string name; 
    unsigned int copies; 
    double rating; 
    std::string description; 
    std::string genre; 
}; 

因爲你使用的是C++,你可能希望你的電影的大小靈活的名單,你應該使用std::vector代替C數組:

std::vector<Movie> movies; 

然後,你可以通過使用其push_back方法簡單地添加新的電影進入這個

而且當你要訪問這些電影后,你可以把它就像一個數組:

for (int i = 0; i < movies.size(); ++i) 
    std::cout << movies[i].name << std::endl;