我似乎不明白为什么我会得到:
Segmentation fault (core dumped)
将我的“电影”输入电影结构时。这里是否有任何明显的逻辑错误或什么?
随着分段错误,我在 void set_movies() 中的 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;
}