简体   繁体   中英

Can't call struct function in main

So, for some reason, my code won't compile and I keep running into this error:

could not convert '(Movie*)(& myMovie)' from 'Movie*' to 'Movie'**

Not sure what does it mean when it says that, appreciate it if someone can tell me whats wrong with my code, also, new to structs so apologies for any stupid mistakes.

I want to output what the user entered when prompted for the movie details questions, but as I mentioned not working.

Thanks. Here is my code :

#include <iostream>
#include <cstring>
#include <iomanip>
#include <climits>
#include <cstdlib>

using namespace std;

const int MAX = 256;
const int MAXNUM = 10;
const int MINNUM = 1;

typedef char  Word[MAX];

//define a struct for a Movie
struct Movie{
  Word title;
  Word Director;
  float length;
};


// prompts user for number between min and max, and returns valid input
int getIntInRange(int min, int max);



//prompts user for a positive float, and returns valid value
float getPositiveFloat();


//prompts user to enter all the values in a movie and returns the movie

//implement these two functions

Movie getMovieFromUser();
//outputs a single movie

void outputMovie(Movie m);



//describe program to user
void displayOverview();


int main(){

//output overview of program

//prompt user to enter their favourite movie

//use the getMovieFromUser() function to get the movie and store it in a variable

//output the movie using the outputMovie function


  cout << endl;

  Movie myMovie[MAXNUM];
  getMovieFromUser(????);
  outputMovie(????);


return 0;
}

Movie getMovieFromUser(){

    Movie myMovie;
    //prompt user to enter title, and save it
    //prompt user to enter director and save it
    //prompt user for length, and save it

    Word buffer;
    cout << "Please enter your favourite movie \n";
    cout << "Title: ";
    cin.getline(buffer, MAX);
    strcpy(m.title, buffer);
    cout << "Director: ";
    cin.getline(buffer, MAX);
    strcpy(m.Director, buffer);
    m.length = getPositiveFloat();


    return myMovie;
}


void outputMovie(Movie m){
//output the contents of this movie
    cout << "Title: " << m.title << endl;
    cout << "Director: " << m.Director << endl;
    cout << "Length (minutes): " << m.length << endl;
}

Your code doesn't agree with itself. You have:

Movie getMovieFromUser();

But then later:

Movie getMovieFromUser(Movie &m){

The first seems to make more sense. A function called getMovieFromUser can either take a reference parameter that it fills in or return a value. Returning a value seems to make more sense. With that fix, this code should compile:

Movie myMovie[MAXNUM];
myMovie[0] = getMovieFromUser();
outputMovie(myMovie[0]);

You'll need to get rid of the Movie &m parameter in getMovieFromUser . Just create one locally (as you already do). I'd suggest calling it m rather than myMovie as it can create confusion to have two variables with the same name.

Putting that together:

Movie getMovieFromUser(){

    Movie m;
    //prompt user to enter title, and save it
    //prompt user to enter director and save it
    //prompt user for length, and save it

    Word buffer;
    cout << "Please enter your favourite movie \n";
    cout << "Title: ";
    cin.getline(buffer, MAX);
    strcpy(m.title, buffer);
    cout << "Director: ";
    cin.getline(buffer, MAX);
    strcpy(m.Director, buffer);
    m.length = getPositiveFloat();
    return m;
}

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