2013-03-14 81 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;