Arrays vs. Structs C++

Enz*_*ile 0 c++ arrays struct class

我似乎不明白为什么我得到一个:

Segmentation fault (core dumped)
Run Code Online (Sandbox Code Playgroud)

将我的"电影"输入电影结构时.这里有什么明显的逻辑错误或什么?

随着分段错误,我在for void循环中的for循环似乎只返回4个电影提示,因为#define NUM_MOVIES 5应该返回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;
}
Run Code Online (Sandbox Code Playgroud)

Kri*_*son 6

您将结构的成员声明为空数组,但也声明了这些结构的数组.

我想你真的想要这个:

struct movie{
   string name;
   double copies;
   double rating;
   string description;
   string genre;
} films [NUM_MOVIES];
Run Code Online (Sandbox Code Playgroud)

然后使用films[i].movie,films[i].copies等等.