简体   繁体   中英

How can I solve this pointer/memory issue?

I trying to write a program that asks the user for movie information. stores the information of a movie as a struct in a vector and then output the result to the screen with the 2 functions having a return type of void.

#include <iostream> 
#include <iomanip> 
#include <vector>
#include <string>
using namespace std;

void make_movie(struct movie *film);
void show_movie(vector <movie> data, int cnt);

struct movie {
    string name;
    string director;
    int year;
    int duration;
};

int main() {

    int count = 0;
    char input;
    vector <movie> record;
    movie *entry = nullptr;

    do {

        make_movie(entry);
        record.push_back(*entry);
        count++;

        cout << endl;
        cout << "Do you have more movie info to enter?\n";
        cout << "Enter y / Y for yes or n / N for no: ";
        cin.ignore();
        cin >> input;
        cout << endl;


    } while (input == 'y' || input == 'Y');

    show_movie(record, record.size());

    return 0;
}

void make_movie(struct movie *film) {

    cout << "Enter the title of the movie: ";
    cin.ignore();
    getline(cin, film -> name);

    cout << "Enter the director's name: ";
    cin.ignore();
    getline(cin, film -> director);

    cout << "Enter the year the movie was created: ";
    cin >> film -> year;

    cout << "Enter the movie length (in minutes): ";
    cin >> film -> duration;

}

void show_movie(vector <movie> data, int cnt) {

    cout << "Here is the info that you entered: " << endl;

    for (int i = 0; i < cnt; i++) {

        cout << "Movie Title: " << data[i].name << endl;
        cout << "Movie Director: " << data[i].director << endl;
        cout << "Movie Year: " << data[i].year << endl;
        cout << "Movie Length: " << data[i].duration << endl;
        cout << endl;
    }
 }

I am getting a error that says that i am trying to access a prohibited memory address.

The least amount of changes you need to make is to change:

movie *entry = nullptr;

do {
    make_movie(entry);
    record.push_back(*entry);

to:

movie entry;

do {
    make_movie(&entry);
    record.push_back(entry);

Further improvements would be:

  • Change make_movie to accept parameter by reference, then your program does not use any pointers and therefore is not vulnerable to any of the problems associated with pointers.
  • Change make_movie to return by value instead of taking a reference parameter.
  • cin.ignore(); is being used incorrectly. Your program will lose the first character of several of the input strings. Instead, remove all of those calls, and at the end of the make_movie function, ignore the rest of the current line. Also, change cin >> input; to use getline .

your bug
movie *entry = nullptr;

and
you have extra cin.ignore();

    cout << "Enter the title of the movie: ";
//    cin.ignore();
    getline(cin, film -> name);

    cout << "Enter the director's name: ";
//    cin.ignore();
    getline(cin, film -> director);

how to fix

movie main_info;
movie* entry = &main_info;

test

intput:

Enter the title of the movie: any_thing  
Enter the director's name: yourself  
Enter the year the movie was created: 2016  
Enter the movie length (in minutes): 120  

Do you have more movie info to enter?  
Enter y / Y for yes or n / N for no: n  

output

 Here is the info that you entered: Movie Title: any_thing Movie Director: yourself Movie Year: 2016 Movie Length: 120 

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