繁体   English   中英

数组与结构C ++

[英]Arrays vs. Structs C++

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

Segmentation fault (core dumped)

将我的“电影”输入到电影结构中时。 这里有什么明显的逻辑错误吗?

与分段错误一起,由于#define NUM_MOVIES 5,我的for循环在void set_movies()中的for循环似乎仅返回4条电影提示,而应返回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;
}

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

我认为您实际上想要这样:

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

然后使用films[i].moviefilms[i].copiesfilms[i].copies等。

“ string name [];”定义了一个空数组,然后当您写入“ set.name [i]”时,将导致核心转储。 对结构电影的其他成员也是如此。

实际上,您可以使用gdb读取核心文件,这将向您显示发生核心转储的位置。

正确使用数据结构的唯一函数是printmovie() 您在其他任何使用set.name[i]类的地方的地方都不正确。 此外,您应该从结构内部的定义中删除[] 否则,我认为它们被视为指针类型。

您的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方法将新电影简单地添加到此vector

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

之后,当您要访问这些电影时,可以将其视为数组:

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

这是因为您没有在影片结构中声明数组的大小。 请记住,C ++继承了C的许多功能,后者强制声明要在每个函数的开头分配多少内存。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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