c++ - struct output with cout, and function call struct parameters -
so, problem several errors @ compile time.
readmoviedata(string* title, string* director) cannot convert movieinfo string* displaymoviedata(string title, string director) cannot convert movieinfo string no operator found takes right-hand operand of type 'movieinfo' (or there no acceptable conversion.
the bottom error happens twice in displaymoviedata() wrote once simplicity sake.
the readmoviedata function should accept structure pointer reference variable , displaymoviedata function should accept movieinfo structure variable.
the main function creates array of 2 movieinfo struct variables , other functions should called on element of array.
the code have finished below.
#include <stdafx.h> #include <string> #include <iomanip> #include <iostream> using namespace std; //prototypes int readmoviedata(string* title, string* director); int displaymoviedata(string title, string director); struct movieinfo { string title, director; }; int main(){ const int size = 2; movieinfo movielist[size]; movieinfo movie; //supposed assign data movielist[i] @ point (int = 0; < size; i++){ readmoviedata(movie, movie); displaymoviedata(movie, movie); } return 0; } int readmoviedata(movieinfo &title, movieinfo &director){ movieinfo movie; //get movie name cout << "what movie? "; cin.ignore(); cin >> movie.title; //get movie director cout << "what director of " << movie.title << "?"; cin.ignore(); cin >> movie.director; return 0; } int displaymoviedata(movieinfo title, movieinfo director){ cout << "the movie name is: " << title << endl; cout << "the director of " << title << " is: " << director << endl; return 0; }
there mismatches between function prototypes , definitions, can see comparing parameter types in both.
note since defined structure movie info, can directly pass reading , displaying functions (instead of passing single structure data member strings).
you may want read following compilable code:
#include <iostream> #include <string> using namespace std; struct movieinfo { string title; string director; }; void readmoviedata(movieinfo& movie); void displaymoviedata(const movieinfo& movie); int main() { const int size = 2; movieinfo movielist[size]; (int = 0; < size; i++) { readmoviedata(movielist[i]); displaymoviedata(movielist[i]); } } // since movie output parameter in case, pass non-const reference. void readmoviedata(movieinfo& movie) { //get movie name cout << "what movie? "; cin >> movie.title; //get movie director cout << "what director of " << movie.title << "?"; cin >> movie.director; } // since movie input parameter in case, pass reference const. void displaymoviedata(const movieinfo& movie) { cout << "the movie name is: " << movie.title << endl; cout << "the director of " << movie.title << " is: " << movie.director << endl; }
Comments
Post a Comment