简体   繁体   中英

Arrays vs. Structs C++

I don't seem to understand why I'm getting a:

Segmentation fault (core dumped)

When inputting my 'movies' into the movie struct. Are there any blaring obvious logic errors here or what?

Along with the segmentation fault my for loop within the void set_movies() seems to only be returning 4 prompts for movies when it should be returning 5 due to the #define NUM_MOVIES 5.

Thanks a ton!

#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;
}

You are declaring the members of your struct as empty arrays, but also declaring an array of those structs.

I think you actually want this:

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

And then use films[i].movie , films[i].copies , etc.

“string name[];” this defines empty array, then when you write to "set.name[i]", it causes core dump. So to other memebers of struct movie.

Actually, you can use gdb to read core file, that will show you where core dump happened.

The only function in which you have correctly used your data structure is printmovie() . Everywhere else where you use things like set.name[i] is incorrect. Furthermore, you should remove the [] from the definitions inside your struct. Otherwise I believe that they are treated as pointer types.

Your struct could look this:

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

and since you are using C++ and you probably want to be your list of movies flexible in size, you should use std::vector instead of C-style array:

std::vector<Movie> movies;

Then you can simply add new movies into this vector by using its push_back method:

Movie m;
// set m's data members here
movies.push_back(m);

And later when you want to access these movies, you can treat it just like an array:

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

This is because you are not declaring array's size in movie struct. Remember that C++ inherits many features from C, who forces to declare how many memory you want to allocate at the beginning of each functions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM