Note that there are some explanatory texts on larger screens.

plurals
  1. POArrays vs. Structs C++
    primarykey
    data
    text
    <p>I don't seem to understand why I'm getting a:</p> <pre><code>Segmentation fault (core dumped) </code></pre> <p>When inputting my 'movies' into the movie struct. Are there any blaring obvious logic errors here or what?</p> <p>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.</p> <p>Thanks a ton!</p> <pre><code>#include &lt;iostream&gt; #include &lt;string&gt; #include &lt;sstream&gt; #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&lt;NUM_MOVIES; i++){ cout &lt;&lt; "Enter movie title: " &lt;&lt; endl; cin &gt;&gt; set.name[i]; cout &lt;&lt; "Enter how many copies: " &lt;&lt; endl; cin &gt;&gt; set.copies[i]; cout &lt;&lt; "Enter the rating: " &lt;&lt; endl; cin &gt;&gt; set.rating[i]; cout &lt;&lt; "Enter a description: " &lt;&lt; endl; cin &gt;&gt; set.description[i]; cout &lt;&lt; "Enter the genre: " &lt;&lt; endl; cin &gt;&gt; set.genre[i]; } } int which_movies_to_view(){ movie set; cout &lt;&lt; " " &lt;&lt; set.name[1] &lt;&lt; set.name[2] &lt;&lt; set.name[3] &lt;&lt; set.name[4] &lt;&lt; set.name[5] &lt;&lt; endl; cout &lt;&lt; "Which movie would you like to view?: [1, 2, 3, 4, or 5]" &lt;&lt; endl; cin &gt;&gt; choice; return choice; } int get_movies(){ movie set; if(choice == 1){ cout &lt;&lt; set.name[1] &lt;&lt; endl; } if(choice == 2){ cout &lt;&lt; set.name[2] &lt;&lt; endl; } if(choice == 3){ cout &lt;&lt; set.name[3] &lt;&lt; endl; } if(choice == 4){ cout &lt;&lt; set.name[4] &lt;&lt; endl; } if(choice == 5){ cout &lt;&lt; set.name[5] &lt;&lt; endl; } return 0; } int printmovie(){ int n; for(int n = 0; n&lt;NUM_MOVIES; n++) cout &lt;&lt; films[n].name; cout &lt;&lt; films[n].copies; cout &lt;&lt; films[n].rating; cout &lt;&lt; films[n].description; cout &lt;&lt; films[n].genre; return 0; } int rent_movie(){ movie set; if(choice == 1){ set.copies[0] - 1; cout &lt;&lt; set.copies[0] &lt;&lt; " copies left!" &lt;&lt; endl; } if(choice == 2){ set.copies[1] - 1; cout &lt;&lt; set.copies[1] &lt;&lt; " copies left!" &lt;&lt; endl; } if(choice == 3){ set.copies[2] - 1; cout &lt;&lt; set.copies[2] &lt;&lt; " copies left!" &lt;&lt; endl; } if(choice == 4){ set.copies[3] - 1; cout &lt;&lt; set.copies[3] &lt;&lt; " copies left!" &lt;&lt; endl; } if(choice == 5){ set.copies[4] - 1; cout &lt;&lt; set.copies[4] &lt;&lt; " copies left!" &lt;&lt; endl; } return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload